PHP mysqli_query error in syntax

2019-09-21 16:42发布

问题:

I have a problem with my code. I think it's good but the console says its not:

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/u772833821/public_html/DB_Functions.php on line 66

Here is the code :

$result = mysqli_query($con, "INSERT INTO users SET (email, username, encrypted_password, salt, created_at) VALUES('$email', '$uname', '$encrypted_password', '$salt', NOW())");

回答1:

Your query is incorrect. Try as below :

$result = mysqli_query($con, "INSERT INTO users (email, username, encrypted_password, salt, created_at) VALUES('$email', '$uname', '$encrypted_password', '$salt', NOW())");

Try to check $con is valid or not:

$host = "your_host"; // replace these settings for your own
$username = "your_username";
$password = "your_password_if_any";
$db = "your_db_name";
$con = mysqli_connect($host, $username, $password, $db);

if (!$con) {
    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;
    exit;
}

References:

  • http://php.net/manual/en/function.mysqli-connect.php
  • http://php.net/manual/en/mysqli.query.php
  • http://php.net/manual/en/mysqli.error.php
  • http://dev.mysql.com/doc/refman/5.7/en/insert.html


回答2:

Please follow steps, there are more then one correction:

1. Remove SET from INSERT query

$result = mysqli_query($con, "INSERT INTO users (email, username, encrypted_password, salt, created_at) VALUES('$email', '$uname', '$encrypted_password', '$salt', NOW())");

2. Check connection error

$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);

if (!$con) {
  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;
  exit;
}

Perhaps issue with connection. Let me know what is the output from point 2.



标签: php mysqli