In PHP applications, database management is crucial. Integration with MySQL database is one of the skills that PHP developers should master. MySQL is a popular open source database management system for which PHP provides extensive support. PHP programmers can use MySQL functions to integrate with the MySQL database to implement operations such as data insertion, query, update and deletion.
In this article, we will take a deep dive into how to use MySQL functions in PHP. We will cover the following topics:
Connecting to a MySQL database in PHP is easy. You can use built-in mysqli functions or PDO classes.
When using the mysqli function, you need to use the following command to create a MySQL connection:
$conn = mysqli_connect($servername, $username, $password, $dbname);
Among them:
: MySQL server name
: MySQL username
: MySQL user password
: The name of the database to be connected to
$conn. If the connection fails, FALSE will be returned.
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
: MySQL server name
: MySQL username
: MySQL user password
: The name of the database to be connected to
$conn. If the connection fails, a PDOException will be thrown.
$sql = "SELECT id, name, age FROM users"; $result = mysqli_query($conn, $sql);
$result in variables. You can use the following code to display the results:
while($row = mysqli_fetch_assoc($result)) { echo "ID: " . $row["id"] . " Name: " . $row["name"] . " Age: " . $row["age"]; }
$sql = "SELECT id, name, age FROM users"; $result = $conn->query($sql);
$result variable. You can use the following code to display the results:
foreach($result as $row) { echo "ID: " . $row["id"] . " Name: " . $row["name"] . " Age: " . $row["age"]; }
$sql = "INSERT INTO users (name, age) VALUES ('John Doe', 25)"; mysqli_query($conn, $sql);
table, where name
is John Doe
and age
is 25
. Use the PDO class to insert data
$sql = "INSERT INTO users (name, age) VALUES ('John Doe', 25)"; $conn->exec($sql);
This code will insert a record into the
users table, wherename
is John Doe
and age
is 25
. Update data
$sql = "UPDATE users SET age = 26 WHERE name = 'John Doe'"; mysqli_query($conn, $sql);
This code will update the
name in theusers table
The age
field for the record John Doe
is 26
. Using the PDO class to update data
$sql = "UPDATE users SET age = 26 WHERE name = 'John Doe'"; $conn->exec($sql);
This code will update the
name in the users
table The age
field of the record for John Doe
is 26
. Delete data
$sql = "DELETE FROM users WHERE name = 'John Doe'"; mysqli_query($conn, $sql);
This code will delete the
name in theusers table
Records for John Doe
. Use the PDO class to delete data
$sql = "DELETE FROM users WHERE name = 'John Doe'"; $conn->exec($sql);
This code will delete the
name in the users
table Records for John Doe
. Conclusion
By reading this article, you should already know how to use MySQL functions in PHP. In practice, you can use them to create powerful database applications.
The above is the detailed content of How to use MySQL functions in PHP. For more information, please follow other related articles on the PHP Chinese website!