sql help needed!!

Started on the old forum in March 2012 · view the original in the archive · old links redirect here

3 replies · 438 views · started 14 years ago · 1 min read

2 participants
icedroplet1987 Senior · 258 posts #1

$x=$db->select("SELECT message, username, groupid, mstime FROM b_shout ORDER BY mstime DESC LIMIT 0,15");

friends, as i know this line is to select data from database and as limit defined to 15 so it seects 15 rows. but i want to select single row inbetween condition without changing anything in this line. likewise i will like to select only one message, how should i do it? hope you got my point.

antony2kx Developer · 94 posts #2

using your sql statement

do this

$x=$db->select("SELECT message, username, groupid, mstime FROM b_shout ORDER BY mstime DESC LIMIT 0,15");
$messages = array();

while($m=mysql_fetch_object($x))
{

   $messages[] = $m;
}

$random_val = mt_rand(0, count($messages)-1);
$random_message = $messages[$random_val];

echo $random_message->message; //will display a message from the 15 you have selected
icedroplet1987 Senior · 258 posts #3

antony2kx wrote: using your sql statement

do this

$x=$db->select("SELECT message, username, groupid, mstime FROM b_shout ORDER BY mstime DESC LIMIT 0,15");
$messages = array();

while($m=mysql_fetch_object($x))
{

   $messages[] = $m;
}

$random_val = mt_rand(0, count($messages)-1);
$random_message = $messages[$random_val];

echo $random_message->message; //will display a message from the 15 you have selected

getting this error mate..

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource Notice: Undefined offset: 0 Notice: Trying to get property of non-object

antony2kx Developer · 94 posts #4
$x=mysql_query("SELECT message, username, groupid, mstime FROM b_shout ORDER BY mstime DESC LIMIT 0,15");
$messages = array();

while($m=mysql_fetch_object($x))
{

   $messages[] = $m;
}

$random_val = mt_rand(0, count($messages)-1);
$random_message = $messages[$random_val];

echo $random_message->message; //will display a message from the 15 you have selected  

This is because you used the $db->query, your using a class and i dont know if it returns resource or what so i changed it to mysql_query instead

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close