Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / mysql

Mac OS下PHP环境搭建及PHP操作MySQL常用方法小结_MySQL

作者:泪﹌无痕   发布日期:2024-08-17   浏览:563

在Mac OS下搭建PHP环境并操作MySQL,可以按照以下步骤进行:

  1. 安装Homebrew:在终端中运行以下命令安装Homebrew:

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  2. 安装PHP:在终端中运行以下命令安装PHP:

    brew install php
  3. 安装MySQL:在终端中运行以下命令安装MySQL:

    brew install mysql
  4. 启动MySQL服务:在终端中运行以下命令启动MySQL服务:

    brew services start mysql
  5. 设置MySQL根密码:在终端中运行以下命令设置MySQL根密码:

    mysql_secure_installation
  6. 连接MySQL:在终端中运行以下命令连接MySQL:

    mysql -u root -p
  7. 创建数据库:在MySQL命令行中运行以下命令创建数据库:

    CREATE DATABASE mydatabase;
  8. 创建表:在MySQL命令行中运行以下命令创建表:

    USE mydatabase;
    CREATE TABLE mytable (
     id INT AUTO_INCREMENT PRIMARY KEY,
     name VARCHAR(100),
     age INT
    );
  9. PHP操作MySQL常用方法:

  • 连接MySQL数据库:
    
    $servername = "localhost";
    $username = "root";
    $password = "your_password";
    $dbname = "mydatabase";

$conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }


- 插入数据:
```php
$sql = "INSERT INTO mytable (name, age) VALUES ('John Doe', 25)";
if ($conn->query($sql) === TRUE) {
    echo "Record inserted successfully";
} else {
    echo "Error inserting record: " . $conn->error;
}
  • 查询数据:
    
    $sql = "SELECT * FROM mytable";
    $result = $conn->query($sql);

if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "Name: " . $row["name"]. " - Age: " . $row["age"]. "
"; } } else { echo "No records found"; }


- 更新数据:
```php
$sql = "UPDATE mytable SET age = 30 WHERE name = 'John Doe'";
if ($conn->query($sql) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $conn->error;
}
  • 删除数据:
    $sql = "DELETE FROM mytable WHERE name = 'John Doe'";
    if ($conn->query($sql) === TRUE) {
      echo "Record deleted successfully";
    } else {
      echo "Error deleting record: " . $conn->error;
    }
  1. 关闭数据库连接:
    $conn->close();

这样就完成了在Mac OS下搭建PHP环境并操作MySQL的过程,并给出了PHP操作MySQL常用方法的小结。

上一篇:实例解析MySQL中的存储过程及存储过程的调用方法_MySQL

下一篇:VS2013连接MySQL5.6成功案例一枚_MySQL

大家都在看

php进入mysql(php进入根目录)

php链接不到mysql(php连接mys

php连接小马(php连接mysql代码)

php与ldap怎么连接(php pdo连

php代码后台增删改查(php对mysql

[PHP从小白到大牛]-022 PHP操作

php搭建mysql连接池

PHP MySQL:连接MySQL数据库

PHP访问MySQL查询超时设置

PHP 原生连接 Mysql

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站