在mysqli_connect中使用数组值

时间:2017-08-30 09:22:15

标签: php mysql arrays file mysqli

我正在从文件中读取数据库连接凭据并将其存储在数组中。我想在mysql_connect()中使用数组中的值。但是我收到警告并且没有连接:

  

警告:mysqli_connect():php_network_getaddresses:getaddrinfo失败:没有这样的主机。在第13行的A:\ xamppnew \ htdocs \ mdb_display.php中   警告:mysqli_connect():( HY000 / 2002):php_network_getaddresses:getaddrinfo failed:没有这样的主机。在第13行的A:\ xamppnew \ htdocs \ mdb_display.php

这是我的代码

    <?php
    $myfile = fopen("db_con.txt", "r") or die("Unable to open file!");
    $db_cred="";
    while(!feof($myfile)) {
        $db_cred[]= fgets($myfile) ;
       }
    fclose($myfile);


    $con = mysqli_connect($db_cred[0],$db_cred[1],$db_cred[2],$db_cred[3]);

 $var="select ulastdatetime from userattedancelog where orgid=1 ";
 $result = $con->query($var);
 $row = $result->fetch_assoc();
 echo $row["ulastdatetime"];
 $dat='#'. $row["ulastdatetime"].'#';

 //closing databse connection
 $con->close();
 ?>

如何在mysql_connect中使用数组中的值。

1 个答案:

答案 0 :(得分:0)

gets之后修剪数据,以便删除额外的新行空间

$myfile = fopen("db_con.txt", "r") or die("Unable to open file!");
    $db_cred=array();
    while(!feof($myfile)) {
        $db_cred[]= trim(fgets($myfile)); //<------------trim data
       }
    fclose($myfile);


    $con = mysqli_connect($db_cred[0],$db_cred[1],$db_cred[2],$db_cred[3]);

 $var="select ulastdatetime from userattedancelog where orgid=1 ";
 $result = $con->query($var);
 $row = $result->fetch_assoc();
 echo $row["ulastdatetime"];
 $dat='#'. $row["ulastdatetime"].'#';

 //closing databse connection
 $con->close();