该函数不与查询连接到数据库

时间:2018-08-27 20:45:50

标签: php

我正在使用一个应以关联数组的形式获取结果行的函数,但并没有得到我的支持,因为它根据主持人的指示从mysql更改为mysqli。

我在做什么错

function dbquery($link,$query) {

    $result = mysqli_query($link, $query );
    if (!$result) {
        echo mysqli_connect_error();
        return false;
    } else {
        return($result);

    }
    mysqli_close($link);
}

连接数据库的功能

function dbconnect($db_host, $db_user, $db_pass, $db_name) {
    global $db_connect;
    $db_connect = mysqli_connect($db_host, $db_user, $db_pass);
    $db_select = mysqli_select_db($db_connect, $db_name);
    if (!$db_connect) {
        die("<div style='font-family:Verdana;font-size:11px;text-align:center;'><b>Unable to establish connection to MySQL</b><br />".mysqli_connect_error()." : ".mysqli_connect_error()."</div>");
    } elseif (!$db_select) {
        die("<div style='font-family:Verdana;font-size:11px;text-align:center;'><b>Unable to select MySQL database</b><br />".mysqli_connect_error($db_name)." : ".mysqli_connect_error()."</div>");
    }

}
$link = dbconnect($db_host, $db_user, $db_pass, $db_name);

怎么了?

  


通知:未定义的索引:siteurl在    /home/sfera/public_html/locale/Polish-utf8/global.php 在线    132

通知:未定义的索引:siteurl in    /home/sfera/public_html/locale/Polish-utf8/global.php 在线    140

通知:未定义索引:siteurl in    /home/sfera/public_html/locale/Polish-utf8/global.php 在线    147

您知道如何从语言环境中删除该错误。我会告诉你,你知道发生了什么事

    $locale['global_441'] = "Your account on ".$settings['sitename']."has been banned";
$locale['global_442'] = "Hello [USER_NAME],\n
Your account on ".$settings['sitename']." was caught posting too many items to the system in very short time from the IP ".USER_IP.", and have therefor been banned. This is done to prevent bots from submitting spam messages in rapid succession.\n
Please contact the site administrator at ".$settings['siteemail']." to have your account restored or report if this was not you causing this security ban.\n
".$settings['siteusername'];
// Lifting of suspension
$locale['global_450'] = "Suspension automatically lifted by system";
$locale['global_451'] = "Suspension lifted at ".$settings['sitename'];
$locale['global_452'] = "Hello USER_NAME,\n
The suspension of your account at ".$settings['siteurl']." has been lifted. Here are your login details:\n
Username: USER_NAME
Password: Hidden for security reasons\n
If you have forgot your password you can reset it via the following link: LOST_PASSWORD\n\n
Regards,\n
".$settings['siteusername'];
$locale['global_453'] = "Hello USER_NAME,\n
The suspension of your account at ".$settings['siteurl']." has been lifted.\n\n
Regards,\n
".$settings['siteusername'];
$locale['global_454'] = "Account reactivated at ".$settings['sitename'];
$locale['global_455'] = "Hello USER_NAME,\n
Last time you logged in your account was reactivated at ".$settings['siteurl']." and your account is no longer marked as inactive.\n\n
Regards,\n

尽管我在函数中有一个参数,但这仍然使我成为一个基础论点

///从数据库中获取网站设置,并将其存储在$ settings变量中

$settings = dbarray(dbquery($link,"SELECT * FROM ".$db_prefix."setting"));

这只是语言环境,不想阅读它们

3 个答案:

答案 0 :(得分:2)

mysqli_connect_error()仅应用于报告在mysqli_connect()期间发生的错误。如果执行查询时出现错误,则应使用mysqli_error()来获取该错误。

此外,您正在呼叫mysqli_close($link);。从未执行过此操作,因为if语句的两个分支均从该函数返回。但是您不应该在此函数中关闭该链接,您很可能希望对其他查询使用相同的链接。

所以函数应该是:

function dbquery($link,$query) {
    $result = mysqli_query($link, $query );
    if (!$result) {
        echo mysqli_error($link);
        return false;
    } else {
        return($result);
    }
}

类似地,dbconnect()在报告mysqli_error()失败时应使用mysqli_select_db()。它还需要返回连接,而不是设置全局变量。

function dbconnect($db_host, $db_user, $db_pass, $db_name) {
    $db_connect = mysqli_connect($db_host, $db_user, $db_pass);
    $db_select = mysqli_select_db($db_connect, $db_name);
    if (!$db_connect) {
        die("<div style='font-family:Verdana;font-size:11px;text-align:center;'><b>Unable to establish connection to MySQL</b><br />".mysqli_connect_error()." : ".mysqli_connect_error()."</div>");
    } elseif (!$db_select) {
        die("<div style='font-family:Verdana;font-size:11px;text-align:center;'><b>Unable to select MySQL database $db_name</b><br />".mysqli_error($db_connect)." : ".mysqli_error($db_connect)."</div>");
    }
    return $db_connect;
}

您还可以组合mysqli_connect()mysqli_select_db(),因为可以将数据库名称指定为mysqli_connect()的附加参数:

mysqli_connect($db_host, $db_user, $db_pass, $db_name);

答案 1 :(得分:0)

根据我在这里收集的信息,您的代码需要更改为:

$result = dbquery($link, "SELECT * FROM ".$db_prefix."product ");

请注意,上面发布的代码中缺少$link变量。

答案 2 :(得分:-2)

不使用MySQL或MySQLi。使用PDO:

ob_start();
session_start();
$db_hostname = "localhost";
$db_username = "root";
$db_password = "";
$db_name = "XXXXXX";
#error_reporting(0);
#error_reporting(E_ALL);
#ini_set('display_errors', 1);
try {
$dbh = new PDO("mysql:host=$db_hostname;dbname=$db_name", $db_username, $db_password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

} catch (PDOException $e) {
    echo $e->getMessage()."<br/>";
}