Hi,

I'm a bit of a noob with PHP.

I am trying to retrieve information from multiple tables with the following code

<?php
	function check_input($id)
				{
				// Stripslashes
				if (get_magic_quotes_gpc())
				  {
				  $id = stripslashes($id);
					}
				// Quote if not a number
				if (!is_numeric($id))
				  {
				  $id = "'" . mysql_real_escape_string($id) . "'";
				  }
				return $id;
				}
				include 'private/auth.php'; //sql login file
			if (!$con)
  				{
  				die('Could not connect: ' . mysql_error());
				  				}
				mysql_select_db("$database");
				$id=$_GET["id"];
				$id=check_input($id);
				$query="SELECT * FROM products WHERE id=$id";
				
				$result=mysql_query($query);
				while($row = mysql_fetch_array($result))
				{
				echo "<table>";
				echo "<tr><td colspan=2><h3>" . $row['Name'] . " - " . $row['Referance'] . "</h3></td></tr>";
				echo "<tr><td><p>Other Details</p></td><td><p>" . $row['Details'] . "</p></td></tr></table>";
				}

{
					$id2=$row['id'];
				  $query2="SELECT * FROM transactions WHERE ProdId='$id2' limit 10";
					$result2=mysql_query($query2);
					echo "Last 10 Hires:<table>";
					while ($trans = mysql_fetch_array($result2)); 
					{
					$DocketId=$trans['DocketId'];
					$query3="SELECT * FROM dockets WHERE id='$DocketId'";
					$result3=mysql_query($query3);
					while ($DocketInfo = mysql_fetch_array($result3))							
						{
						echo "<tr><td><a href='docket.php?id=" . $DocketId . "'>" . $DocketId . "</a></td>";
						$query4="Select * FROM customers WHERE id='$DocketInfo[CustId]'";
						$result4=mysql_query($query4);
						while ($CustInfo=mysql_fetch_array($result4))
							{
							echo "<tr><td><a href='docket.php?id=" . $DocketInfo['CustId'] . "'>" . $CustInfo['Name'] . "</a></td></tr>";
							}
						}
					}
					echo "</table>";		
					echo $query2;		
					echo $query3;		
					echo $query4;		
				  }

				mysql_close($con);
	?>

My Problem is that all the queries seem to be executed at the same time.
The query in $query2 is using information retrieved from $query, and the query in $query3 is using information from $query2, and the query in $query4 is using information retrieved in $query3, this means $query2, $query3, and $query4 is not working.

Any help would be greatly apreciated, it may be that I am doing this completely wrong!

Thanks in advance!

Recommended Answers

All 2 Replies

On line 36 you appear to have $id2 outside the braces of the first query so i dont think it would have a value. Try moving $id2 to line 32 and make sure all other variables are with the while loop of the query.

one thing you could try is to use an if statement like below, it may help to identify any other breakdowns in the code because it will stop you there.

if ($result=mysql_query($sql)) {
  while ($row=mysql_fetch_assoc($result)) {

Another thing is that you may need to have your where clause variable in this format '".$var."' rather than '$var'

Hi and thanks for your reply, I just got it sorted, the problem was with the semi-colon at the end of line 40, d'oh!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.