how to query sql.....yet again.....(i hate being a noob to s

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

how to query sql.....yet again.....(i hate being a noob to s

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

select * from news order by post_id desc limit 5
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post 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
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Code: Select all

$query = mysql_query($sql);
$var = mysql_result($query);
echo $var;
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post by Paddy »

Put

$sql = "select * from news order by post_id desc limit 5";

prior to the above code.
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post 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;
?>
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post 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];
?>
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post 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....:)
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post 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.
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post 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
dmcglone
Forum Newbie
Posts: 20
Joined: Sun Sep 28, 2003 7:54 pm
Location: Columbus, Ohio

Post 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;
?>
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post 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
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

Code: Select all

<?php
$query = mysql_query("SELECT * FROM news order by post_id DESC limit 1") 
?>
thats all i can say :twisted:...lets see if you can find the problem.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

ooOoOo ooo ooo!! i found it!! :D


how come that isn't a parse error? stupid PHP.. :(
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

i'll give a hint.. it doesn't know that's all you wanted to do with that line ;)
Post Reply