PHP mysql_query()函数用于在表中插入记录。
由于PHP5.5版本之后不再推荐使用mysql_query()函数,建议用以下2种代替方法
mysqli_query()
PDO::query()
示例
<?php
$host = 'localhost:3306';
$user = 'root';
$pass = '';
$dbname = 'mydb';
$conn = mysqli_connect($host, $user, $pass, $dbname);
if(!$conn)
{
die('Could not connect: '.mysqli_connect_error());
}
echo 'Connect successfully<br/>';
$sql = 'INSERT INTO emp5(name,emp_salary) VALUES ("maxsu" , 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 sucessfully