starvinmarvin14 Posted December 14, 2011 Share Posted December 14, 2011 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? Quote Link to comment Share on other sites More sharing options...
marcelobm Posted December 14, 2011 Share Posted December 14, 2011 try this $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; Quote Link to comment Share on other sites More sharing options...
starvinmarvin14 Posted December 14, 2011 Author Share Posted December 14, 2011 $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" Quote Link to comment Share on other sites More sharing options...
marcelobm Posted December 14, 2011 Share Posted December 14, 2011 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'; } Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 14, 2011 Share Posted December 14, 2011 No, the query is completely failing. Use mysqli_error to get some insight as to why that's happening. Quote Link to comment Share on other sites More sharing options...
starvinmarvin14 Posted December 14, 2011 Author Share Posted December 14, 2011 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()? Quote Link to comment Share on other sites More sharing options...
mikosiko Posted December 14, 2011 Share Posted December 14, 2011 for your code sure Pikachu2000 means to say mysql_error() instead of mysqli_error() Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 14, 2011 Share Posted December 14, 2011 Yes, you're right . . . I thought I saw an i in there, but I guess not. Time for new glasses, I suppose. Quote Link to comment Share on other sites More sharing options...
starvinmarvin14 Posted December 14, 2011 Author Share Posted December 14, 2011 $row = mysql_query("SELECT * FROM image ORDER BY RAND() LIMIT 1;"); $id = $row['id']; echo <<<OPT <meta http-equiv="REFRESH" content="0;url=http://www.mysite.com/view.php?id={$id}"> OPT; echo $row When i echo the row I get "Resource id #2" every time. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 14, 2011 Share Posted December 14, 2011 $row holds the result resource returned by mysql_query. You'll need to use one of the mysql_fetch_* functions, such as mysql_fetch_assoc to extract the data from the result resource. Quote Link to comment Share on other sites More sharing options...
starvinmarvin14 Posted December 14, 2011 Author Share Posted December 14, 2011 Thanks this worked! $row = mysql_fetch_array(mysql_query("SELECT id FROM image ORDER BY RAND() LIMIT 1")); $id = $row['id']; echo <<<OPT <meta http-equiv="REFRESH" content="0;url=http://www.mysite.com/view.php?id={$id}"> OPT; Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 14, 2011 Share Posted December 14, 2011 It may work, but it's a really bad way to do it with all the functions nested like that. It makes debugging that part of the code quite difficult. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.