[英]PHP: Can't find my database
我在连接数据库时遇到问题
<?php
@mysqli_connect( "127.0.0.1", "root", "root" ) or die( "Cannot connect to the database!" );
@mysqli_select_db( "phpherexamen" ) or die( "Connected to the database, but there's nothing here!" );
session_start();
?>
这也是我用来连接我的bd的代码,但是我总是得到消息:
已连接到数据库,但是这里什么都没有!
我的数据库看起来像这样
我没看到什么? :)
您应该将mysqli_connect函数的结果用作mysql_select_db函数的第二个参数。
$link = mysqli_connect( "127.0.0.1", "root", "root" )
if (!$link) {
die("Cannot connect to the database!");
}
$db_selected = mysql_select_db('phpherexamen', $link);
if (!$db_selected) {
die ("Connected to the database, but there's nothing here!");
}
使用过程样式mysqli
您需要将mysqli_select_db
传递mysqli_select_db
mysqli
实例
$mysqli = mysqli_connect("127.0.0.1", "root", "root");
mysqli_select_db($mysqli, "phpherexamen");
将连接存储在变量中,然后将其传递给select_db命令。
$db = mysqli_connect(...);
mysqli_select_db($db, 'phpherexamen');
如果您使用面向对象的样式:
$handle = @mysqli_connect( "127.0.0.1", "root", "root" ) or die( "Cannot connect to the database!" );
$handle->mysqli_select_db( "phpherexamen" ) or die( "Connected to the database, but there's nothing here!" );
或程序样式:
$handle = @mysqli_connect( "127.0.0.1", "root", "root" ) or die( "Cannot connect to the database!" );
@$mysqli_select_db($handle, "phpherexamen" ) or die( "Connected to the database, but there's nothing here!" );
但是Mysqli需要一个对象来知道mysql select db应该应用于哪个连接。
旁注:已被MySQLi取代的旧扩展MySQL支持缓存最后一个连接,因此您无需指定数据库句柄,但是如果您的网站使用多个连接或要在循环时触发查询,则这是一个不错的选择结果或类似的结果,为什么它只能与有效的句柄或更好的所述对象一起使用,所以比其他旧方法更理智。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.