Mysql
 sql >> Database >  >> RDS >> Mysql

Ottenere il numero di righe e dati di quella riga in una singola query utilizzando PDO in PHP

spero di averti capito bene.

provalo con Scarica tutto

$rows = $query->fetchAll(PDO::FETCH_ASSOC);

nel tuo caso

$sql='select uid,name from users where username=? and password=?';
$query=$con->prepare($sql);
$result=$query->execute(array($username,$password));


// check if a single row is returned
// i guess to avoid the warnings you can just set the array befor like
// $row = array();
// or put @ befor the VARS in the TRUE statement

$rows = $query->fetchAll(PDO::FETCH_ASSOC));

/** 
if you have more than one result you can look at them like this

foreach ($rows as $row)
{
    echo $row['uid'];
}
**/

if((count($rows)===1)
    {
        echo $_SESSION['id'][email protected]$rows['uid']; 
        echo $_SESSION['name'][email protected]$rows['name'];
        // header('Location: lading_page.php');
    }
else echo "Invalid username/password";