Page 1 of 2
how to query sql.....yet again.....(i hate being a noob to s
Posted: Wed Dec 17, 2003 9:03 pm
by dull1554
i posted eariler asking about aut_increment, now i want to read from the database and pull out five entrys with the highest post_id number, i don't evem know where to start, could someone help me
any help you guys could give me would be awesome......thanks
Posted: Wed Dec 17, 2003 9:16 pm
by Weirdan
Code: Select all
select * from news order by post_id desc limit 5
Posted: Wed Dec 17, 2003 9:38 pm
by dull1554
now how do i display, the contents of the sql query, if i try to print the query i get "Resource id #3" returned
Posted: Wed Dec 17, 2003 10:05 pm
by d3ad1ysp0rk
Code: Select all
$query = mysql_query($sql);
$var = mysql_result($query);
echo $var;
Posted: Wed Dec 17, 2003 11:12 pm
by Paddy
Put
$sql = "select * from news order by post_id desc limit 5";
prior to the above code.
Posted: Thu Dec 18, 2003 5:59 am
by dull1554
when i do that i get this error.....
Code: Select all
Warning: Wrong parameter count for mysql_result() in c:\program files\apache group\apache\htdocs\test\news\news.php on line 5
heres my code
Code: Select all
<?php
include "db.php";
$sql = "select * from news order by post_id desc limit 5";
$query = mysql_query($sql);
$var = mysql_result($query);
echo $var;
?>
Posted: Thu Dec 18, 2003 7:03 am
by qads
you only need to pull out 1 line, use it like this.
Code: Select all
<?php
$news = mysql_fetch_array(mysql_query("selecy ID from news order by ID DESC limit 1"));
echo $news['ID];
?>
Posted: Thu Dec 18, 2003 9:48 am
by dull1554
so if i just pull out one line, but make it loop five times and stick the post_text, subject, and date into a array, then print the array......but i guess that won't work will it because if i make it loop five times its gonna pull out the same line, the last line all five times......it would be good if i could pull out the three fields that i need and slap them into a array, heres how my database is setup
DATABASE : news
TABLE :news
FIELDS :post_id | subject | date | post_text
(post_id auto_increments)
i need to pull out subject, date, and post_text, then i want to place those values in a table like this
Code: Select all
<?php
echo "<table>
<tr><td>".$subject." -- ".$date."</td></tr>
<tr><td>".$post_text."</td></tr>
</table>";
but i dont have the slightest clue as to how to pull the values from the database.....i hope somone can help my poor poor soul....

Posted: Thu Dec 18, 2003 9:58 am
by qads
Code: Select all
<?php
$array = mysql_get_array(mysql_query("`post_id`, `subject`,`date`,`post_text` order by `post_id` limit 1");
echo "<table>
<tr><td>".$array['subject']." -- ".$array['date']."</td></tr>
<tr><td>".$array['post_text']."</td></tr>
</table>";
?>
i dont mean to rude or anything, but you seem to ask the question right away when you have a problem, why dont you spend some time researching it on google or php.net? you will learn it much better cos you would've worked for it.
Posted: Thu Dec 18, 2003 10:05 am
by dull1554
thanks for the help, i understand that, and honnestly most of the time thats what i do, but there are certain cirsumstances that i need to complete a project in a hasty manner, and i do try to do it myself but you prolly know me by now, i tend to run into many errors, or i hit a stumbling block, and have to start yelling "Help.....I've fallen and i can't get up", but i do understand that me asking on this forum is not always the best way in which to learn, and i really appreciate the immense help qads
Posted: Thu Dec 18, 2003 5:03 pm
by dmcglone
how about:
<?php
include "db.php";
$sql = "select * from news order by post_id desc limit 5";
$query = mysql_query($sql);
$var = mysql_result($query);
while($row=mysql_fetch_assoc($var));
echo $var;
?>
Posted: Sat Dec 20, 2003 6:05 pm
by dull1554
ok i've slaped together a bit of each of your guyses code and this is what i have
Code: Select all
<?php
include "db.php";
$query = mysql_query("SELECT * FROM news order by post_id DESC limit 1")
$array = mysql_fetch_array($query);
echo "<table>
<tr><td>".$array['subject']." -- ".$array['date']."</td></tr>
<tr><td>".$array['post_text']."</td></tr>
</table>";
?>
when i try to execute this code i get this error;
Code: Select all
Parse error: parse error, unexpected T_VARIABLE in c:\program files\apache group\apache\htdocs\test\news\news.php on line 4
Posted: Sat Dec 20, 2003 6:26 pm
by qads
Code: Select all
<?php
$query = mysql_query("SELECT * FROM news order by post_id DESC limit 1")
?>
thats all i can say

...lets see if you can find the problem.
Posted: Sat Dec 20, 2003 9:23 pm
by d3ad1ysp0rk
ooOoOo ooo ooo!! i found it!!
how come that isn't a parse error? stupid PHP..

Posted: Sat Dec 20, 2003 9:48 pm
by infolock
i'll give a hint.. it doesn't know that's all you wanted to do with that line
