简体   繁体   English

mysqli_query()失败MAMP Phpmyadmin PHP

[英]mysqli_query() failed MAMP Phpmyadmin PHP

I'm new to most of this, but I'm trying to simply insert some information into my new database in phpmyadmin through php code. 我是大多数人的新手,但我试图通过php代码将一些信息简单地插入phpmyadmin中的新数据库中。 Here is the code I'm using to connect (dbh.inc.php) 这是我用来连接的代码(dbh.inc.php)


<?php

$user = 'root';
$password = 'root';
$db = 'test';
$host = 'localhost';
$port = 8889;

$link = mysqli_init();
$conn = mysqli_real_connect(
   $link, 
   $host, 
   $user, 
   $password, 
   $db,
   $port
);

?>

And here is the code i'm using to connect and insert 这是我用来连接和插入的代码


<?php

    include_once 'includes/dbh.inc.php';

    $name = $_POST['mName'];
    $email = $_POST['mEmail'];
    if($conn)
        echo "Connected <br/>";
    else
    {
        echo "Error: Unable to connect to MySQL." . PHP_EOL;
        echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
        echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    }

    if(isset($_POST['mName']))
        echo $name.'<br/>';
    if(isset($_POST['mEmail']))
        echo $email.'<br/>';

    $sql = "INSERT INTO userinfo (_email, _name, _date) VALUES ('$email', '$name', '2018-6-23 11:20:01')";
    echo "Query = " . $sql.'<br/>';
    if(mysqli_query($conn,$sql))
        echo 'It Worked!';
    else
    {
        echo "Error: " . mysqli_error($conn);
    }


?>

Here's a picture of my phpmyadmin database with one test entry 这是我的phpmyadmin数据库的图片,其中包含一个测试条目 在此处输入图片说明

and finally, here's what I get when trying to run this code: 最后,这是尝试运行此代码时得到的信息:

在此处输入图片说明

As you can see, I connect to the database just fine, and then I hit my "Error:" line after trying to insert information, but nothing prints, and nothing is added to my phpmyadmin database. 如您所见,我可以很好地连接到数据库,然后在尝试插入信息后点击“错误:”行,但是没有任何显示,并且没有任何内容添加到我的phpmyadmin数据库中。 I'm using MAMP. 我正在使用MAMP。

Does anyone have any ideas as to why this is happening? 有谁知道为什么会这样吗? Let me know if additional info is needed. 让我知道是否需要其他信息。

Thank you so much :) 非常感谢 :)

You are checking the wrong variable for your mysqli_error() . 您正在为mysqli_error()检查错误的变量。 mysqli_real_connect() returns a boolean and mysqli_error() requires the result of mysqli_init () or mysqli_connect() . mysqli_real_connect()返回一个布尔值,而mysqli_error()需要mysqli_init ()mysqli_connect()

// change 
echo "Error: " . mysqli_error($conn);

// to this
echo "Error: " . mysqli_error($link);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM