Jump to content

how can i construct this call to my database


jimmi8

Recommended Posts

Hi,

I have this problem that i dont know how to work out as im still very new to php/sql. I have a script that queries my database and pulls out information about a blog entry ( title, body text, author, date submitted). All this info is generated from one table ( and yes the author column is creating redundant data but ill sort that out soon)

I have another table in my db thats for categories. My first query gets the category id fine and assigns it to a variable ($cat). Now the problem question i have is how to incorportae the query ive made below with the join. Can i include it somehow in the query above and do both at once or what? ive never made a call to two databases in the same instance before.

Could anyone suggest a good way to do this? As the code is now all thats getting printed out( in green) is resource1d#4???!?!?!

[code]
<ul>



<?php
require_once ('mysql_connect.php'); // Connect to the db.

$sql = "SELECT * FROM entries ORDER BY date_submitted DESC LIMIT 5";


$query = mysql_query($sql);
echo $query;

while ($row = mysql_fetch_array($query)) {
  $title = $row['title'];
$body = $row['body'];
$author = $row['author_id'];
$date = $row['date_submitted'];
$cat = $row['category_id'];


?>





<li><?php echo $title; echo $date; echo $body; ?></li>
<p style="red"><?php echo $author; ?></p>
<p style="color: red;"><?php echo $cat; ?></p>

<?php
}
?>


<?php

$sql = "SELECT category FROM categories Where categories.category_id = $cat";
$result = mysql_query($sql);

?>

</ul>

<p style="color: green;"><?php echo $result; ?>
</p>


[/code]
Hi orio,

Thanks for the reply. I tires the code you gave but it didnt work. i got this error:

Warning: mysql_result() [function.mysql-result]: category_ID not found in Mysql result index 5 in.....my doc root...on line 41.

I also need the query to kinda be in the the the above while loop somehow because im spitting out 5 articles so i would need the category query to be tied in so that i got a result for each article

Any ideas?!?

Archived

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

×
×
  • Create New...

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.