skip to Main Content

Warning: mysqli::__construct(): php_network_getaddresses: getaddrinfo
failed: nodename nor servname provided, or not known in
/Users/davidrooney/Sites/conn_test.php on line 10

Warning: mysqli::__construct(): (HY000/2002):
php_network_getaddresses: getaddrinfo failed: nodename nor servname
provided, or not known in /Users/davidrooney/Sites/conn_test.php on
line 10

connection successful

Warning: mysqli::query(): Couldn’t fetch
mysqli in /Users/davidrooney/Sites/conn_test.php on line 14

hello world

Warning: mysqli::close(): Couldn’t fetch mysqli in /Users/davidrooney/Sites/conn_test.php on line 18

The following is my conn_test.php file:

<?php

define('DB_NAME','testdb');
define('DB_USER','root');
define('DB_PASSWORD','password');
define('DB_HOST','localhost');

//Connecting to sql db
$connect = new mysqli('DB_HOST','DB_USER','DB_PASSWORD','DB_NAME');
if (!$connect) { die ('could not connect'); }
echo 'connection successful'; 

$message = $connect->query("SELECT first_name FROM testtable");

echo "$message <br/>";
echo "hello world";
$connect->close();
?>

The PHP code listed is able to connect to my SQL database “testdb” but making a query on the table “testtable” throws errors.

I have a column in that table ‘first_name’ with one entry as ‘john’. I have verified my apache server is running fine with PHP7.1 enabled because I can output phpinfo() in a separate script. And MySQL server (mysql5.7) is also running and healthy because I can access it through Sequel Pro and create databases, tables, etc.

I can also manually query from Sequel Pro and ‘john’ is returned, so I know that query works as well.

I just started working with PHP but after everything I’ve read on the web, this code looks to be correct.

What am I missing?

2

Answers


  1. Try MySQLi Object Oriented

    <?php
    $servername = "localhost";
    $username = "username";
    $password = "";
    $dbname = "testdb";
    
    $connect = new mysqli($servername, $username, $password, $dbname);
    if ($connect->connect_error) {
        die("Connection failed: " . $connect->connect_error);
    }
    $sql = "SELECT first_name FROM testtable";
    $result = $connect->query($sql);
    
    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            echo "Hello " . $row["first_name"];
        }
    }
    
    $connect->close();
    ?>
    
    Login or Signup to reply.
  2. I felt that I needed to step in here, given the answers which IMHO did not answer the question in question.

    As I stated in comments, the quotes need to be removed from the database declaration.

    $connect = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    

    Since those are constants.

    References:

    Error reporting would also have been of help:

    Edit:

    As user3783243 pointed out in comments:

    Additionally when you get the connection working $message will be a result object, you will need to fetch that. – user3783243

    is right. Echoing the query is simply the result set. You need to loop over (successful) results and there are many ways to do this.

    You appear to be new to using a database. If you look through the manual inside the mysqli_query() function on PHP.net http://php.net/manual/en/mysqli.query.php, you will find many examples to fetch and show the results from the query, given that it was successful.

    If you don’t see results, then it (the query) may have failed. This is when you need to check for errors using mysqli_error($connect) on the query.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search