Picking a random row in my sql

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 1756 Views - Last Post: 20 March 2010 - 07:28 AM Rate Topic: -----

#16 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2881
  • View blog
  • Posts: 9,954
  • Joined: 08-August 08

Re: Picking a random row in my sql

Posted 20 March 2010 - 06:42 AM

Oh, it helps to use the semicolon on each line:
<?php
        
mysql_connect("localhost", "_", "");
mysql_select_db("_");

$qry = mysql_query("SELECT * FROM pokemon
WHERE pokemon_map='1'");
$max = mysql_num_rows($qry) - 1; // i.e.: 5 rows would be 0,1,2,3,4
$random_number = mt_rand(0, $max);
$result = mysql_query("SELECT * FROM pokemon
WHERE pokemon_map='1' LIMIT ".$random_number.", 1");

mysql_close($con); // unnecessary since connections are closed when the script ends.

?> 


Was This Post Helpful? 0
  • +
  • -

#17 nick1200  Icon User is offline

  • Php Coder
  • member icon

Reputation: -19
  • View blog
  • Posts: 922
  • Joined: 21-March 09

Re: Picking a random row in my sql

Posted 20 March 2010 - 06:45 AM

View PostCTphpnwb, on 20 March 2010 - 05:42 AM, said:

Oh, it helps to use the semicolon on each line:
<?php
        
mysql_connect("localhost", "_", "");
mysql_select_db("_");

$qry = mysql_query("SELECT * FROM pokemon
WHERE pokemon_map='1'");
$max = mysql_num_rows($qry) - 1; // i.e.: 5 rows would be 0,1,2,3,4
$random_number = mt_rand(0, $max);
$result = mysql_query("SELECT * FROM pokemon
WHERE pokemon_map='1' LIMIT ".$random_number.", 1");

mysql_close($con); // unnecessary since connections are closed when the script ends.

?> 




now the page is loading but not showing no results wouldn't it be better to use the error thing in my sql ?
Was This Post Helpful? 0
  • +
  • -

#18 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2881
  • View blog
  • Posts: 9,954
  • Joined: 08-August 08

Re: Picking a random row in my sql

Posted 20 March 2010 - 06:48 AM

Now you need to output the results. If you var_dump($resultarray) you should see a random array each time you run it.
<?php
        
mysql_connect("localhost", "_", "");
mysql_select_db("_");

$qry = mysql_query("SELECT * FROM pokemon
WHERE pokemon_map='1'");
$max = mysql_num_rows($qry) - 1; // i.e.: 5 rows would be 0,1,2,3,4
$random_number = mt_rand(0, $max);
$result = mysql_query("SELECT * FROM pokemon
WHERE pokemon_map='1' LIMIT ".$random_number.", 1");

$resultarray = mysql_fetch_array($result);
echo "<br>";
var_dump($resultarray);
echo "<br>";

mysql_close($con); // unnecessary since connections are closed when the script ends.

?> 


Was This Post Helpful? 0
  • +
  • -

#19 nick1200  Icon User is offline

  • Php Coder
  • member icon

Reputation: -19
  • View blog
  • Posts: 922
  • Joined: 21-March 09

Re: Picking a random row in my sql

Posted 20 March 2010 - 06:53 AM

View PostCTphpnwb, on 20 March 2010 - 05:48 AM, said:

Now you need to output the results. If you var_dump($resultarray) you should see a random array each time you run it.
<?php
        
mysql_connect("localhost", "_", "");
mysql_select_db("_");

$qry = mysql_query("SELECT * FROM pokemon
WHERE pokemon_map='1'");
$max = mysql_num_rows($qry) - 1; // i.e.: 5 rows would be 0,1,2,3,4
$random_number = mt_rand(0, $max);
$result = mysql_query("SELECT * FROM pokemon
WHERE pokemon_map='1' LIMIT ".$random_number.", 1");

$resultarray = mysql_fetch_array($result);
echo "<br>";
var_dump($resultarray);
echo "<br>";

mysql_close($con); // unnecessary since connections are closed when the script ends.

?> 




now im getting

array(8) { [0]=> string(4) "nick" ["pokemon_name"]=> string(4) "nick" [1]=> string(4) "fire" ["pokemon_type"]=> string(4) "fire" [2]=> string(1) "1" ["pokemon_map"]=> string(1) "1" [3]=> string(49) "http://www.pokemontoxic.net/pokemon/Bulbasaur.png" ["pokemon_pic"]=> string(49) "http://www.pokemontoxic.net/pokemon/Bulbasaur.png" }


on the page lol


just wanna show the image pokemon_pic
"http://www.pokemontoxic.net/pokemon/Bulbasaur.png"

the pokemon_name and pokemon_type
Was This Post Helpful? 0
  • +
  • -

#20 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2881
  • View blog
  • Posts: 9,954
  • Joined: 08-August 08

Re: Picking a random row in my sql

Posted 20 March 2010 - 07:01 AM

So then it looks like you want to use $resultarray['pokemon_pic'] in an image tag.
Was This Post Helpful? 0
  • +
  • -

#21 nick1200  Icon User is offline

  • Php Coder
  • member icon

Reputation: -19
  • View blog
  • Posts: 922
  • Joined: 21-March 09

Re: Picking a random row in my sql

Posted 20 March 2010 - 07:04 AM

View PostCTphpnwb, on 20 March 2010 - 06:01 AM, said:

So then it looks like you want to use $resultarray['pokemon_pic'] in an image tag.


and id get rid of the dump ?

var_dump($resultarray);



then just eco out the name and type and image ?

how many can this go up two ? cus at the moment i have two things in that table and its working for 2 but don't wanna be adding like 50 and it stops


i have this now and its showing a broke image





$qry = mysql_query("SELECT * FROM pokemon
WHERE pokemon_map='1'");
$max = mysql_num_rows($qry) - 1; // i.e.: 5 rows would be 0,1,2,3,4
$random_number = mt_rand(0, $max);
$result = mysql_query("SELECT * FROM pokemon
WHERE pokemon_map='1' LIMIT ".$random_number.", 1");

$resultarray = mysql_fetch_array($result);
echo "<br>";
var_dump($resultarray);
echo "<br>";

mysql_close($con); // unnecessary since connections are closed when the script ends.


?> 

   <img src="http://<?php echo $resultarray['pokemon_pic'] ?>" width="90" height="80" />


This post has been edited by nick1200: 20 March 2010 - 07:06 AM

Was This Post Helpful? 0
  • +
  • -

#22 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2881
  • View blog
  • Posts: 9,954
  • Joined: 08-August 08

Re: Picking a random row in my sql

Posted 20 March 2010 - 07:08 AM

Yes, get rid of the dump. That's just there for debugging. The point is that this should be much faster than using rand() in mysql for very large tables with millions of records. With only 50 it would be about the same speed either way.

Oh, and I wouldn't break out of php just to output a tag:

   <img src="http://<?php echo $resultarray['pokemon_pic'] ?>" width="90" height="80" />


would be:

   echo '<img src="'.$resultarray['pokemon_pic'].'" width="90" height="80" />';


This post has been edited by CTphpnwb: 20 March 2010 - 07:11 AM

Was This Post Helpful? 1
  • +
  • -

#23 nick1200  Icon User is offline

  • Php Coder
  • member icon

Reputation: -19
  • View blog
  • Posts: 922
  • Joined: 21-March 09

Re: Picking a random row in my sql

Posted 20 March 2010 - 07:11 AM

Thanks made it work i have a button on the page now this mite be off the subject but is there a way to make the button refresh the page ?


<form name="input" action="submit.php" method="get">
<input type="submit" value="Submit" />
</form> 




cus ive got it showing the Pokemon now but want it so when they click the button a new 1 pops up

then id have to make another button to do more sql after to add it to there box but at the moment just need to refresh the page

This post has been edited by nick1200: 20 March 2010 - 07:16 AM

Was This Post Helpful? 0
  • +
  • -

#24 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2881
  • View blog
  • Posts: 9,954
  • Joined: 08-August 08

Re: Picking a random row in my sql

Posted 20 March 2010 - 07:18 AM

This is ugly and confusing:
?> 
<?php echo $resultarray['pokemon_name'] ?>
<?php echo $resultarray['pokemon_type'] ?>
   <img src="http://<?php echo $resultarray['pokemon_pic'] ?>" width="90" height="80" />


You break out of php only to break back in!
Do:

echo $resultarray['pokemon_name'];
echo $resultarray['pokemon_type'];
echo '<img src="'.$resultarray['pokemon_pic'].'" width="90" height="80" />';


or even better:
echo $resultarray['pokemon_name'].resultarray['pokemon_type'].'<img src="'.$resultarray['pokemon_pic'].'" width="90" height="80" />';



View Postnick1200, on 20 March 2010 - 09:11 AM, said:

Thanks made it work i have a button on the page now this mite be off the subject but is there a way to make the button refresh the page ?


<form name="input" action="submit.php" method="get">
<input type="submit" value="Submit" />
</form> 




cus ive got it showing the Pokemon now but want it so when they click the button a new 1 pops up

then id have to make another button to do more sql after to add it to there box but at the moment just need to refresh the page

You can do that with Javascript. Post that question there.
Was This Post Helpful? 0
  • +
  • -

#25 nick1200  Icon User is offline

  • Php Coder
  • member icon

Reputation: -19
  • View blog
  • Posts: 922
  • Joined: 21-March 09

Re: Picking a random row in my sql

Posted 20 March 2010 - 07:19 AM

View PostCTphpnwb, on 20 March 2010 - 06:18 AM, said:

This is ugly and confusing:
?> 
<?php echo $resultarray['pokemon_name'] ?>
<?php echo $resultarray['pokemon_type'] ?>
   <img src="http://<?php echo $resultarray['pokemon_pic'] ?>" width="90" height="80" />


You break out of php only to break back in!
Do:

echo $resultarray['pokemon_name'];
echo $resultarray['pokemon_type'];
echo '<img src="'.$resultarray['pokemon_pic'].'" width="90" height="80" />';


or even better:
echo $resultarray['pokemon_name'].resultarray['pokemon_type'].'<img src="'.$resultarray['pokemon_pic'].'" width="90" height="80" />';



View Postnick1200, on 20 March 2010 - 09:11 AM, said:

Thanks made it work i have a button on the page now this mite be off the subject but is there a way to make the button refresh the page ?


<form name="input" action="submit.php" method="get">
<input type="submit" value="Submit" />
</form> 




cus ive got it showing the Pokemon now but want it so when they click the button a new 1 pops up

then id have to make another button to do more sql after to add it to there box but at the moment just need to refresh the page

You can do that with Javascript. Post that question there.



ok thanks again ill click the thanks button now :bananaman:
Was This Post Helpful? 0
  • +
  • -

#26 nick1200  Icon User is offline

  • Php Coder
  • member icon

Reputation: -19
  • View blog
  • Posts: 922
  • Joined: 21-March 09

Re: Picking a random row in my sql

Posted 20 March 2010 - 07:28 AM

d.w i found it lol

This post has been edited by nick1200: 20 March 2010 - 07:35 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2