php mysqli使用数组连接

时间:2017-09-28 06:51:18

标签: php mysqli

设置与数据库的连接的最佳做法是什么。使用普通数组还是关联数组?当我使用关联数组时我有这个错误警告:mysqli :: __ construct():( HY000 / 1044):访问被拒绝用户'' @' localhost'到数据库' cms'在第21行的C:\ xampp \ htdocs \ cms \ includes \ db.php中,但它表示我已连接。 我们有联系!

<?php
//stores the database host,user,pass,name into array

/*$db['db_host'] = "localhost";
$db['db_user'] = "root";
$db['db_pass'] = "";
$db['db_name'] = "cms";*/

/*$db = array("db_host"=>"localhost",
        "db_user"=>"user",
        "db_pass"=>"",
        "db_name"=>"cms");*/

//we loop through each of the array
foreach ($db as $key => $value){

define(strtoupper($key), $value);

}

$connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

if(isset($connection)){
    echo "We are connected!";
}else{
    echo "Connection failure!";
}

1 个答案:

答案 0 :(得分:3)

条件是你使用错误的方式<Button x:Name="Del" DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor AncestorType=UserControl AncestorLevel=3}}"只检查变量是否设置。 isset已设置(如果已连接或未连接。如果连接有问题,则会包含错误),因此请将条件更改为:

$connection