【问题标题】:MySQL -- Fatal error: Call to a member function bind_param() on a non-objectMySQL -- 致命错误:在非对象上调用成员函数 bind_param()
【发布时间】:2015-10-22 07:56:52
【问题描述】:

我已经搜索了几十个与这篇文章标题几乎相同的问题,但我还没有找到针对我的具体问题的答案。我正在尝试准备一份声明,但每次我都会收到此错误。我在想我的 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();

【问题讨论】:

  • prepare返回false时,执行echo $conn-&gt;error查看SQL错误信息

标签: php mysql sql create-table prepare


【解决方案1】:

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

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

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

【讨论】:

  • 啊,好吧。不知道,我以为我可以在任何地方使用占位符。谢谢!这解决了它。
  • 这是一个常见的误解。
猜你喜欢
  • 2013-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多