<?php
try {
$nombre=$_GET["nombre"];
$password=$_GET["pass"];
$base=new PDO ("mysql:host=localhost; dbname=te_la_juegas","root", "");
$base->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql="select nombre, pass from usuarios where nombre= :nombre and pass= :pass";
//--EL--POKEMON--
$pokemon="select pokemon from usuarios where nombre= :nombre ";
$resultado=$base->prepare($sql);// PARA COMPROBAR QUE ESTAN EN BASE DATOS
$eleccion=$base->prepare($pokemon);//PARA SABER EL POKEMON
$resultado->bindValue(":nombre",$nombre);
$resultado->bindValue(":pass",$password);// PARA COMPROBAR QUE ESTAN EN BASE DATOS
$eleccion->bindValue(":nombre",$nombre);//PARA SABER EL POKEMON
$resultado->execute();
$eleccion->execute();
//******************************************************************
$numero_registro=$resultado->rowCount();
$inicial=mysql_fetch_row($pokemon);
if ($numero_registro!=0) {
if ($inicial=="charmander") {
# code...
}elseif ($inicial=="vulvasaur") {
# code...
}else {
}
}else {
header("location:index.php");
}
} catch (Exception $e) {
die("Error".$e->getMessage());
}
?>
Before I provide a possible solution I want to point out a few things about your code.
Beware of a misconfigured PDO : For me, PDO is the best option when it comes to querying our database. But if it is not properly configured it could be dangerous. This good configuration implies that when making the connection you set the attribute
PDO::ATTR_EMULATE_PREPARES
tofalse
... otherwise you could be a victim of SQL Injection , as I explain in point 2 of this answer .Using rowCount is not the best option to know if there is data . Although it is the most popular way to try to know the number of rows in a query, rowCount is not the best option for it . The same PHP Manual says the following:
Many people seem not to have read that and continue to use rowCount as a panacea.
In PDO, to know if there is data, you just have to ask for the data itself , since the method that asks for the data returns
false
if there is no data.So if you do something like this:
If you want to count the number of rows anyway, use a
SELECT COUNT(*)
, which will safely and truly return how many rows are in that table .You can see this interesting article posted on PHP Delusions .
Now I'm going to answer your question.
To store the results of the query in a variable, the procedure is as follows:
Note: There are other ways to store the result set, you can consult them in the PHP Manual .
Recommendation
As I said at the beginning, PDO is in my opinion the best option to consult our data.
The job is made considerably easier when we have a class dedicated to handling connections and sending queries securely. If you are interested, you can try this class: PHP-PDO .
FULL CODE EXAMPLE
VER DEMO EN PHPFIDDLE
Result :
The test print_r returns this:
And the table that we generate with the data is this: