php mysql 工具方法
在PHP中,可以使用MySQLi和PDO这两个扩展来连接和操作MySQL数据库。
1. 使用MySQLi扩展:
连接数据库:
```php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}
```
执行查询语句:
```php
$sql = "SELECT * FROM table";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        // 处理每一行数据
    }
} else {
    echo "0 结果";
}
```
执行插入、更新、删除语句:
```php
$sql = "INSERT INTO table (column1, column2, column3) VALUES ('value1', 'value2', 'value3')";
if ($conn->query($sql) === TRUE) {
    echo "插入成功";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
$sql = "UPDATE table SET column1='new value' WHERE condition";
if ($conn->query($sql) === TRUE) {
    echo "更新成功";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
$sql = "DELETE FROM table WHERE condition";
if ($conn->query($sql) === TRUE) {
    echo "删除成功";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
```
关闭数据库连接:
```php
$conn->close();
```
2. 使用PDO扩展:
连接数据库:
```php
sql中update什么意思
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "连接成功";
} catch(PDOException $e) {
    echo "连接失败: " . $e->getMessage();
}
```
执行查询语句:
```php
$sql = "SELECT * FROM table";
$result = $conn->query($sql);
if ($result->rowCount() > 0) {
    foreach($result as $row) {
        // 处理每一行数据
    }
} else {
    echo "0 结果";
}
```
执行插入、更新、删除语句:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。