5

Unable to connect to mySQL server using AOP

 2 years ago
source link: https://www.codesd.com/item/unable-to-connect-to-mysql-server-using-aop.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Unable to connect to mySQL server using AOP

advertisements

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");
}

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");
}

Where's the problem at? Thanks.


So as I mention in comment,

pdo -> query($insert);

should be

$pdo->query($insert);


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK