dongqie2010
2018-06-02 00:29
浏览 43
已采纳

将查询传递给PDO时出错

Whenever I pass a query after a PDO connection the page returns an 500 error. I don't know whats causing the issue. I am using LAMP server. Here is the code:

$user = 'root';
$database = 'mysql';
$password = 'root';
$dbname = 'pdotest';
$host = 'localhost';

$dsn = $database . ":" . $host . ";dbname=" . $dbname;

$opt = array(
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
);

$pdo = new PDO($dsn, $user, $password, $opt) or die('Can\'t establish connection');

// This is where it gives 500 error

$stmt = $pdo->query("SELECT * FROM pdo");

图片转代码服务由CSDN问答提供 功能建议

每当我在PDO连接后传递查询时,页面都会返回500错误。 我不知道是什么造成了这个问题。 我正在使用LAMP服务器。 下面是代码:

  $ user ='root'; 
 $ database ='mysql'; 
 $ password ='root'; 
 $ dbname ='  pdotest'; 
 $ host ='localhost'; 
 
 $ dsn = $ database。  “:”。  $ host。  “; dbname =”。  $ dbname; 
 
 $ opt = array(
 PDO :: ATTR_ERRMODE => PDO :: ERRMODE_EXCEPTION,
 PDO :: ATTR_DEFAULT_FETCH_MODE => PDO :: FETCH_OBJ,
); 
 
 $ pdo  =新的PDO($ dsn,$ user,$ password,$ opt)或死('不能建立连接'); 
 
 //这是它给出500错误的地方
 
 $ stmt = $  pdo-> query(“SELECT * FROM pdo”); 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dongwei3866 2018-06-02 00:40
    最佳回答

    Seems like I made a silly mistake. Just missed a host in dsn. Changing dsn from:

    $dsn = $database . ":" . $host . ";dbname=" . $dbname;
    

    To:

    $dsn = $database . ":host=" . $host . ";dbname=" . $dbname;
    

    solved my problem.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题