首页 > 数据库 >PHP执行sql

PHP执行sql

时间:2024-05-31 21:44:25浏览次数:23  
标签:PHP lastname sql echo connect error 执行 conn

PHP连接MySQL

连接数据库

<?php
$severname='localhost';
$username='root';
$password='123456';
$dbanme='数据库名'

//创建连接
$conn = new mysqli($servername, $username,$password, $dbname);

//连接检测
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
} else {
    echo "连接成功";
    $conn->close();
}

PHP执行sql

规定由 mysqli_query()、mysqli_store_result() 或 mysqli_use_result() 返回的结果集标识符
mysqli_query() 函数执行某个针对数据库的查询
创建数据库
<?php
$servername='localhost';
$username='root';
$password='root';

//创建连接
$conn = new mysqli($servername,$username,$password);

//检测连接
if ($conn->connect_error)
{
    die('连接失败:'.$conn->connect_error);
}else{
    echo '连接成功!';
}

//创建数据库
$sql="create database myDB";
if($conn->query($sql) === TRUE){
    echo '数据库创建成功';
}else{
    echo '数据库创建失败:'.$conn->error;
}

$conn->close();
<?php
$servername = "localhost";
$username = "username";
$password = "password";
 
// 创建连接
$conn = mysqli_connect($servername, $username, $password);
// 检测连接
if(!$conn){
    die("连接失败: " . mysqli_connect_error());
}
 
// 创建数据库
$sql = "CREATE DATABASE myDB";
if(mysqli_query($conn,$$sql)){
	echo "数据库创建成功";
} else {
    echo "Error creating database: " . mysqli_error($conn);
mysqli_close($conn);
}
创建表
  • DEFAULT value - 设置默认值
  • UNSIGNED - 使用无符号数值类型,0 及正数
  • PRIMARY KEY - 设置数据表中每条记录的唯一标识。 通常列的 PRIMARY KEY 设置为 ID 数值,与 AUTO_INCREMENT 一起使用。
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "myDB";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}
//sql语句
$sql="create table users(
id int(6) unsigned auto_increment primary key,
firstname varchar(30) not null,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";

if ($conn->query($sql)===TRUE){
	echo "表创建成功!";
} else {
    echo "创建数据表错误: " . $conn->error;
}

$conn->close();
插入数据
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "myDB";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}
//sql语句
$sql = "INSERT INTO users (firstname, lastname, email)
VALUES ('John', 'Doe', '[email protected]')";

if ($conn->query($sql)===TRUE){
	echo "表创建成功!";
} else {
    echo "创建数据表错误: " . $conn->error;
}

$conn->close();
插入多条数据
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "myDB";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}
//sql语句
$sql = "INSERT INTO users (firstname, lastname, email)
VALUES ('John', 'Doe', '[email protected]');";
$sql .= "INSERT INTO users (firstname, lastname, email)
VALUES ('Mary', 'Moe', '[email protected]');";
$sql .= "INSERT INTO users (firstname, lastname, email)
VALUES ('Julie', 'Dooley', '[email protected]')";

if ($conn->multi_query($sql)===TRUE){
    echo "表创建成功!";
} else {
    echo "创建数据表错误: " . $conn->error;
}

$conn->close();

image-20240531181119863

预处理语句

预处理:预留的值使用参数 "?" 标记

INSERT INTO MyGuests (firstname, lastname, email) VALUES(?, ?, ?)

?的使用:可将问号替换为整型,字符串,双精度浮点型和布尔值

$stmt->bind_param("sss", $firstname, $lastname, $email);

bind_param() 函数:

该函数绑定了 SQL 的参数,且告诉数据库参数的值。 "sss" 参数列处理其余参数的数据类型。s 字符告诉数据库该参数为字符串

<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "myDB";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

//预处理及绑定
$stmt=$conn->prepare("insert into users (firstname,lastname,email) values (?,?,?)");
$stmt->bind_param('sss',$firstanme,$lastname,$email);

$firstname='John';
$lastname='Doe';
$email='[email protected]';
$stmt->execute();

$firstname = "Mary";
$lastname = "Moe";
$email = "[email protected]";
$stmt->execute();

$firstname = "Julie";
$lastname = "Dooley";
$email = "[email protected]";
$stmt->execute();

echo "新记录插入成功";

$stmt->close();
$conn->close();
读取数据
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "myDB";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

//读取数据
$sql = "SELECT id, firstname, lastname FROM users";//读取三个字段,将其结果赋值给$result
$result = $conn->query($sql);

if ($result->num_rows > 0) {//函数 num_rows() 判断返回的数据
    // 输出数据
    while($row = $result->fetch_assoc()) {//如果返回的是多条数据,函数 fetch_assoc() 将结合集放入到关联数组并循环输出
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
} else {
    echo "0 结果";
}
$conn->close();
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "myDB";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

//读取数据
$sql = "SELECT id, firstname, lastname FROM users";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // 输出数据
    while($row = mysqli_fetch_assoc($result)) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
} else {
    echo "0 结果";
}

mysqli_close($conn);
where子句
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "myDB";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

//读取数据
$sql = "SELECT * from users where firstanem=John";
$result = mysqli_query($conn, $sql);

while($row = mysqli_fetch_array($result))
{
    echo $row['FirstName'] . " " . $row['LastName'];
    echo "<br>";
}
$conn->close();
order by
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "myDB";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

//读取数据
$sql = "SELECT * from users order by age";
$result = mysqli_query($conn, $sql);

while($row = mysqli_fetch_array($result))
{
    echo $row['FirstName'];
    echo " " . $row['LastName'];
    echo " " . $row['Age'];
    echo "<br>";
}
$conn->close();
update
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "myDB";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

//读取数据
$sql = "update users set age=18 where firstname=’john‘ and lastname=’doe‘";
$result = mysqli_query($conn, $sql);

$conn->close();
Delete
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "myDB";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

//读取数据
$sql = "delete from users where lastname='doe'";
$result = mysqli_query($conn, $sql);

$conn->close();

标签:PHP,lastname,sql,echo,connect,error,执行,conn
From: https://www.cnblogs.com/Yolololo/p/18225318

相关文章

  • 成为MySQL DBA后,再看ORACLE数据库(四、系统视图)
    在数据库的运维管理中,熟悉数据库系统视图的使用可以帮助我们了解数据库各方面的状态信息。一般数据库的系统视图分为数据字典视图和动态性能视图,数据字典视图用于显示数据库的元数据信息和系统状态信息包括各种数据库对象、用户、角色、权限等;动态性能视图用于显示数据库的性能统......
  • MySQL基础索引知识【索引创建删除 | MyISAM & InnoDB引擎原理认识】
      博客主页:花果山~程序猿-CSDN博客文章分栏:MySQL之旅_花果山~程序猿的博客-CSDN博客关注我一起学习,一起进步,一起探索编程的无限可能吧!让我们一起努力,一起成长!目录 一,索引用处二,磁盘三,mysql与磁盘的基本交互单位四,管理page的数据结构(InnoDB引擎下)单个page多个pa......
  • PHP执行sql
    PHP连接MySQL连接数据库<?php$severname='localhost';$username='root';$password='123456';$dbanme='数据库名'//创建连接$conn=newmysqli($servername,$username,$password,$dbname);//连接检测if($conn->connect_error){......
  • MyBatis实战:如何将拼接的SQL打印到日志
    哈喽,大家好,我是木头左!一、前言在日常开发中,经常会遇到拼接SQL的情况,这时候,如何将拼接的SQL打印到日志,以便追踪和调试呢?本文将详细介绍MyBatis如何实现这一功能。二、MyBatis简介MyBatis是一个优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射。MyBatis避免了几乎所有......
  • 免费、开源、好用的 SQL 客户端合集
    0、SQLChatSQLChat是2023年3月推出的新型SQL客户端,它将数据库管理带入了基于聊天的新时代。SQLChat由ChatGPT驱动,能够帮你编写和润色SQL语句,让数据库操作变得更加智能和便捷。目前支持MySQL、PostgreSQL和MSSQL数据库。官方地址:https://www.sqlchat.ai......
  • 【SQL DB - 关系型数据库是如何工作的】
    SQLDB-关系型数据库是如何工作的从数据结构说起时间复杂度归并排序二叉搜索树B+树索引哈希表全局概览核心组件工具查询管理器数据管理器:数据查询的流程客户端管理器查询管理器查询解析器查询重写器统计查询优化器索引存取路径联接运算符嵌套循环联接哈希联接合并联接......
  • MYSQL数据库
    创建数据库createdatabasemyDB;删除数据库dropdatabasefirstDB;选择数据库usemyDB;创建表mysql>createtablePEOPLE(->IDintAUTO_INCREMENTPRIMARYKEY,->NAMEvarchar(20)notnull,->PASSWORDtinyint(50)notnull,->AGEintno......
  • 立即执行函数IFEE
    //立即执行函数IIFE//声明functionname(a,b){returna+b;}//调用console.log(name(1,2));//立即执行将声明与执行二合一,一步到位//函数声明外部使用一个圆括号括住,使之转为表达式(function(a,b){console.log(a+b);......
  • sqlSugar 使用原生模式链接数据库
    usingSystem.Reflection;usingzhulongxu_webapi_pro.Tools;namespacezhulongxu_webapi_pro.Services{///<summary>///初始化数据库///</summary>publicstaticclassInitDataBaseService{publicstaticvoidInitDataBase......
  • mysql查询表基础信息
    --一、查询数据库名称为db_name的所有表 SELECTt.table_catalog,t.table_schema,t.table_name,table_typeFROMinformation_schema.TABLEStwheret.table_schema='db_name'SELECTt.table_catalog,t.table_schema,t.table_name,table_typeFROMinformation_schema.TABLES......