[英]PHP: Can't find my database
I'm having problems connecting to 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();
?>
This is the code I'm using to connect too my bd, but i always get the message: 这也是我用来连接我的bd的代码,但是我总是得到消息:
Connected to the database, but there's nothing here!
已连接到数据库,但是这里什么都没有!
My db looks like this 我的数据库看起来像这样
What am I not seeing? 我没看到什么? :)
:)
You should use the result of mysqli_connect function as a second parameter of the mysql_select_db function. 您应该将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!");
}
With procedural style mysqli
you need to pass mysqli_select_db
an instance of mysqli
使用过程样式
mysqli
您需要将mysqli_select_db
传递mysqli_select_db
mysqli
实例
$mysqli = mysqli_connect("127.0.0.1", "root", "root");
mysqli_select_db($mysqli, "phpherexamen");
Store the connection in a variable, and then pass that to the select_db command. 将连接存储在变量中,然后将其传递给select_db命令。
$db = mysqli_connect(...);
mysqli_select_db($db, 'phpherexamen');
Wether you use the object oriented style: 如果您使用面向对象的样式:
$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!" );
or the procedural style: 或程序样式:
$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!" );
but Mysqli requires an object to know which connection the mysql select db should apply to. 但是Mysqli需要一个对象来知道mysql select db应该应用于哪个连接。
Sidenote: The old extension MySQL that was replaced by MySQLi supported to cache the last connection so you didnt need to specify the database handle but this is a great error-magnet if you have a website using multiple connections or want to fire a query while looping a result or similar thatswhy this only works with a valid handle or better said object which is alot more sane then the other, old method. 旁注:已被MySQLi取代的旧扩展MySQL支持缓存最后一个连接,因此您无需指定数据库句柄,但是如果您的网站使用多个连接或要在循环时触发查询,则这是一个不错的选择结果或类似的结果,为什么它只能与有效的句柄或更好的所述对象一起使用,所以比其他旧方法更理智。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.