Home > Backend Development > PHP Tutorial > PHP预处理语句执行过程中不知道什么问题,求大神解答,分数不多,下面是代码

PHP预处理语句执行过程中不知道什么问题,求大神解答,分数不多,下面是代码

WBOY
Release: 2016-06-23 13:25:26
Original
920 people have browsed it

$servername = 'servername'; //服务器名称$username = 'username'; //数据库用户名$password = 'password'; //登录密码$dbname = 'mydb';//创建连接$conn = new mysqli($servername, $username, $password, $dbname);//检测连接(PHP 5.2.9和5.3.0版本适用)/* if($conn->connect_error){	die('连接失败:' . $conn->connect_error);} *///检测连接(兼容更早版本)if(mysqli_connect_error()){	die('连接失败:' . mysqli_connect_error());}else{	$sql = "INSERT INTO myguests VALUES(?, ?, ?)";		//为mysqli_stmt_prepare()初始化statement对象	$stmt = mysqli_stmt_init($conn); 	echo $stmt; //不知道为什么$stmt是空的,没有数据		//预处理语句	if(mysqli_stmt_prepare($stmt, $sql)){		//绑定参数		mysqli_stmt_bind_param($stmt, 'sss', $firstname, $lastname, $email);				//设置参数并执行		$firstname = 'Sam';		$lastname = 'Poly';		$email = 'sam@gmail.com';		mysqli_stmt_execute($stmt);				$firstname = 'Mary';		$lastname = 'Moe';		$email = 'mary@gmail.com';		mysqli_stmt_execute($stmt);				$firstname = 'Julie';		$lastname = 'Dooley';		$email = 'julie@gmail.com';		mysqli_stmt_execute($stmt);				echo '插入成功';	}}
Copy after login

好像mysqli_stmt_init($conn)执行后没反应,我echo了一下,没东西输出,所以后面的mysqli_stmt_prepare($stmt, $sql)也执行不下去了,实在不清楚为什么没有执行,也没有报错


回复讨论(解决方案)

你用混了!
$conn = mysqli_connect(....)
mysqli_stmt_init($conn)
这是一对

$conn = new mysqli(.....)
$conn ->stmt_init()
这才是一对

谢谢版主热心的回答,自己刚刚找到问题了,你列出来的两种方式都可以用,问题还是出在SQL语句上了,INSERT语句的参数列表我没有写,所以语句就不执行了,原来echo mysqli_stmt_init($conn)本来就是不输出任何结果的,我误以为没有值就是错的,最近刚学PHP,所以代码是照着一个教程敲的,这教程真是坑

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template