Php Mysqli准备状态每次都失败
Mysqli prepare语句每次尝试都失败。我试了很多东西,但都不管用。我的密码是:Php Mysqli准备状态每次都失败,php,mysql,mysqli,prepared-statement,Php,Mysql,Mysqli,Prepared Statement,Mysqli prepare语句每次尝试都失败。我试了很多东西,但都不管用。我的密码是: <?php include_once 'config.php'; class Database { function insertImage($title,$desc,$thumbnail_path,$image_path, $uploaded_by){ $link = mysqli_connect('DB_SERVER', 'DB_USER', 'DB_PASSWORD', 'DB_NAME
<?php
include_once 'config.php';
class Database {
function insertImage($title,$desc,$thumbnail_path,$image_path, $uploaded_by){
$link = mysqli_connect('DB_SERVER', 'DB_USER', 'DB_PASSWORD', 'DB_NAME');
/* check connection */
if (!$link) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
else{
$query = "INSERT INTO photos (title, description, thumbnail,image,uploaded_by) VALUES(?, ?, ?, ?, ?)";
$stmt = mysqli_prepare($link,$query);
if(!$stmt){
die("NO statement");
}
else
{
mysqli_stmt_bind_param($stmt,'sssss',$title,$desc,$thumbnail_path,$image_path,$uploaded_by);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
}
mysqli_close($link);
}
}
请告诉我哪里出了问题。
谢谢。您没有正确使用课堂。我建议你找一本关于面向对象编程的教程,这样你就会明白了。下面是用基本OO格式重写的类
<?php
include_once 'config.php';
class Database {
private $conn;//the class property holding your mysqli database object
function __construct(){
$this->conn = mysqli_connect('DB_SERVER', 'DB_USER', 'DB_PASSWORD', 'DB_NAME');//construct your $mysqli object.
}
public function insertImage($title,$desc,$thumbnail_path,$image_path, $uploaded_by){
try{
$query = $this->conn->query("START TRANSACTION;");//start the transaction
$stmt = $this->conn->prepare("INSERT INTO photos (title, description, thumbnail,image,uploaded_by) VALUES(?, ?, ?, ?, ?);");//OO prepared statement
$stmt->bind_param('sssss',$title,$desc,$thumbnail_path,$image_path,$uploaded_by);//bind params
if(!$stmt->execute()){ //if statement doesn't execute
throw new Exception('problem with query');//throw execption, the statement failed to execute.
}else{
$stmt->close();//close the statement
$query = $this->conn->query("COMMIT;");//commit the transaction
}
catch(Exception $e){
$query = $this->conn->query("ROLLBACK;");//roll the database back removing any partially commited data.
return $e->getMessage();
}
}
}
你犯了什么错误?没有陈述,没有陈述;执行。您确定mysqli_bind_参数中的参数类型都正确吗?请检查您的数据库中是否有“按”字段上载的\u。它闻起来像整数,你在传递字符串。我们可以看看photos表的结构和数据类型吗?你能试试这个吗:如果mysqli_connect_errno{echo无法连接到MySQL:.mysqli_connect_error;}//要检查链接是否已设置,已经检查了连接。我不会为此使用事务。当ACID属性在银行系统中非常重要时,使用事务。否则,对我来说,这似乎是一种过激行为。@Luka如果数据库中有带有默认值的列,它将允许部分数据插入。很明显,您在某个地方读到了这一点,并将其作为个人资料,但相信我,ACID对于用于多种用途的应用程序数据至关重要,而不仅仅是银行系统。我指的是银行系统只是一个示例,而不是交易的专用应用程序。在数据库中允许使用默认值在这里没有多大区别。如果允许使用默认值,则事务不会阻止插入此类记录。
<?php
include_once 'config.php';
class Database {
private $conn;//the class property holding your mysqli database object
function __construct(){
$this->conn = mysqli_connect('DB_SERVER', 'DB_USER', 'DB_PASSWORD', 'DB_NAME');//construct your $mysqli object.
}
public function insertImage($title,$desc,$thumbnail_path,$image_path, $uploaded_by){
try{
$query = $this->conn->query("START TRANSACTION;");//start the transaction
$stmt = $this->conn->prepare("INSERT INTO photos (title, description, thumbnail,image,uploaded_by) VALUES(?, ?, ?, ?, ?);");//OO prepared statement
$stmt->bind_param('sssss',$title,$desc,$thumbnail_path,$image_path,$uploaded_by);//bind params
if(!$stmt->execute()){ //if statement doesn't execute
throw new Exception('problem with query');//throw execption, the statement failed to execute.
}else{
$stmt->close();//close the statement
$query = $this->conn->query("COMMIT;");//commit the transaction
}
catch(Exception $e){
$query = $this->conn->query("ROLLBACK;");//roll the database back removing any partially commited data.
return $e->getMessage();
}
}
}