Update Data In a MySQL Table Using MySQLi and PDOThe UPDATE statement is used to update existing records in a table: Show UPDATE table_name Notice the WHERE clause in the UPDATE syntax: The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated! To learn more about SQL, please visit our SQL tutorial. Let's look at the "MyGuests" table:
The following examples update the record with id=2 in the "MyGuests" table: Example (MySQLi Object-oriented)<?php // Create connection $sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2"; if ($conn->query($sql) === TRUE) { $conn->close(); Example (MySQLi Procedural)<?php // Create connection $sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2"; if (mysqli_query($conn, $sql)) { mysqli_close($conn); Example (PDO)<?php try { $sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2"; // Prepare statement // execute the query // echo a message to say the UPDATE succeeded $conn = null; After the record is updated, the table will look like this:
We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.
How to check if UPDATE statement was successful??? I can't find it https://docs.phalcon.io/en/latest/reference/phql.html
is always returning true
is always returning "1" that suppose to be a successful update, check in your database that records are correctly updated, or query again the record and try to create a fail by providing wrong values for the fields to see what is the $success in that case maybe this will work:
Try this:
The problem is that $result->success() is always == 1 and $success is always != false even when UPDATE is not successful When does the update is not successful for you?
$email and $email_code are (parameters) from url. $result->success() is always ==1 even when UPDATE really take place and when parameters are wrong. Why it is not successful? It's successful if the validators defined on the model Users do not reject the values used as parameters. So query which is not changing (updating) anything is successful too????(wrong parameters)
$result->success() always return true. I've got Phalcon\Mvc\Model\Validator\Email validation in model Users and even when $email = 'aaaaaaaaaa' $result->success()==true So, according to your expected behavior this must work:
I am looking for an answer to a similar issue. Is there no way to get the affected rows using ORM/PHQL on an UPDATE/DELETE? Do I have to resort to Raw SQL to get the affected rows? I am using query method for UDPATE/DELETE using Raw SQL. Phalcon docs suggests using execute method for UPDATE/DELETE. The following works.
Resulting object belongs to \Phalcon\Db\Result\Pdo. I second the problems described above. It would be great if the PDO statements themselves would indicate whether a query is succesful or not, by means of a BOOLEAN. E.g. this is my code: $del_cust_recipe = $this->connection->delete('custom_recipes','owner_id=:owner_id AND recipe_id=:recipe_id',array('owner_id'=>$this->user_id,'recipe_id'=>$recipe_id)); $del_cust_recipe always returns true here, even though no rows were deleted.... I have similar issue with update and delete query How check PDO query is successful in PHP?To determine if the PDO::exec method failed (returned FALSE or 0), use the === operator to strictly test the returned value against FALSE. To execute an SQL statement that returns one or more result sets, call the PDO::query method on the PDO connection object, passing in a string that contains the SQL statement.
How check mysql update query was successful in PHP?Use if(mysqli_affected_rows($mysqli) >0 ) or no comparison at all. Sidenote: ==1 is only comparing for 1, as opposed to >0 which you may be trying to update more than one row. However and on the rare occasion, >0 is required where this has also happened to me before; that is the reason of my answer.
How to update in PDO PHP?In order to run an UPDATE query with PDO just follow the steps below:. create a correct SQL UPDATE statement.. replace all actual values with placeholders.. prepare the resulting query.. execute the statement, sending all the actual values to execute() in the form of array.. How to check row count in PDO?PDOStatement::rowCount() returns the number of rows affected by a DELETE, INSERT, or UPDATE statement. print("Return number of rows that were deleted:\n"); $count = $del->rowCount();
|