我在php中使用prepare和bind_param语句时遇到问题。我的代码如下:
<?php
$dbhost = 'localhost'; //default
$dbuser = 'root';
$dbpass = ‘somepassword’;
//Create a connection object
$conn = new mysqli($dbhost, $dbuser, $dbpass);
if($conn->connect_error )
{
die('Could not connect: %s' . $conn->connect_error);
}
echo "Connected successfully<br>";
$conn->select_db("TUTORIAL");
$stmt=$conn->prepare("INSERT INTO tutorial_info (tutorial_title,tutorial_author) VALUES (?,?)");
$stmt->bind_param("sss",$tutorial_title,$tutorial_author);
//try and insert
$tutorial_title=“English Lit”;
$tutorial_author=“Bob Trotter”;
$stmt->execute();
//Close the database
$conn->close();
?>
当我执行脚本时,我收到消息&#34;已成功连接&#34;但是,如果我检查表格内容,则尚未添加新行。我在这里做错了什么想法?
我还在文档中看到,在bind_param中我们添加了一个额外的参数,即$ stmt-&gt; bind_param(&#34; sss&#34; ,$ tutorial_title,$ tutorial_author);在这种情况下,它是&#34; sss&#34;。它是干什么用的?我可以摆脱它吗?
任何指针都会非常感激!感谢
答案 0 :(得分:1)
bind_param
的第一个参数描述了参数的类型。 RTM!
例如,您可以为s
指定string
,为i
指定integer
。第一个参数中的字符数必须完全覆盖其他参数的数量。
答案 1 :(得分:1)
试试此代码
<?php
$dbhost = 'localhost'; //default
$dbuser = 'root';
$dbpass = ‘somepassword’;
//Create a connection object
$conn = new mysqli($dbhost, $dbuser, $dbpass);
if($conn->connect_error )
{
die('Could not connect: %s' . $conn->connect_error);
}
echo "Connected successfully<br>";
$conn->select_db("TUTORIAL");
$stmt=$conn->prepare("INSERT INTO tutorial_info (tutorial_title,tutorial_author) VALUES (?,?)");
$stmt->bind_param("ss",$tutorial_title,$tutorial_author);
//try and insert
$tutorial_title=“English Lit”;
$tutorial_author=“Bob Trotter”;
$stmt->execute();
//Close the database
$conn->close();
?>
答案 2 :(得分:1)
删除额外的
$stmt=$conn->prepare("INSERT INTO tutorial_info (tutorial_title,tutorial_author) VALUES (?,?)");
$stmt->bind_param("ss",$tutorial_title,$tutorial_author);