此代码工作正常!
$con=mysqli_connect("localhost","root","","laboratory");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM test");
while($row = mysqli_fetch_array($result))
{
echo $row['name'];
echo "<br>";
}
mysqli_close($con);
但是当我从 mysqli_connect 中删除 database_name 时,我会使用 mysql_select_db,出现以下错误“警告:mysql_select_db() 期望参数 2 是资源,给定的对象”
修改后的代码:
$con=mysqli_connect("localhost","root","");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$db_selected = mysql_select_db("laboratory", $con);
if (!$db_selected)
{
die ("Can\'t use laboratory : " . mysql_error());
}
$result = mysqli_query($con,"SELECT * FROM test");
while($row = mysqli_fetch_array($result))
{
echo $row['name'];
echo "<br>";
}
mysqli_close($con);