php 事务, mysqli_fetch_all(),mysql 一张表的多个字段关联另外一张表
<?php $dbhost = 'localhost:3306'; // mysql服务器主机地址 $dbname = 'test'; $dbuser = 'root'; // mysql用户名 $dbpass = 'root'; // mysql用户名密码 $conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname); if(! $conn ) { die('连接失败: ' . mysqli_error($conn)); } // 设置编码,防止中文乱码 mysqli_query($conn, "set names utf8"); mysqli_select_db( $conn, 'RUNOOB' ); mysqli_query($conn, "SET AUTOCOMMIT=0"); // 设置为不自动提交,因为MYSQL默认立即执行 mysqli_begin_transaction($conn); // 开始事务定义 if(!mysqli_query($conn, "insert into callback (name) values(8)") || !mysqli_query($conn, "insert into callback (id) values(9)")) { mysqli_query($conn, "ROLLBACK"); // 判断当执行失败时回滚 } /*if() { mysqli_query($conn, "ROLLBACK"); // 判断执行失败时回滚 }*/ mysqli_commit($conn); //执行事务 mysqli_close($conn); ?>
mysqli_fetch_all()
<?php // 假定数据库用户名:root,密码:123456,数据库:RUNOOB $con=mysqli_connect("localhost","root","123456","RUNOOB"); if (mysqli_connect_errno($con)) { echo "连接 MySQL 失败: " . mysqli_connect_error(); } $sql="SELECT name,url FROM websites ORDER BY alexa"; $result=mysqli_query($con,$sql); // 获取数据 mysqli_fetch_all($result,MYSQLI_ASSOC); // 释放结果集 mysqli_free_result($result); mysqli_close($con); ?>
定义和用法
mysqli_fetch_all() 函数从结果集中取得所有行作为关联数组,或数字数组,或二者兼有。
注释:该函数只在带有 MySQL Native Driver 时可用。
mysqli_fetch_all($result,MYSQLI_ASSOC);//获取所有数据的关联数组
转自:https://www.cnblogs.com/furuihua/p/11190191.html
mysql 一张表的多个字段关联另外一张表:
SELECT vtiger_orderitem.orderid, ( SELECT vtiger_users.last_name FROM vtiger_users WHERE vtiger_orderitem.signatory = vtiger_users.id ) AS signatory, ( SELECT vtiger_users.last_name FROM vtiger_users WHERE vtiger_orderitem.servicer = vtiger_users.id ) AS servicer FROM vtiger_orderitem SELECT n.orderid, m.last_name, q.last_name FROM vtiger_orderitem n LEFT JOIN vtiger_users m ON n.signatory = m.id LEFT JOIN vtiger_users q ON n.servicer = q.id
转自:https://www.cnblogs.com/anxiaoyu/p/8951797.html