douxi2011 2012-07-24 02:17
浏览 64
已采纳

在非对象PHP / SQL上调用成员函数execute()

what the following is supposed to do , is grab the title of the table we want to create from an HTML form, for now I hard coded the value of that ... But even with a hard coded value i'm getting the following error . Connected to database Fatal error: Call to a member function execute() on a non-object in 'filepath'.php on line 44

    <?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条回答 默认 最新

  • dpnhp20440 2012-07-24 02:19
    关注

    The $sql and $sqlb variables contain strings, not PHP objects. They don't have an execute() method.

    You need to prepare the queries first, then you can execute the resulting statement object:

    $stmt = $conn->prepare($sql);
    $stmtb = $conn->prepare($sqlb);
    
    $stmt->execute();
    $stmtb->execute();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里