无法使用PDO连接到MySQL服务器 [英] Can't connect to mySQL server using PDO

查看:204
本文介绍了无法使用PDO连接到MySQL服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是mySQL的新手,我使用的代码正在运行,显然已经过时,因此我改用PDO.我试图将编码尽可能准确地从旧样式转换为PDO,但是现在我无法连接,并且我确定自己只是在做一些愚蠢的事情来弄乱它.

I'm new to mySQL, and the code I used, which was working, is apparently out of date, so I switched to PDO. I tried to transfer my coding as accurately as possible from the old style to PDO, but now I can't connect and I'm sure I'm just doing something silly to mess it up.

这是我的旧代码,可以正常工作:

Here's my old code which worked:

//insert.php
mysql_connect("localhost","root","root");//database connection
mysql_select_db("Menu_Items");

$name = $_POST['food'];
$order = "INSERT INTO foods
            (name)
            VALUES
            ('$name')";
//inserting data order
//declare in the order variable
$result = mysql_query($order);
if($result){
    echo("<br>Input data is succeed");
}else{
    echo("<br>Input data is fail");
}

这是我无法使用的新PDO代码:

Here's my new PDO code which is not working:

$dbc    = "mysql:host=localhost;dbname=Menu_Items";
$user   = "root";
$pass   = "root";

$pdo    = new pdo($dbc, $user, $pass);
$name   = $_POST['food'];
$insert     = "INSERT INTO foods (name) VALUES ('$name')";

$result = pdo -> query($insert);
if($result){
    echo("<br>Input data is succeed");
}else{
    echo("<br>Input data is fail");
}

问题出在哪里?谢谢.

推荐答案

正如我在评论中提到的那样,

So as I mention in comment,

pdo -> query($insert);

应该是

$pdo->query($insert);

这篇关于无法使用PDO连接到MySQL服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆