MySQL Question

cant show the results of query in php from mysql

Hello I am new at

php
and
mysql
and I don't know what is wrong.

I cant show the results from query and the connection with
mysql
is successfully connected.

I don't use
wampserver
I just install
php
,
mysql
and
Apache
separately.

Thanks in advance.

Code

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$servername = "localhost";
$username = "andreas";
$password = "Xatzios1#";

// Create connection
$conn = mysqli_connect($servername, $username, $password);

// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";


$sql="select * from `books`;";
$result=mysqli_query($conn,$sql);
if (!$result){
echo "query cannot execute";
};

?>


its only show me "query cannot execute"

Answer

You need to pass fourth parameter database name in mysqli_connect()

It would be

$conn = mysqli_connect($servername, $username, $password,"YOUR_DATABASE");

Read http://php.net/manual/en/mysqli.error.php to check error in query.

Read http://php.net/manual/en/mysqli-result.fetch-array.php To fetch data from query result

Comments