PHP mysqli_query error in syntax

2019-09-21 16:40发布

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

标签: php mysqli
2条回答
够拽才男人
2楼-- · 2019-09-21 16:50

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.

查看更多
一纸荒年 Trace。
3楼-- · 2019-09-21 16:53

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:

查看更多
登录 后发表回答