php 函数不与查询连接到数据库[重复]

juzqafwq  于 2023-11-16  发布在  PHP
关注(0)|答案(3)|浏览(155)

此问题在此处已有答案

"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP(29个回答)
Reference - What does this error mean in PHP?(38个回答)
7天前关闭
我使用了一个函数,它应该得到结果行作为一个关联数组,但它没有得到我,因为它改变了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位于**/home/sfera/public_html/locale/Polish-utf8/global.php上线140**
注意事项:未定义索引:siteurl位于**/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"));


所以这是,但现场不想读他们

4szc88ey

4szc88ey1#

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_select_db()失败时应该使用mysqli_error()。它也需要返回连接,而不是设置全局变量。

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()结合使用,因为数据库名称可以指定为x1m10 n1x的附加参数:

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

wf82jlnq

wf82jlnq2#

根据我在这里收集的信息,您的代码需要更改为:
第一个月
请注意,上面发布的代码中缺少$link变量。

lg40wkob

lg40wkob3#

不要使用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/>";
}

字符串

相关问题