简体   繁体   English

无法在php中使用prepare和bind_param

[英]Trouble using prepare and bind_param in php

I am having trouble using prepare and bind_param statements in php. 我在php中使用prepare和bind_param语句时遇到问题。 My code is as follows: 我的代码如下:

<?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();




 ?>

When I execute the script I get the message "connected successfully" however if I check the table contents the new row has not been added. 当我执行脚本时,会收到消息“连接成功”,但是如果我检查表内容,则不会添加新行。 Any ideas what I might be doing wrong here? 有什么想法我可能在这里做错了吗?

I also see in the documentation that in bind_param we add an additional parameter ie $stmt->bind_param( "sss" ,$tutorial_title,$tutorial_author); 我还在文档中看到,在bind_param中,我们添加了一个附加参数,即$ stmt-> bind_param( “ sss” ,$ tutorial_title,$ tutorial_author) ;。 In this case it is "sss". 在这种情况下,它是“ sss”。 What is it used for? 这有什么用途? Can I get rid of it? 我可以摆脱它吗?

Any pointers would be really appreciated! 任何指针将不胜感激! thanks 谢谢

The first parameter of bind_param describes the types of the parameters. bind_param的第一个参数描述参数的类型。 RTM! RTM!

You can specify s for string and i for integer for example. 例如,您可以为string指定s ,为integer指定i The number of characters in the first parameters have to cover the number of the other parameters exactly. 第一个参数中的字符数必须完全覆盖其他参数的数。

Try this code 试试这个代码

<?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();




 ?>

Remove extra s 删除多余的

$stmt=$conn->prepare("INSERT INTO tutorial_info (tutorial_title,tutorial_author) VALUES (?,?)");
$stmt->bind_param("ss",$tutorial_title,$tutorial_author);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM