Jump to content

Select Random id from table to link


starvinmarvin14

Recommended Posts

I'm trying to select a random id from a table called 'image'. I want the id number to be at the end of a link such as view.php?id=123. Here is my code:

 

$query=mysql_query("SELECT id FROM image WHERE active='yes' ORDER BY RAND() LIMIT 1");

 

echo <<<OPT

<meta http-equiv="REFRESH" content="0;url=http://www.mysite.com/view.php?id={$query}">

OPT;

 

It does not display the number in the link. What have i done wrong?

Link to comment
Share on other sites

$query=mysql_query("SELECT id FROM image WHERE active='yes' ORDER BY RAND() LIMIT 1");

$query_result = mysql_result($query,0);

 

echo <<<OPT

<meta http-equiv="REFRESH" content="0;url=http://www.mysite.com/view.php?id={$query_result}">

OPT;

 

Tried that and got this...

 

"Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/.../public_html/.../random.php on line 9"

Link to comment
Share on other sites

it seems like your query is not returning any rows, try to do this

 

if(mysql_num_rows($query)){
$query_result = mysql_result($query,0);
echo <<<OPT
   <meta http-equiv="REFRESH" content="0;url=http://www.mysite.com/view.php?id={$query_result}">
OPT;
}else{
echo 'no records found';
}

Link to comment
Share on other sites

Tried this...

 

$query = mysql_query("SELECT id FROM image WHERE active='yes' ORDER BY RAND() LIMIT 1");

if(mysql_num_rows($query)){

$query_result = mysql_result($query,0);

 

 

echo <<<OPT

<meta http-equiv="REFRESH" content="0;url=http://www.mysite.com/view.php?id={$query_result}">

OPT;

}else{

echo 'no records found';

}

 

 

Now I am getting this error...

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/.../public_html/.../random.php on line 9

no records found

 

I only have to rows at the moment with the id 18 and 19. How would i use mysqli_error()?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.