[PHP] Resource ID problem
Ok, so i'm making a news system for school.
Uhm everything works like a smoothy (login, post message to db and such)
But the selecting of the newest message fucks up! (So it only selects the newest message, the newest message has the highest id because it auto_increases..)
So this is my code:
Code:
<?php
include("config.php");
$query="SELECT TOP 1 id FROM news ORDER BY id DESC";
$id=mysql_query($query);
$title="SELECT subject FROM news WHERE id='$id'";
$titleout=mysql_query($title);
$message="SELECT content FROM news WHERE id='$id'";
$messageout=mysql_query($message);
$author="SELECT author FROM news WHERE id='$id'";
$authorout=mysql_query($author);
?>
<div class="nieuws">
<h3><?php echo $titleout ?></h3><br />
Door: <?php echo $authorout ?>
<p />
Bericht: <br /><?php echo $messageout ?>
It's outputting Resource IDs..
So any help would be greatly apprecieated ^^>
Thanks!
Re: [PHP] Resource ID problem
PHP Code:
<?php
include("config.php");
$query = mysql_query("SELECT id, subject, content, author FROM news WHERE id='$id' ORDER BY id DESC");
$post = mysql_fetch_assoc($query);
echo "<div class='nieuws'>
<h3>".$post['subject']."</h3><br />
Door:".$post['author']."
<p />
Bericht: <br />".$post['content'];
?>
Hope this fixes your mistakes;)
Re: [PHP] Resource ID problem
PHP Code:
<?php
include("config.php");
$query = mysql_query("SELECT * FROM news WHERE id='$id' ORDER BY id DESC LIMIT 1");
$post = mysql_fetch_array($query);
echo "<div class='nieuws'>
<h3>".$post['subject']."</h3><br />
Door:".$post['author']."
<p />
Bericht: <br />".$post['content'];
?>
He needed LAST ID.
Not all id's.
Re: [PHP] Resource ID problem
Quote:
Originally Posted by
sayuta
PHP Code:
<?php
include("config.php");
$query = mysql_query("SELECT * FROM news WHERE id='$id' ORDER BY id DESC LIMIT 1");
$post = mysql_fetch_array($query);
echo "<div class='nieuws'>
<h3>".$post['subject']."</h3><br />
Door:".$post['author']."
<p />
Bericht: <br />".$post['content'];
?>
He needed LAST ID.
Not all id's.
Yea sorry missed that, thanks for noticing it;D
Re: [PHP] Resource ID problem
mysql_query is only to execute the query, nothing more.
Re: [PHP] Resource ID problem
the code you posted makes me wonder how everything else is coded and works...
Re: [PHP] Resource ID problem
Quote:
Originally Posted by
holthelper
the code you posted makes me wonder how everything else is coded and works...
I was wondering the same^^
Re: [PHP] Resource ID problem
Huh? all of those queries have "where id=$id..", um, that's not what the OP actually wants. Well maybe it is, but there's a much better way than stacking up a bunch of queries.
The correct query (SINGLE query) would be,
PHP Code:
$last_post_query = mysql_query('SELECT `author`,`subject`,`content` FROM `news` ORDER BY `id` DESC LIMIT 1');
$last_post = mysql_fetch_assoc($last_post_query);
echo '<div class="nieuws">
<h3>'.$last_post['subject'].'</h3><br />
Door:'.$last_post['author'].'
Bericht: <br />'.$last_post['content'].'</div>';
No WHERE clause is needed. OP wants just three columns within a single row from the db- and ordering from last-to-first.
Re: [PHP] Resource ID problem
add another column in MySQL withes auto makes the date and time of creation.
Let your script select the most recent date.
Maby little out handed but it can make the the system go little smoother ;)
I will have a look at this if I can help you out I will post it else I won't post within the next week in this thread :)
Just give me some time and I would love to see the other parts are coded this gives me a better look into your system.
But for now I will do it with what u gave us.