PHP Prepared statement returning wrong value - php

I am trying to find out if a user exists in my database already, but no matter what I try, I always get the equivalent of a false.
$stmt = $connection->prepare("SELECT * FROM members WHERE member_username = ?");
$stmt->bind_param("s", $username);
$stmt->execute();
if($stmt->num_rows > 0) {
return 'user exists';
}
I know this has got to be something very simple... something I am clearly overlooking, but I always get that num_rows = 0, even though the user is in the database already.
Can someone please help?

Call get_result() or store_result() before call num_rows:
$stmt = $connection->prepare("SELECT * FROM members WHERE member_username = ?");
$stmt->bind_param("s", $username);
$stmt->execute();
$res = $stmt->get_result(); //<--- HERE
if($res->num_rows > 0) { //<--- HERE
return 'user exists';
}
OR
$stmt->execute();
$stmt->store_result(); //<--- HERE
if($stmt->num_rows > 0) {

Related

num_rows not working pdo,mysqli, always return 0

I have a interesting problem.
// in pdo with function --> not work
function UserIsExist($name)
{
global $db;
$stmt = $db->prepare("SELECT id FROM tarskereso_users WHERE email = '$name' LIMIT 1");
$stmt->execute();
if ($stmt->fetchColumn() == 1) return 1;
else return 0;
}
// with MySQLi --> not working
function UserIsExist($name)
{
global $db;
$stmt = $db->prepare("SELECT id,email FROM tarskereso_users WHERE email = ? LIMIT 1");
$stmt->bind_param('s', $name);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows == 1)
return 1;
else
return 0;
$stmt->close();
}
// In Register.php
... other ..
if(UserIsExist($user) == 1)
$error_msg = "Is Exist";
else
{
$birthdate = $year.'.'.$month.'.'.$day;
CreateUser($user,$pass,$birthdate,$sex);
$error_msg = 'Success';
}
So, with function not working, I try with:
$stmt = $db->prepare("SELECT id,email FROM tarskereso_users WHERE email = ? LIMIT 1");
$stmt->bind_param('s', $name);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0)
... other ...
else
echo 'isnt exist...';
$stmt->close();
but not working, the num_rows always return 0. And the account in the database successfuly created
in pdo, num_rows won't work.
you have to use $sql->rowCount() method to get number of records in a table.
<?php
$sql = $con->prepare("<YOUR SQL QUERY HERE>");
$sql->execute();
if($sql->rowCount() > 0){
echo $sql->rowCount() ." rows found";
}
?>

Why is fetch_assoc returning false?

$stmt = $mysqli->prepare("SELECT username, email, password, code FROM temp_users WHERE code = ?");
$stmt->bind_param('s', $code);
$stmt->execute();
$stmt->store_result();
//if SELECT statement returns 1, grab data.
if ($stmt->num_rows === 1) {
echo "Got Row";
$result = $stmt->get_result();
var_dump($result);
while ($row = $result->fetch_assoc()) {
$username = $row['username'];
$email = $row['email'];
$password = $row['password'];
}
This is really weird, the query must be going through because the script is echoing "Got Row", and I have no errors up to that point. But when I try to use $result->fetch_assoc() I get an error,and $result is spitting out false, so why is that? Please excuse how dumb this question may seem, I'm still learning how to use mysqli. :)
Your question is neither dumb nor weird. You are simply confused by store_result() and get_result().
Both of these functions fetch the whole record set from the database. Once the data is fetched you can't fetch it again. Therefore, you can't use both of these functions at the same time!
We can fix your code in two ways.
With store_result():
$stmt = $mysqli->prepare("SELECT username, email, password, code FROM temp_users WHERE code = ?");
$stmt->bind_param('s', $code);
$stmt->execute();
$stmt->store_result();
//if SELECT statement returns 1, grab data.
if ($stmt->num_rows === 1) {
echo "Got Row";
$stmt->bind_result($username, $email, $password);
while ($stmt->fetch()) {
// use the data here
var_dump($username);
}
}
with get_result():
$stmt = $mysqli->prepare("SELECT username, email, password, code FROM temp_users WHERE code = ?");
$stmt->bind_param('s', $code);
$stmt->execute();
$result = $stmt->get_result();
//if SELECT statement returns 1, grab data.
if ($result->num_rows === 1) { // <--- !!! We are using the result object here
echo "Got Row";
foreach ($result as $row) {
$username = $row['username'];
$email = $row['email'];
$password = $row['password'];
}
}

mySQLi Debugging Issues

I actually have this PHP code, which i use to obtain my database info..
function searchUserEmail($username, $raw_email){
//Prepare Statements
$query = "SELECT * FROM users WHERE username = ?";
$query2 = "SELECT * FROM users WHERE email = ?";
//Sanitize Input
$user = $this->conn->real_escape_string($username);
$email = $this->conn->real_escape_string($raw_email);
if($stmt = $this->conn->prepare($query)){
$stmt->bind_param('s', $user);
$stmt->execute();
if($stmt->num_rows > 0){
$stmt->close();
return true;
}
}
if($stmt = $this->conn->prepare($query2)){
$stmt->bind_param('s', $email);
$stmt->execute();
if($stmt->num_rows > 0){
$stmt->close();
return true;
}
}
return false;
}
I've tried this alot of times, ran it through a statement checker, still it doesn't work. Is there anything i'm missing???
Somehow it always returns a false (Even though its suppose to return true)
mysql_select_db($dataname,$conn);

Prepared statement error. Newbie issue

I can't figure out what is wrong with these few lines:
if ( $stmt = $db->prepare('SELECT * FROM users WHERE username = ? LIMIT 1');
$stmt->bind_param('s', $username);
$stmt->execute();
$stmt->store_result();
$stmt->fetch();
$stmt->num_rows == 1;
)
{
$errors[] = "Username is taken.";
}
if($stmt = $db->prepare('SELECT * FROM users WHERE username = ? LIMIT 1'))
{
$stmt->bind_param('s', $username);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows > 0)
{
$errors[] = "Username $username is taken.";
}
$stmt->free_result();
$stmt->close();
}
Not sure how you got your other code, but I think you want something like this:
$stmt = $db->prepare('SELECT * FROM users WHERE username = ? LIMIT 1');
$stmt->bind_param('s', $username);
$stmt->execute();
$stmt->bind_result($res);
if($stmt->num_rows == 1)
{
$errors[] = "Username is taken.";
}
There's an unnecessary fetch in your original code, also

How to return the results from a Mysqli prepared statement

Im studying Mysqli at the moment.
But i have a problem about how to return a resultset from a mysqli prepared statement.
I have the following code in Loginclass.php:
$stmt = mysqli_stmt_init($db->getCon());
$stmt->prepare('SELECT * FROM `user` WHERE `user_username` = ? AND `user_password` = ?');
$user_username = $this->_user_name;
$user_password = $this->_password;
$stmt->bind_param('ss', $user_username, $user_password);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows() == 0) {
return false;
}
else {
$this->setPass(true);
return $result;
}
Now i want in login.php to do the following with the returning $result var:
while ($row = $result->fetch_assoc()) {
$_SESSION['login']['user_id'] = $row['user_id'];
}
It's all because of $db->getCon() method name :)
Looks like you don't understand logic of operators - read manual.
Try this code:
//$stmt is object, which can be returned by prepare method
$stmt = $db->prepare('SELECT * FROM `user` WHERE `user_username` = ? AND `user_password` = ?');
$user_username = $this->_user_name;
$user_password = $this->_password;
$stmt->bind_param('ss', $user_username, $user_password);
$stmt->execute();
$stmt->bind_result($result); //instead of $stmt->store_result();
$stmt->fetch();
if (empty($result))
{
return false;
}
else
{
$this->setPass(true);
return $result;
}

Categories