1

下面应该做的,是从 HTML 表单中获取我们想要创建的表的标题,现在我硬编码了它的值......但即使使用硬编码的值,我也会收到以下错误. 连接到数据库致命错误:在第 44 行的 'filepath'.php 中的非对象上调用成员函数 execute()

    <?php

            // Configuration
            $hostname = 'host';
            $username = 'user';
            $password = 'pass';
            $database = 'dbname';

            $table = $_Post['table'];
        try {
            $conn = new PDO('mysql:host='. $hostname .';dbname='. $database, $username, $password);
    echo "Connected to database"; // check for connection
    echo $table;
    // We get the connected to database message....
$conn->exec("SET CHARACTER SET utf8");      // Sets encoding UTF-8
 // table A is going to be a var , but for now testing with a hardcoded value 
  $sql = "CREATE TABLE tableA (
  id int(8) ,
  Question varchar(170),
  AnswerA varchar(100),
  AnswerB varchar(100),
  AnswerC varchar(100),
  AnswerD varchar(100),
  A int(8),
  B int (8), 
  C int (8) , 
  D int (8)
  ) CHARACTER SET utf8 COLLATE utf8_general_ci";

  $sqlb = "INSERT INTO `DBNAME` tableA (`id`, `Question`, `AnswerA`, `AnswerB`, `AnswerC`, `AnswerD`, `A`, `B`, `C`, `D`) 
  VALUES ('1', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL), 
  ('2', NULL,  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL), 
  ('3', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL),
  ('4', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL),
  ('5', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL), 
  ('6', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL), 
  ('7', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL),
  ('8', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL), 
  ('9', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL),
  ('10', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)";
  // lets not do it all at once eh , run a secound script to add in all the values 



 $sql->execute(); 
 $sqlb->execute(); 
 // if($conn->exec($sql) !== false) echo 'The sites table is created';  
//if($conn->exec($sqlb) !== false) echo 'We inserted some values !';  


}
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
//  }
// write code to check if we succeded in creating a table with with $, if so display an alert of of some kind
// with Javascript ....

// disabled the redirect so i could see the error message
//header ("Location: quizsubmit.html");
/* Make sure that code below does not get executed when we redirect. */
exit ;



?>
4

4 回答 4

7

$sqland$sqlb变量包含字符串,而不是 PHP 对象。他们没有execute()方法。

您需要先准备查询,然后才能执行生成的语句对象:

$stmt = $conn->prepare($sql);
$stmtb = $conn->prepare($sqlb);

$stmt->execute();
$stmtb->execute();
于 2012-07-24T02:19:45.117 回答
0

你正在调用->execute()一个字符串,它应该如何工作?您需要使用prepare()方法来创建语句对象。

于 2012-07-24T02:20:04.503 回答
0

如上所述,您可以调用准备返回PDOStatement,然后调用执行或只调用execPDO 对象。1)

$stmt = $conn->prepare($sql);
$stmtb = $conn->prepare($sqlb);

$stmt->execute();
$stmtb->execute();

2)

$pdoObj = new PDO('mysql:host='. $hostname .';dbname='. $database, $username, $password);
$pdoObj->exec($sql);
于 2012-07-24T02:23:30.257 回答
0

我有同样的错误......

我正在选择一个不存在的表。我的错误。

于 2014-06-01T10:22:31.107 回答