MYSQLi bind_result is returning null - php

I am trying to output the variables that I get from the database in my query but nothing is being returned. Using MYSQLi prepared statements.
Please see code below:
$stmt = $con->prepare("SELECT first_name, last_name FROM transactions WHERE order_id = ?");
$stmt->bind_param('i', $order_id);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($first_name, $last_name);
$stmt->close();
// Output review live to page
echo $first_name;
Where am I going wrong?

You forgot the line to fetch the result. fetch().
Try that:
$stmt->bind_result($first_name, $last_name);
$stmt->fetch(); // ----- > you forget that line to fetch results.
$stmt->close();

Related

PHP returning wrong row in mysqli prepared statment

I'm trying to create vanity URL's for my website and as of now there is only one username in the database that can be referenced. My query will return the profile with the username "callmeoddie" no matter what letters are entered. The idea here is for the user to be able to type website.com/username to access the user's profile. I know that my htaccess is setup correctly and is working. This is a PHP issue.
$stmt = $db->prepare("SELECT `FirstName,`LastName`,`Username`,`RandNum` FROM `Users` WHERE `Username`=?");
$stmt->bind_param("i", $UserIdent);
$stmt->execute();
$stmt->store_result();
$num_of_rows = $stmt->num_rows;
$stmt->bind_result($FirstName, $LastName, $Username, $RandNum);
$stmt->fetch();
$stmt->close();
Solved my own problem.
The $stmt->bind_param("i", $UserIdent); line was expecting a string, not an integer.
The code is chanaged to $stmt->bind_param("s", $UserIdent);

PHP, MySQL statement results in ZERO rows

hope someone can help me.
i have a very simple prepared SELECT statment in PHP:
$query_select = ("SELECT * FROM companies where user_name = ? ");
$stmt = $mysqli->prepare($query_select);
$stmt->bind_param("s", $user_name);
$stmt->execute();
$count = $stmt->num_rows;
in companies table I have several rows with the $user_name i`m trying to query. But i still get 0 rows as a result.
The strange thing is that the non PREPARED version works:
$query = 'SELECT * FROM companies WHERE user_name="'.$user_name.'"';
$result = $mysqli->query($query);
$count= $result->num_rows;
echo "Aantal: ".$count;
So my question is, does anyone know why the prepared version returns ZERO and the non prepared version returns the correct number of rows?
Add this line to your code between execute and num_rows statement.
$stmt->store_result();
You have to store it before counting it.
For mysqli prepared statements, you must take an additional step: storing the result.
Try this:
$query_select = ("SELECT * FROM companies where user_name = ? ");
$stmt = $mysqli->prepare($query_select);
$stmt->bind_param("s", $user_name);
$stmt->execute();
$stmt->store_result(); // <-- new line
$count = $stmt->num_rows;
May be you need to bind the result:
/* bind result variables */
$stmt->bind_result($district);
Full example here

Getting aggregate function result from php stmt?

I am attempting to retrieve the latest id from the DB and it works in my terminal but will not work as a prepared statement.
Here is my code.It all works up to it, inserting and pulling a id if it is not through the max function.
$stmt=$mysqli->prepare("select id from pickup where id in (select max(id) from pickup)");
$stmt->bind_result($id);
$stmt->fetch();
$stmt->close();
$mysqli->close();
return $id;
check
$stmt=$mysqli->prepare("select max(id) as id from pickup");
$stmt->execute();
$stmt->bind_result($id);
$stmt->fetch();
$ret_id = $id;
$stmt->close();
$mysqli->close();
return $ret_id;

MySQLI STMT Num_rows

I've been having a problem with the prepared statements in MySQLI OOP. The problem is that I don't know how to use the num_rows() method correct.
If I use var_dump to see the result of the variables I will get NULL because there is no data.
$stringMail = trim($_POST['mail']);
/*
.....
*/
$stmt = $mysqli->prepare("SELECT email FROM users WHERE email = ? ");
$stmt->bind_param('s', $stringMail);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($usersMail);
$iaMail = $stmt->num_rows;
$stmt->close();
Sorry for the bad English, I'm Dutch and my English isn't the very best... Thanks for helping!
You don't need it. As you have $usersMail already.

"Commands out of sync;" Prepared Stored Procedure Error

I seem to be having some trouble with running a 2nd prepared query after running a 1st prepared stored procedure. I understand this is because I can't buffer results in mySQL, but I'm not sure exactly I should be using to get all the results.
I've tried:
$stmt->store_result();
$stmt->fetch();
$stmt->bind_result($var1, $var2);
$stmt->free_result();
But I still get the same error. The procedure works fine, and is designed to only return 1 row, so not sure.
The full error is:
Commands out of sync; you can't run this command now
UPDATE:
More relevant code:
$stmt = $mysqli->prepare('CALL createNewListing(?,?,?,?,?,?)');
$time = time();
$stmt->bind_param('sdssii', $listing['title'], $listing['price'], $_SESSION['user']['id'], $listing['description'], $time, $listing['featured']);
$stmt->execute();
$stmt->store_result();
$stmt->fetch();
$stmt->bind_result($listing['primarypic']['id'], $listing['id']);
$stmt->free_result();
$stmt->close();
// Error is generated here.
$statement = $mysqli->prepare('INSERT INTO pictures (listid, primarypic) VALUES (?, ?)');
if (!$statement) {
echo $mysqli->error;
die();
}

Categories