警告:mysqli_connect():( HY000 / 2002):php_network_getaddresses:getaddrinfo failed:没有这样的主机是已知的

时间:2015-01-29 14:17:48

标签: php

我有代码我试图将从两个传感器读取的数据上传到arduino到WAMP服务器但是当运行localhost \ project时,我得到那个Warning.THE php文件位于projects目录中.Connect.php在访问数据库时使用Connect.php代码,这里主要用于连接,add.php处理来自arduino.index.php的POST显示表中的传感器值:

以下是Connect.php

****<?php
    function Connection(){
        $server="server";
        $user="user";
        $pass="pass";
        $db="database";

        $connection = mysqli_connect($server, $user, $pass);
        //$db = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'user', 'pass');
        if (!$connection) {
            die('MySQL ERROR: ' . mysql_error());
        }

        mysql_select_db($db) or die( 'MySQL ERROR: '. mysql_error() );
        return $connection;
    }
?>****

以下是add.php:

**<?php
    include("connect.php");

    $link=Connection();
    $temp1=$_POST["temp1"];
    $hum1=$_POST["hum1"];
    $query = "INSERT INTO `tempLog` (`temperature`, `humidity`) 
        VALUES ('".$temp1."','".$hum1."')"; 

    mysql_query($query,$link);
    mysql_close($link);
    header("Location: index.php");
?>**

最后是index.php:

**<?php
    include("connect.php");     

    $link=Connection();
    $result=mysql_query("SELECT * FROM `tempLog` ORDER BY `timeStamp` DESC",$link);
?>
<html>
   <head>
      <title>Sensor Data</title>
   </head>
<body>
   <h1>Temperature / moisture sensor readings</h1>
   <table border="1" cellspacing="1" cellpadding="1">
        <tr>
            <td>&nbsp;Timestamp&nbsp;</td>
            <td>&nbsp;Temperature 1&nbsp;</td>
            <td>&nbsp;Moisture 1&nbsp;</td>
        </tr>
      <?php 
          if($result!==FALSE){
             while($row = mysql_fetch_array($result)) {
                printf("<tr><td> &nbsp;%s </td><td> &nbsp;%s&nbsp; </td><td> &nbsp;%s&nbsp; </td></tr>", 
                   $row["timeStamp"], $row["temperature"], $row["humidity"]);
             }
             mysql_free_result($result);
             mysql_close();
          }
      ?>
   </table>
</body>
</html>**

2 个答案:

答案 0 :(得分:0)

尝试使用mysqli_select_db和正确的参数,如下所示

$connection = mysqli_connect($server, $user, $pass);
if (!$connection) {
    die('MySQL ERROR: ' . mysql_error());
}

mysql_select_db($connection,$db) or die( 'MySQL ERROR: '. mysql_error() );
return $connection;

或者使用mysqli_connect()通过将其添加为第4个参数来选择数据库,然后您可以完全忘记mysql_select_db()

$connection = mysqli_connect($server, $user, $pass, $db);
if (!$connection) {
    die('MySQL ERROR: ' . mysql_error());
}

答案 1 :(得分:0)

你正在mysqli_mysql_(这不是一回事......)

Connection返回mysqli_连接:

$connect = mysqli_connect($server, $user, $pass);

但您尝试使用它来通过mysql_选择数据库并进行查询:

mysql_select_db($db) or die( 'MySQL ERROR: '. mysql_error() );

此外,使用mysqli_连接作为mysql_查询的链接毫无意义:

$link=Connection();
$result=mysql_query("SELECT * FROM `tempLog` ORDER BY `timeStamp` DESC",$link);

请稍后查看mysqli docs并抓取mysql_代码。 - 或者甚至更好,使用PDO