php不合理的错误没有数据库选择,但我选择数据库

时间:2013-10-01 10:22:04

标签: php mysql

<?php
// Create connection
$con=mysql_connect('localhost','root','');//mysqli_connect(host,username,password,dbname);
mysql_select_db('stat');

// Check connection
if (mysqli_connect_errno($con))
echo "Failed to connect to MySQL: " . mysqli_connect_error();
else 
echo "yes\n";

$sql = "INSERT INTO stat VALUES (NULL, \'2012-09-02\', \'1000\')";
$result2 = mysql_query($sql) or die(mysql_error());
mysql_close($con);
?>

选择了DB,但msql.error()返回“No database selected”! 为什么呢?

2 个答案:

答案 0 :(得分:2)

我修改了你的代码试试这个。

$con=mysql_connect('localhost','root','') or die("!Server");

mysql_select_db('stat',$con) or die("!db");

$sql = "INSERT INTO stat VALUES (NULL,'2012-09-02','1000')";

$result2 = mysql_query($sql) or die(mysql_error());

答案 1 :(得分:2)

$con=mysqli_connect("localhost","root","","stat");
// Check connection
if (mysqli_connect_errno())
 {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }

mysqli_query($con,"INSERT INTO stat(column1, column2, column3)
VALUES (NULL, \'2012-09-02\', \'1000\')");