PHP MySQL插入记录

PHP mysql_query()函数用于在表中插入记录。从PHP 5.5开始, 不推荐使用mysql_query()函数。现在建议使用两种替代方法之一。

  • mysqli_query()
  • PDO :: __ query()
PHP MySQLi插入记录示例例子
< ?php $host = 'localhost:3306'; $user = ''; $pass = ''; $dbname = 'test'; $conn = mysqli_connect($host, $user, $pass, $dbname); if(!$conn){ die('Could not connect: '.mysqli_connect_error()); } echo 'Connected successfully< br/> '; $sql = 'INSERT INTO emp4(name, salary) VALUES ("sonoo", 9000)'; if(mysqli_query($conn, $sql)){ echo "Record inserted successfully"; }else{ echo "Could not insert record: ". mysqli_error($conn); }mysqli_close($conn); ?>

【PHP MySQL插入记录】输出
Connected successfully Record inserted successfully

    推荐阅读