我的PHP包括数据库连接有什么问题?

问题描述 投票:2回答:2

我试图将我的数据库连接脚本移动到外部(因此更安全)的文件。但是,它不起作用。

这是我的PHP页面(包含链接)

<?php
include 'block/datalogin.php';
..etc

并且继承阻止/ datalogin.php

$dbhost = "localhost";
$dbuser = "************";
$dbpass = "***********";
$dbname = "************";
@mysql_connect($dbhost, $dbuser, $dbpass) or die("unable to
connect to database."
);
mysql_select_db($dbname) or die ("Unable to select");

我确定路径和登录信息是正确的。

有什么建议?

php include include-path mysql-connect
2个回答
1
投票

首先,Don't use mysql_* functions in new code。他们不再维持and are officially deprecated。看看red box?了解prepared statements,并使用PDOMySQLi - this article将帮助您决定哪个。如果你选择PDO,here is a good tutorial


<?php
    $dbhost = "localhost";
    $dbuser = "************";
    $dbpass = "***********";
    $dbname = "************";
    $conn=mysql_connect($dbhost, $dbuser, $dbpass) or die("unable to connect to database.");
    mysql_select_db($dbname) or die ("Unable to select");
?>

然后在查询中使用$conn变量

mysql_query("SELECT * from ....",$conn);

2
投票

取出mysql_connect前面的“@”符号。

© www.soinside.com 2019 - 2024. All rights reserved.