php - MySQL -- fatal error : Call to a member function bind_param() on a non-object

标签 php mysql sql create-table prepare

我已经搜索了几十个与这篇文章标题几乎相同的问题,但我还没有找到我的具体问题的答案。我正在尝试准备一份声明,但每次都会收到此错误。我在想我的 sql 语法一定有某种错误,但我终究找不到它。

$title = "this_is_my_table_title";    

//import the database login credential variables
require("localhost_credentials.php");

$conn = new mysqli($db_servername, $db_username, $db_password, $db_name);
if($conn->connect_error)
{
    die("Connection failed: " . $conn->connect_error);
}

$querystr  = "CREATE TABLE ? (";
$querystr .= "id int(11) not null auto_increment,";
$querystr .= "section varchar(255) not null,";
$querystr .= "raw_content LONGTEXT not null,";
$querystr .= "formatted_content LONGTEXT not null,";
$querystr .= "primary key (id)";
$querystr .= ");";


$statement = $conn->prepare($querystr);  <--- error here
$statement->bind_param("s", $title);
$statement->execute();
$statement->close();

最佳答案

表名不能使用占位符,它只能用来代替表达式。所以 CREATE TABLE ? 无效。使用:

$querystr  = "CREATE TABLE `$title` (";

在验证 $title 是可接受的表名之后。

关于php - MySQL -- fatal error : Call to a member function bind_param() on a non-object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31732589/

相关文章:

php - mysql 功能,例如 'show processlist' 以及客户端代码信息

php - MySql 查询以在 Chartjs 中显示数据

mysql - FROM 崩溃查询中未使用的表

php - Mysql aruba Json类型不存在

php - CodeIgniter - 每小时方法调用

php - 如何获取最近查看的记录的id来执行cgridview,它对于yii中的每条记录都是唯一的?

php - MySql 表上的自连接

php - 如何向 MySQL 添加自定义时区

php - Yii2:Gridview 中过滤值列的总和

sql - 如何只允许sql中的某些字符(oracle)