Update Data In a MySQL Table Using MySQLi and PDO
The UPDATE statement is used to update existing records in a table:
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
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:
1 | John | Doe | 2014-10-22 14:26:15 | |
2 | Mary | Moe | 2014-10-23 10:22:30 |
The following examples update the record with id=2 in the "MyGuests" table:
Example (MySQLi Object-oriented)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
Example (MySQLi Procedural)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";
if (mysqli_query($conn, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
Example (PDO)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";
// Prepare statement
$stmt = $conn->prepare($sql);
// execute the query
$stmt->execute();
// echo a message to say the UPDATE succeeded
echo $stmt->rowCount() . " records UPDATED successfully";
} catch(PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
After the record is updated, the table will look like this:
1 | John | Doe | 2014-10-22 14:26:15 | |
2 | Mary | Doe | 2014-10-23 10:22:30 |
We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.
- Home
- Beginners
Created Mar '14 | Last Reply Nov '14 | Replies 10 | Views 15203 | Votes 0 |
How to check if UPDATE statement was successful??? I can't find it //docs.phalcon.io/en/latest/reference/phql.html
is always returning true
$result = $query->execute(); $result->success()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:
$success = $query->execute(); if ($success != false) { $this->flash->success("ok"); }Try this:
$phql = "UPDATE Cars SET color='red' WHERE id=1"; $result = $this->modelsManager->executeQuery($pqhl); if($result->success() == true) { //do something }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?
$query = $this->modelsManager->createQuery( "UPDATE Users SET email_code = 0, password = :password: WHERE email = :email: AND email_code = :email_code:"); $result = $query->execute(array( 'pass' => $password, 'email' => $email, 'email_code' => $email_code ));
$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)
public function someAction($email) { $query = $this->modelsManager->createQuery( "UPDATE Users SET name = 'NameAfterUpdate' WHERE email = :email:"); $result = $query->execute(array( 'email' => $email )); if ($result->success()==true) { $this->flash->success("ok"); } else { $this->flash->error("error"); } }
$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:
$pdo = new PDO ("mysql:host=localhost;dbname=test","root",""); //Delete all records $pdo->exec("DELETE FROM users"); //SQL is valid but no records to update if (!$pdo->exec("UPDATE users SET name = 'some name'")) { echo "no records to update or an error has occurred"; }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.
$result = $connection->query($updateSql, $params); if ($result->numRows()) { //do something on success }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