Jump to content

Selecting random withs with a WHERE clause

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
Verk

Verk

    Newbie

  • Members
  • Pip
  • 1 posts
I'm currently trying to code a random pokemon generator. I have an HTML form that gives a person the option to select from 1 to 6 pokemon to generate and then select a pokemon type to restrict the results to (or select "any" and have all types included). I then want to results to be displayed with information like "id" "name" "type" then a picture/icon (will grab from id to fetch image). I already have a database built with all the information I need.

I've been having some trouble, though. I can't get the query to write to an array. I'm not sure where I'm going wrong with my code. Perhaps one of you can figure it out:


$db = new PDO( 'mysql:host=localhost;dbname=fluue_pokemon', "fluue_***removed***", "***removed***" );



	//set limit based on form drop down box

	$limit = $_POST['pokemonlimit'];

	//set pokemon type base on form

	$t = $_POST['t1'];

	switch ($t)  //determines which group to query

{

case 0:

  $type = '%';

  break;

case 1:

  $type = 'grass';

  break;

case 2:

  $type = 'fire';

  break;

case 3:

  $type = 'ground';

  break;

default:

  echo "Error!";

}


$i=1;  //set base value for looping - we want it to loop only the set number of times as in $limit!




do

{

	$i++;

	

	

	

$stmt = $db->prepare( "

    SELECT * 

    FROM pokemon

    WHERE type1 LIKE :type1

" );


var_dump($stmt->execute( array(

    "type1" => $type

)));


$rows = array($stmt);

if( var_dump($rows = $stmt->fetchAll()) )

{

    print_r( $rows[ rand() ] );

}

else

{

    die( "Bugger.\n" );

}




}

while($i<=$limit);



Any help to fixing this would be greatly appreciated. Thanks!

#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
$stmt = $db->prepare( " 

    SELECT *  

    FROM pokemon 

    WHERE type1 LIKE :type1 

" ); 


$stmt->execute(array("type1" => $type)); 

$rows = $stmt->fetchAll();

print_r($rows);