-3
<?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() 返回“未选择数据库”!为什么?

4

2 回答 2

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());
于 2013-10-01T10:32:32.930 回答
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\')");
于 2013-10-01T10:33:50.010 回答