bind_result() not binding variables - php

The following code is producing the error: Notice: Undefined variable: morrisons.
I have absolutely no idea why this error is occuring, it seems to be a problem with the actual assigned variable in that it just doesn't get assigned.
$sql = "SELECT name
FROM stds
INNER JOIN `users`
ON users.`id` = stds.`id`
WHERE users.`username` = ?";
if ($stmt = $db->prepare($sql)) {
$stmt->bind_param("s", $_POST['username']);
$stmt->execute();
$stmt->bind_result($morrisons);
$stmt->fetch();
$stmt->close();
}
echo $morrisons;

$stmt->bind_result($morrisons);
This line only executes if this line
if ($stmt = $db->prepare($sql)) {
produces a true result. Otherwise the whole block is skipped and $morrisons never gets declared. So this line
echo $morrisons;
will produce an error. So the root cause is that $db->prepare returns false, which means the query is wrong in some way or the database connection is gone. Check for errors in this case.

Related

Getting fatal error when I use prepared statments for a searching date input

I made this code to display my user's details if I search for their email.
file.php
$sql = "SELECT * FROM users WHERE email='$email'";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $email);
$stmt->execute();
$result2 = $stmt->get_result();
file.html
while ($row = $result2->fetch_assoc()) { //results }
The problem with this code is that I get always a fatal error.
Fatal error: Uncaught Error: Call to a member function fetch_assoc() on null in ____ Stack trace: #0 {main} thrown in ______
Even though my script works perfectly because when I am searching for my users detail it shows them as expected.
What do they mean with this error? Can I get SQL Injected if I stay it like this? How can I remove this error?
1.) Fix binding your email parameter....
$sql = "SELECT * FROM users WHERE email='?'";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $email);
$stmt->execute();
$result2 = $stmt->get_result();
2.) For error you're getting i assume you get some error, you should check for $result2, if it's false > that means error

Call to undefined method mysqli_stmt::free()

I want to prepare a mysql script that first checks the id of the user based on given email and later on use this found id in the next query. What I did so far is as follows:
$find_id = "SELECT id from client
WHERE email = ? ";
$statement = $mysqli->prepare($find_id);
$statement->bind_param("s", $client_mail);
$statement->execute();
$statement->bind_result($id);
$statement->free();
$sql = "SELECT client_name, contact_name from client_addr
WHERE client_id = ? AND is_actual < ? ";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("is", $id, "Y");
$stmt->execute();
$result = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
echo json_encode($result);
but now I'm getting the error related to this line:
$statement->free();
that says Call to undefined method mysqli_stmt::free() in.
However, when I remove this line I'm getting the error:
Uncaught exception 'mysqli_sql_exception' with message 'Commands out of sync; you can't run this command now'
on this line:
$stmt = $mysqli->prepare($sql);
How can I fix it?
I believe the function you're trying to use would be mysqli_stmt::free_result(). You'll need to change this line:
$statement->free();
To:
$statement->free_result();
The second Uncaught exception occurs because mysqli is an unbuffered SQL query handler, so you should store your results if you're looping simultaneous executions with something like $stmt->store_result(); and then you can unload the mysqli buffer to state a new query.

Fatal error: Call to a member function bindParam() on a null

I need some help. I have some nested SELECT statements that get the user's ID then use that ID to search another table in MySQL. I have a foreach() loop that uses the user_id from the first query to create a folder for the user if there isn't one in the filesystem. Then I used bindParam() to assign it a variable and use it in another query to get the user's name. However, it throws and exception saying 'Call to a member function bindParam() on null in C:\foo\bar\foobarscript.php on line 29'. Here's my code up until the break...
try {
$con = new PDO("mysql:host=$dbname;dbname=$db", $user, $pass);
//Set the PDO error mode to exception
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$con->beginTransaction();
$stmt = $con->prepare("SELECT user_id
FROM users");
$stmt->execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
$res = $stmt->fetchAll();
foreach($res as $row) {
$uid = $row['user_id'];
$filename = './reports/'.$uid.'';
if(!file_exists($filename)) {
if(!mkdir('./reports/'.$uid)) {
die('Failed to create folders..');
}
}
$stmt2->bindParam(':uid', $uid); //<--- Code breaks here
$stmt2 = $con->prepare("SELECT CONCAT(fname,' ',lname) as fullname FROM users WHERE user_id = :uid");
$stmt2->execute();
$fullName = $stmt2->fetchAll();
$userArray[] = array_fill_keys($uid, $fullName);
$stmt2->closeCursor();
}
I have searched up and down and tried rewriting it, debugging piece by piece. Everything works fine until I put it all back together and I get this error again. I really appreciate any help!
Edit: I have even tried removing the assigned variable and binding like:
$stmt2->bindParam(':uid', $row['user_id']);

Simple select query gives error

Does anyone have any idea why I get two errors for such a simple query? Error message are:
Warning: mysqli::prepare(): Couldn't fetch mysqli in (...)/functions.php on line 503
Fatal error: Call to a member function bind_param() on null in (...)functions.php on line 504
$query_select = ("SELECT * FROM vat WHERE vat_status = ?");
$stmt = $mysqli->prepare($query_select); // line 503
$stmt->bind_param("s", $vat_status);
$stmt->execute();
$stmt->store_result();
$count = $stmt->num_rows();
$stmt->bind_result ($vat_id ,
$vat_rate ,
$vat_account ,
$vat_description ,
$vat_status ,
$vat_timestamp );
The problem was that I was triggering an UPDATE statement and then an SELECT statement, and both of them used $stmt variable. That was what went wrong.
Now I use
mysqli_stmt_close($stmt);
Which really closes the $stmt and frees the result, so I can trigger after an update, the select statement.

bind_param() error when making nested mysqli call

When trying to bind params i get the error: "Call to a member function bind_param() on a non-object in ....." These queries run fine on separate scripts but when i want to run them nested i keep getting the same error. I've tried a lot to find out how to fix it but could not.
$mysqli = new mysqli('localhost', 'user', 'pass', 'db');
$ignore_time=time()-(24*60*60);
$stmt=$mysqli->prepare("select id,new_count from view_gallery where lastUpdate< ? and new_count >0");
$stmt->bind_param('i',$ignore_time);
$stmt->execute();
$stmt->bind_result($id, $count);
while($stmt->fetch())
{
$stmt_new=$mysqli->prepare("update gallery set power=power + ? where id= ?");
$power=$count* 0.01;
$stmt_new->bind_param('di',$power,$id);
$stmt_new -> execute();
}
The error is thrown in the bind_param line inside the loop.
The error is because your prepare() call is not returning a valid object. This is accounted to the fact that your SQL query is wrong.
In your query, you have power which is a reserved function keyword in MySQL.
Escape it using backticks in the query and your code will work.
$stmt_new = $mysqli->prepare( "UPDATE gallery SET `power` = `power` + ? WHERE `id` = ?" );
Using backticks is always a good practice.
the solution was that the results of the outer loop were to be saved:
$stmt->store_result();
right above the while loop.

Categories