InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Modifying Data in an SQL Database in PHP
PHP

Modifying Data in an SQL Database in PHP

InfinityCoder December 1, 2016

You want to add, remove, or change data in an SQL database.

Use PDO::exec() to send an INSERT, DELETE, or UPDATE command, as shown in Example 10-14.
Example 10-14. Using PDO::exec()

1
2
3
4
5
$db->exec("INSERT INTO family (id,name) VALUES (1,'Vito')");
 
$db->exec("DELETE FROM family WHERE name LIKE 'Fredo'");
 
$db->exec("UPDATE family SET is_naive = 1 WHERE name LIKE 'Kay'");

You can also prepare a query with PDO::prepare() and execute it with PDOStatement::execute(), as shown in Example 10-15.
Example 10-15. Preparing and executing a query

1
2
3
4
5
6
7
8
$st = $db->prepare('INSERT INTO family (id,name) VALUES (?,?)');
$st->execute(array(1,'Vito'));
 
$st = $db->prepare('DELETE FROM family WHERE name LIKE ?');
$st->execute(array('Fredo'));
 
$st = $db->prepare('UPDATE family SET is_naive = ? WHERE name LIKE ?');
$st->execute(array(1,'Kay'));

The exec() method sends to the database whatever it’s passed. For INSERT, UPDATE, and DELETE queries, it returns the number of rows affected by the query.
The prepare() and execute() methods are especially useful for queries that you want to execute multiple times.

Once you’ve prepared a query, you can execute it with new values without repreparing it. Example 10-16 reuses the same prepared query three times.
Example 10-16. Reusing a prepared statement

1
2
3
4
$st = $db->prepare('DELETE FROM family WHERE name LIKE ?');
$st->execute(array('Fredo'));
$st->execute(array('Sonny'));
$st->execute(array('Luca Brasi'));

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Deleting Cookies in PHP
You want to delete a cookie so a browser doesn’t …

Deleting Cookies in PHP

Keeping Passwords Out of Your Site Files in PHP
You need to use a password to connect to a …

Keeping Passwords Out of Your Site Files in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

    Categories

    • Bash
    • PHP
    • Python
    • Uncategorized

    InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

    About Us

    Start learning your desired programming language with InfinityQuest.com.

    On our website you can access any tutorial that you want with video and code examples.

    We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

    Popular Tags

    binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

    Archives

    • June 2017
    • April 2017
    • February 2017
    • January 2017
    • December 2016
    • November 2016
    Copyright © 2019 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
    Programming Tutorials | Sitemap