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);
?>
输出:
Connected successfully
Record inserted successfully