PHP的bind_param无法正常工作 [英] PHP bind_param not working

查看:110
本文介绍了PHP的bind_param无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过PHP访问MySQL,但收到这些错误消息.

I am trying to access MySQL via PHP but I get these error messages.

注意:未定义的变量:第60行的/var/www/contact.php中的stmt

致命错误:在第63行的/var/www/contact.php中的非对象上调用成员函数bind_param()

我的代码:

<?php

$servername = "localhost";
$username = "test";
$password = "test";
$dbname = "test";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// LINE 60
if(!$stmt = $conn->prepare("INSERT INTO MyGuests VALUES(?, ?, ?)") || !is_object($stmt)){
    die( "Error preparing: (" .$conn->errno . ") " . $conn->error);
}
//LINE 63
$stmt->bind_param("sss", $firstname, $lastname, $email) ;

推荐答案

对象是$ conn,因此,如果要将其包含在if语句中,则可以但不是必需的. 我假设MyGuests表为id列和lastname,fisrtname,email列.您需要在SQL中指定实际列"INSERT INTO MyGuests(名字,姓氏,电子邮件)VALUES(?,?,?)"

The object is the $conn, so if you want to include it in the if statement you can but not necessary. Am assuming that the MyGuests table as an id column and lastname, fisrtname, email column. You need to specify the actual column in the SQL, "INSERT INTO MyGuests (firstname, lastname, email) VALUES(?, ?, ?)"

尚未定义变量$ stmt.因此,您不能说!$ stmt,该功能将无法使用.做类似的事情:

The variable $stmt is not defined yet. So you cant say !$stmt, the function not won't work. Do something like:

$stmt = "";
if(!($stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES(?, ?, ?)"))){
    die( "Error preparing: (" .$conn->errno . ") " . $conn->error);
}

$firstname = "John";
$lastname = "Doe";
$email = "john@example.com";

if(!($stmt->bind_param("sss", $firstname, $lastname, $email))){
    die( "Error in bind_param: (" .$conn->errno . ") " . $conn->error);
}


$stmt->execute();

这篇关于PHP的bind_param无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆