0

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?

Naveed Ramzan
  • 3,565
  • 3
  • 25
  • 30
d_rooney
  • 89
  • 2
  • 15

2 Answers2

3

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.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • *palm face* That did it, thank you. The little things... – d_rooney Aug 01 '18 at 22:22
  • @d_rooney You're starting in php/mysql, and that's what we're here for, to outline where you made mistakes. I'm glad I was of help and to (hopefully) steered you in the right direction :-) *Cheers* – Funk Forty Niner Aug 01 '18 at 22:23
-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();
?>
Pray
  • 16
  • 1
  • 1
    This doesn't answer the question. – Funk Forty Niner Aug 01 '18 at 21:51
  • No it doesn't @FunkFortyNiner atleast this code looks like it does not have syntax errors. – Raymond Nijland Aug 01 '18 at 21:54
  • 1
    @RaymondNijland Oh, it will work for sure. It's just that when someone posts code and has errors, it's usually best to outline what was wrong to start with and how to fix it. – Funk Forty Niner Aug 01 '18 at 21:56
  • 1
    @RaymondNijland However and if I had a vote left, I would have upvoted Pray's answer, since it does cover the result set issue. – Funk Forty Niner Aug 01 '18 at 21:57
  • 1
    "it's usually best to outline what was wrong to start with and how to fix it" Yes i agree this post is a 'try' this post without explaining @FunkFortyNiner because of that this question could be selected for very low quality.. If the post contains atleast some explainment the post would be better. – Raymond Nijland Aug 01 '18 at 22:00
  • 1
    Right you are @Raymond - The answer given here could be improved. I'd have no problem upvoting if they decide to do so. – Funk Forty Niner Aug 01 '18 at 22:07