Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

php mysql - displaying results with foriegn key

Featured Replies

I am new to php/coding, please help someone... :(

 

I have trouble using php to display the corresponding name given the ID from another table.

  • I have agent_id from the job table.
  • I need agent_name from the agent table.

I can write the SQL, but how do I use PHP to find this? I'm not sure how to use mysql_result function.

 

This is my code so far....

 

							   <?PHP

	  // ==========================================
	  // RUN SQL QUERY
	  // ==========================================
	  $query="SELECT * FROM job ORDER BY job_date_created DESC LIMIT 0, 2";
	  $result=mysql_query($query);
	  $num=mysql_numrows($result);

	  // ==========================================
	  // DISPLAY RESULTS
	  // ==========================================
	  $i=0;
	  while ($i < $num)
	  {
		  $jobId = mysql_result($result,$i,"job_id");
		  $jobDateCreated = mysql_result($result,$i,"job_date_created");
		  $jobDateCreated = substr("$jobDateCreated", 0, 10);  // keep first 10 characters only
		  $jobTitle = mysql_result($result,$i,"job_title");
		  $jobDateExpires = mysql_result($result,$i,"job_date_expires");
		  $agentId = mysql_result($result,$i,"agent_id");

		  $query2 = "SELECT agent_name FROM agent WHERE agent_id =\"$agentId\"";
		  $result2 = mysql_query($query2);
		  $agentName = mysql_result($result2,0,"agent_name");  // ERROR MESSAGE


		  echo
			  "<a href=\"?page=ViewListing&jobId=$jobId\"><div class=\"indexListingRow\">
			  <div class=\"indexListing-C1\">$jobDateCreated</div> 
			  <div class=\"indexListing-C2\">$jobTitle</div>
			  <div class=\"indexListing-C3\">$agentName</div>
			  <div class=\"indexListing-C4\">$jobDateExpires</div></div></a>";

		  $i++;
	  }
	  // ==========================================
	  // CLOSE CONNECTION
	  // ==========================================
	  mysql_close($link);
	  ?>

 

Error

 

Warning: mysql_result()  function.mysql-result: Unable to jump to row 0 on MySQL result index 8  in -------------------\Index.view.php on line 47

 

As you can see I manage to get the agent_name for the first row to display, but not the rows after. I'm not sure how to go about doing this.

 

post-8061-1237196155_thumb.png

 

My database structure...

 

agent

agent_id

agent_name

 

job

job_id

job_title

job_description

job_date_created

job_date_expires

agent_id

Well for starters you can do it in one query...

 

SELECT job.*, agent.agent_name FROM job LEFT JOIN agent ON job.agent_id=agent.agent_id ORDER BY job_date_created DESC LIMIT 0, 2

One easy way to get the results is to use mysql_fetch_assoc... how about this:-

 

$sql = "SELECT job.*, agent.agent_name FROM job LEFT JOIN agent ON job.agent_id=agent.agent_id ORDER BY job_date_created DESC LIMIT 0, 2";
if ($result = mysql_query($sql)) {
while ($row = mysql_fetch_assoc($result)) {
	?>
	<a href="whatever">
	<div><?php echo $row['job_date_created'] ?></div>
	</a>
	<?php
}
}

I've left out some of the divs and haven't bothered typing out the link properly in the href, but hopefully you get the idea.

  • Author
I've left out some of the divs and haven't bothered typing out the link properly in the href, but hopefully you get the idea.

 

Thanks Zig...I'm going to mess with this and figure out what your suggestion does...haha..it might take an hour or two. :)

 

edit: for starters...this first time seeing LEFT JOIN in SQL haha

  • Author

OMG I GOT IT ...ahhhhhhhhhhh SOOOO HAPPPPPYY :D

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.