为什么这个INSERT不起作用? (PHP MySQL)

时间:2016-06-23 02:12:46

标签: php mysql

我只想尝试将记录插入MySQL数据库。为什么这不起作用?

文件名:insert.php

<?php

$l_dbhost = "x.x.x.x";
$l_dbuser = "dbuser";
$l_dbpass = "password";
$l_db = "db";
$l_table = "db.users";
$l_datetime = date_create()->format('Y-m-d H:i:s');

$l_username = "bob";
$l_password = "bobpass";

$db_conn = mysqli_connect($l_dbhost, $l_dbuser, $l_dbpass, $l_db) or die("Failed to connect!");

$db_query = "INSERT INTO $l_table (username, password, lastUpdate) VALUES ('$l_username', '$l_password', '$l_datetime')";

echo $db_query . "\n";

$db_result = mysql_query($db_conn, $db_query) or die("Failed to update table!");
?>

这是执行&#39; php insert.php&#39; ...

的输出
INSERT INTO users (username, password, lastUpdate) VALUES ('bob', 'bobpass', '2016-06-22 21:03:06')
PHP Warning:  mysql_query() expects parameter 1 to be string, object given in /var/www/example.com/insert.php on line 19
Failed to update table!

MySQL表信息:

mysql> describe users;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| username   | varchar(45) | NO   | PRI | NULL    |       |
| password   | varchar(45) | YES  |     | NULL    |       |
| lastUpdate | datetime    | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

感谢您的反馈! TXBrew

1 个答案:

答案 0 :(得分:0)

你正在组合一个mysqli查询和一个mysql查询:

$db_conn = mysqli_connect($l_dbhost, $l_dbuser, $l_dbpass, $l_db) or die("Failed to connect!");
$db_result = mysql_query($db_conn, $db_query) or die("Failed to update table!");

应该是mysqli或pdo并避免使用mysql查询,因为它们已经过时并且已弃用。