0

It appears that I am connecting to a MySQL database but unable able to return a query using mysqli class. I've done some searches and what I have coded should return the results, but I am missing something.

   <?php

set_include_path('/Applications/MAMP/db/mysql57');

$host = "127.0.0.1";
$user = "admin";
$password = " ";
$bookDatbase = "Books";

$mysqli = new mysqli($host,$user, $password, $bookDatabase);

if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

$result = $mysqli->query("SELECT Title FROM Book");

echo $result;
2
  • 2
    What else does it return if not the results? Commented Feb 21, 2019 at 12:56
  • Hi, you have typo in $bookDatbase/$bookDatabase Commented Feb 21, 2019 at 13:02

2 Answers 2

1
$conn = new mysqli("localhost","username","password","Books");

$result = $conn->query("SELECT Title FROM Book");

if(mysqli_error($conn)){
    echo mysqli_error($conn);
}
else{

    $titles = [];

    while ($r = mysqli_fetch_assoc($result)){
        $titles[] = $r;
    }

    echo "<pre>";
    print_r($titles);
    echo "</pre>";
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yes!!! That worked Thank you! I see that I needed to pass the values to an array. Also, because of how you designed the code I was able to see yet another typo in the query. It should have been "Books" and not "Book"
I am glad it helped you :)
0

You need to pass mysqli object as well as below:

$result = mysqli_query($mysqli, "SELECT Title FROM Book");

Hope it helps you.

1 Comment

The second $mysqli is reduntant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.