Property access is not allowed yet in - php

I have one simple function for delete account from my database. I have written it like below
public function removeAccount($email) {
$response = array('code' => 0, 'error' => false);
$stmt = $this->conn->prepare("SELECT id FROM user WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows) {
$user = $result->fetch_assoc();
$id = $user['id'];
$stmt->close();
$stmt = $this->conn->prepare("DELETE FROM number_list WHERE user_id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->close();
$stmt = $this->conn->prepare("DELETE FROM number_status WHERE user_id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->close();
$stmt = $this->conn->prepare("INSERT INTO old_user(email,serial,premium) SELECT email, device_id, membership FROM user WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->close();
$stmt = $this->conn->prepare("DELETE FROM user WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->close();
if ($stmt->affected_rows) {
$response["code"] = 1;
}
}
return $response;
}
Its giving me warning in below line
if ($stmt->affected_rows) {
I have searched way for solve it but does not getting idea whats wrong and what can fix it. Please check and let me know if someone can have idea about it. Thanks a lot.

$stmt = $this->conn->prepare("DELETE FROM user WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
if($stmt->affected_rows > 0) { $response["code"] = 1; }
$stmt->close();
In this case, we checked to see if any rows got updated. For reference, here's the usage for mysqli::$affected_rows return values.
-1 - query returned an error; redundant if there is already error handling for execute()
0 - no records updated on UPDATE, no rows matched the WHERE clause or no query has been executed
Greater than 0 - returns number of rows affected; comparable to mysqli_result::$num_rows for SELECT

You're trying to get the number of affected rows from a closed statement.
Instead of
$stmt->execute();
$stmt->close();
if ($stmt->affected_rows) {
$response["code"] = 1;
}
Use
$stmt->execute();
$num_affected_rows = $stmt->affected_rows;
$stmt->close();
if ($num_affected_rows) {
$response["code"] = 1;
}

There are many reasons for this error, but the one I had today was one I have not found documented anywhere.
I had two (2) copies of same virtual machine running and they were both competing in some way that I do not understand well enough to explain, but going into VirtualBox and shutting one of them off solved the problem.
I know this an obscure scenario, but if anyone else runs across the same I hope my answer stops them from wasting time on it like I did.

Related

Mysqli bind_param error

I have a issue with my prepared statement, I have searched through stack for the answer and tried many off them them to no avail, I have check the insert statement which is correct, I have also tried using the statement as a regular statement which seems to work,
The error I get is as below
Fatal error:call to function bind_param()on a non object in ** on line 23
When I use a normal query on the first query the second does not show the error
The code is
public function reg_user($name,$username,$password,$email){
$password = password_hash($password);
$stmt = $this->db->prepare("SELECT uid FROM users WHERE uname= ? OR uemail = ?");
$stmt->bind_param("ss", $username,$email);
$stmt->execute();
$count_row = $stmt->num_rows;
if($count_row == 0){
$stmt = $this->db->prepare("INSERT INTO users (uname,upass,fullname,uemail) VALUES(?,?,?,?)");
$stmt->bind_param("ssss", $username,$password,$name,$email); //line 23
$return = $stmt->execute();
return $return;
} else{
return false;
}
}
Add these two lines of code after the first $stmt->execute
$result = $stmt->get_result();
$count_row = $result->num_rows;
The reason why your code fails is because you are trying to get the number of rows from a Prepared Statement object instead of a Result

How to fetch data from database using prepare statement in php?

I have a database in which I have user_id & associated_id.There can be multiple associated_id for a single user_id. Now I want to fetch all the associated_ids into a single array. I have tried this method but don't know how to get them in array.
$stmt = $this->conn->prepare("SELECT * FROM my_contacts WHERE user_id = ?");
$stmt->bind_param("s", $user_id);
if ($stmt->execute())
{
while ($stmt->fetch())
{
//what to write here
}
//echo var_dump($user);
$stmt->close();
}
Try this:
$stmt = $mysqli->prepare("SELECT associated_id FROM my_contacts WHERE user_id = ?")) {
$stmt->bind_param('s', $user_id); // Bind "$user_id" to parameter.
$stmt->execute(); // Execute the prepared query.
$stmt->store_result();
// get variables from result.
$stmt->bind_result($associated_id);
$stmt->fetch();
The results will be stored in the $associated_id array.
You can bind parameters like this and use fetchall method to get all the results in a array
$stmt = $this->conn->prepare("SELECT * FROM my_contacts WHERE user_id = :user_id");
$stmt->bind_param(":user_id", $user_id, PDO::PARAM_INT);
if ($stmt->execute())
{
$result = $stmt->fetchall(PDO::FETCH_ASSOC);
//echo var_dump($user);
$stmt->close();
}
According to your code you used mysqli.
$stmt = $this->conn->prepare("SELECT * FROM my_contacts WHERE user_id = ?");
if($stmt->execute()){
$result = $stmt->get_result();
if($result->nom_rows > 0){
while($row = $result->fetch_assoc()){
var_dump($row)
}
}else{
echo "Sorry NO data found";
}
}else{
echo "Some thing is wrong";
}
here you can't used $stmt->bind_result(); instead of use $stmt->get_result()
$stmt->bind_result(); is only used when you define field in select query
with * you need to used $stmt->get_result()
refer this link for more information
Example of how to use bind_result vs get_result

Processing SQL query result in PHP, then inserting again within same function

I'm trying to make a function work, where I ask for a number of free seats left in a car, and if executed, it subtracts 1 from the result and updates the cell in database.
Sql statements must be correct... am I missing out on anything else?
function seatCalc($id){
$stmt=$this->connection->prepare("SELECT seats_left FROM drive WHERE id=?");
$stmt->bind_param("i", $id);
$stmt->bind_result($seat_count);
$stmt->execute();
return $seat_count;
if($seat_count >= 1){
$seat_count -= 1;
$stmt=$this->connection->prepare("UPDATE drive SET seats_left=? WHERE id=?");
$stmt->bind_param("ii", $seat_count, $id);
$stmt->execute();
}
}
It's a part of my university project and unfortunately, I can't reach my professor at the moment.
Thank you in advance!
Your return statement breaks you out of the function before the if block gets processed.
your condition is incorrect;
you have unreached statements after return;
Here is code:
function seatCalc($id){
$stmt=$this->connection->prepare("SELECT seats_left FROM drive WHERE id=?");
$stmt->bind_param("i", $id);
$stmt->bind_result($seat_count);
$stmt->execute();
if($seat_count > 0){
$stmt=$this->connection->prepare("UPDATE drive SET seats_left=seats_left-1 WHERE id=?");
$stmt->bind_param("i", $id);
$stmt->execute();
}
return $seat_count;
}
Also you can substract one with database..
Oh boy
function seatCalc($id){
$stmt=$this->connection->prepare("SELECT seats_left FROM drive WHERE id=:i");
$stmt->bind_param(":i", $id);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$seat_count = intval($row['seats_left']);
if($seat_count > 0){
$seat_count -= 1;
$stmt=$this->connection->prepare("UPDATE drive SET seats_left=seats_left-1 WHERE id=:id");
$stmt->bind_param(":id", $id);
$stmt->execute();
}
return $seat_count;
}

Noobie issue. PHP else if statement

I'm hitting my head to a wall... This is a fraction of my code:
$stmt = $db->prepare("SELECT vote,id FROM votes WHERE post_id = ? AND user_id = ? ");
$stmt->bind_param('ii', $post_id, $user_id);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($result, $vote_id);
$stmt->fetch();
$num_rows = $stmt->num_rows;
$stmt->close();
if ($num_rows>=1)
{
if ($result!==$vote)
{
$stmt = $db->prepare("UPDATE votes SET vote = ? WHERE id=?");
$stmt->bind_param('ii', $vote, $vote_id);
$stmt->execute();
$stmt->close();
}
else if ($result==$vote)
{
$stmt = $db->prepare("UPDATE votes SET vote = 0 WHERE id=?");
$stmt->bind_param('i', $vote_id);
$stmt->execute();
$stmt->close();
}
}
No errors are shown, the script works just fine until it reaches ELSE IF part. It doesn't update although $vote value is the same as in a database.
!== is a strict comparison operator, and this statement is saying that $result must not be identical (in type as well as value) to $vote. You don't need to be that specific for this.
if ($result!==$vote)
What you should be doing is this:
if ($result != $vote) // only one equal sign, not two
{
...
} else { // they are equal implicitly, no need to redefine the ==
...
}

Problem with mysqli fetch

I'm having a mysqli data fetching problem. I will try to explain my problem by example:
I want to fetch entries (by different persons), from a table. Now I want to look for each of the fetched person's name in another table and see if he has any photo.
My code is given below, however its not working, I'm getting following errors:
mysqli::prepare() [mysqli.prepare]: All data must be fetched before a new statement prepare takes place
Call to a member function fetch() on a non-object in ...
My Code:
if ($stmt = $this->mysqli->prepare("SELECT entry, author, time FROM messages WHERE user = ?")) {
$stmt->bind_param("s", $user_name);
$stmt->execute();
$stmt->bind_result($entry, $author, $time);
while ($stmt->fetch()) {
if ($stmt = $this->mysqli->prepare("SELECT photo_id FROM photos WHERE user = ?")) {
$stmt->bind_param("s", $author);
$stmt->execute();
$stmt->bind_result($photo_id);
}
//echo $photo_id;
}
$stmt->close();
}
I'll be very thankful for any help.
Assign the second statement to new variable so it wouldn't override the first variable and cause the "all data must be fetched.." error.
if ($stmt = $this->mysqli->prepare("SELECT entry, author, time FROM messages WHERE user = ?")) {
$stmt->bind_param("s", $user_name);
$stmt->execute();
$stmt->bind_result($entry, $author, $time);
while ($stmt->fetch()) {
if ($st = $this->mysqli->prepare("SELECT photo_id FROM photos WHERE user = ?")) {
$st->bind_param("s", $author);
$st->execute();
$st->bind_result($photo_id);
}
//echo $photo_id;
$st->close();
}
$stmt->close();
}

Categories