PDA

View Full Version : Get data from 2 tables?



Lilian
29-09-2007, 07:33 PM
Hey,

I am getting a bit lost on how to get information from two different tables.

I have got:


<?
ob_start();
include("config.php");

$a= mysql_query("SELECT * FROM `swnews` ORDER BY `newsid` DESC LIMIT 5") or die(mysql_error());
while ($b = mysql_fetch_array($a)) {
echo "<br />";
echo $b['subject'] . '</a></b><br /><i>';
echo $b['description'] . '</b></i><br />';
echo $b['contents'] . '</b></i><br />';
}

?>

However, I need "contents" to come from the table "swarticles" and not "swnews" when the ID = the subject ID (if you understand that).

Dentafrice,
29-09-2007, 07:37 PM
<?
ob_start();
include("config.php");

$a= mysql_query("SELECT * FROM `swnews` ORDER BY `newsid` DESC LIMIT 5") or die(mysql_error());
while ($b = mysql_fetch_array($a)) {
$c = mysql_query("SELECT * FROM `swarticles` WHERE id='$b[subject]' LIMIT 0,1");
$d = mysql_fetch_array($d);
echo "<br />";
echo $b['subject'] . '</a></b><br /><i>';
echo $b['description'] . '</b></i><br />';
echo $d['contents'] . '</b></i><br />';
}

?>

Lilian
29-09-2007, 07:44 PM
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in news.php on line 8


<?
ob_start();
include("config.php");

$a= mysql_query("SELECT * FROM `swnews` ORDER BY `newsid` DESC LIMIT 5") or die(mysql_error());
while ($b = mysql_fetch_array($a)) {
$c = mysql_query("SELECT * FROM `swnewsdata` WHERE id='$b[subject]' LIMIT 0,1");
$d = mysql_fetch_array($d);
echo "<br />";
echo $b['subject'] . '</a></b><br /><i>';
echo $b['description'] . '</b></i><br />';
echo $d['contents'] . '</b></i><br />';
}

?>

Can't seem to find the problem. All the databases are there :S

Dentafrice,
29-09-2007, 07:47 PM
oops



<?
ob_start();
include("config.php");

$a= mysql_query("SELECT * FROM `swnews` ORDER BY `newsid` DESC LIMIT 5") or die(mysql_error());
while ($b = mysql_fetch_array($a)) {
$c = mysql_query("SELECT * FROM `swnewsdata` WHERE id='$b[subject]' LIMIT 0,1");
$d = mysql_fetch_array($c);
echo "<br />";
echo $b['subject'] . '</a></b><br /><i>';
echo $b['description'] . '</b></i><br />';
echo $d['contents'] . '</b></i><br />';
}

?>

Lilian
29-09-2007, 07:48 PM
Nope =[

Edit:// Got it :) Needed to change id to $b[subject] to $b[newsid].

Would Rep you Denta but need to spread :(

Dentafrice,
29-09-2007, 08:02 PM
ahh in your post I thought you said subject was the id :P

Want to hide these adverts? Register an account for free!