Reputation: 35
I'm trying to run a basic sql query that is failing, even though I can confirm that the database is connected. Using mysql_error() doesn't give an error message. I've been told that it could be an Apache permissions issue, but I'm not seeing that.
Any help or guidance is greatly appreciated.
<?php
// 1. Create a database connection
$dbhost = "localhost";
$dbname = "moneymouth";
$connection = mysqli_connect($dbhost, $dbname);
// Test if connection succeeded
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
?>
<?php
// 2. Perform database query
$query = "SELECT * FROM users";
$result = mysqli_query ($connection, $query);
if (!$result){
die ("Database query failed.");
;
}
?>
Upvotes: 0
Views: 63
Reputation: 393
Are sure about first part 'cause no error form mysql_error()
?
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "my_db_name");
Source: http://php.net/manual/en/mysqli.query.php
It seems that something is missing.
Second part:
<?php
// 2. Perform database query
$query = "SELECT * FROM users;";
$result = mysqli_query ($connection, $query);
if (!$result){
die ("Database query failed.");
}
?>
Try to put ;
at the end of query, in some cases is required and if missing it can cause error.
After die ("Database query failed.");
remove ;
it can give you an error if you forget it and write other code.
Upvotes: 1