Jump to content

Archived

This topic is now archived and is closed to further replies.

Else

Selecting from two db tables in one query?

Recommended Posts

Hello. I have been searching with no prevail about this for weeks. I want to select some info from one table, as well as some different info from another, for use within one array. I say one array because when I used two it seemed to cancel one array out. I\'ve looked into the UNION operator, but it didnt work. Someone told me it was disabled in PHP for security issues or something. The current code for the query is below. If anyone can offer any advice, please do. I really appreciate it!

 


<?php



// MySQL Connection Information

$db_server = "******";        

$db_username = "******";

$db_password = "******";    

$db_name = "******";        



$link = mysql_connect($db_server, $db_username, $db_password) or die(mysql_error()); 

mysql_select_db($db_name, $link);



$sql_topic = mysql_query("SELECT title,  starter_name, starter_id, tid, description FROM ibf_topics WHERE forum_id=\'13\'", $link) or die(mysql_error());



if(mysql_num_rows($sql_topic) > 0)

{

  while($t = mysql_fetch_array($sql_topic))

  {



$sql_attach = mysql_query("SELECT attach_id FROM ibf_posts WHERE topic_id=\'$t[\'tid\']\'", $link) or die(mysql_error());

$i = mysql_fetch_array($sql_attach);



echo "In the actual page all the array vars are outputed here, displaying new article info.";



 }



}

else 

{

  echo "<font color=\'#000000\'><center>No reviews available!<center></font><br>n";

}



mysql_close($link);





?>

The second array returns $i[\'attach_id\'] null (inside the while loop).

Share this post


Link to post
Share on other sites

do the tables have a common field you can join them on? i.e., primary key to foreign.

Share this post


Link to post
Share on other sites

as the previous post indicates you need a common key.

 

with mySQL I believe you want to use JOIN to get data from two tables simultaneously. The Visual Quickstart reference books show examples of this

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.