Code or server not working

Hi all,

I’m following an exercise in the book PHP for Absolute Begginers by Lengstorf. We are supposed to build a simple interface and connect to the database to get a result. Here is the code (which is giving me a blank screen on my localhost:8888/test page):

<?php if($_SERVER['REQUEST_METHOD']=='POST') { //open a mysql connection $link = new mysqli('localhost', 'root', 'test'); if(!$link) { die('Connection failed: '.$mysqli->error()); } //create and execute a mysql query $sql = "SELECT album_name FROM albums WHERE artist_id=?"; if($stmt = $link->prepare($sql)) { $stmt->bind_param('i', $_POST['artist']); $stmt->execute(); $stmt->bind_result($album); while($stmt->fetch()) { printf("Album: %s
", $album); } $stmt->close(); } //close the connection $link->close(); } else { ?> Bon Iver Feist <?php } //end else ?>

Why is this not working? I’m on a Mac using MAMP for my server. My MAMP localhost home page suggests using the following to make a db connection, which contradicts the book. I tried it as well and got no result:

$user = ‘root’;
$password = ‘root’;
$db = ‘inventory’;
$host = ‘localhost’;
$port = 8889;

$link = mysql_connect(
“$host:$port”,
$user,
$password
);
$db_selected = mysql_select_db(
$db,
$link
);

If you know what’s going wrong, please let me know. Thank you.

First thing to do is turn error reporting on,

Add this under the first opening php tag,

[php]error_reporting(E_ALL);
ini_set(‘display_errors’, 1);[/php]

Your book is better, the mysql_* api has been removed from PHP, and using the questionmark(s) in the query and binding the variables is the proper way to avoid sql injection hacks.

You should set PHP to display all errors in your dev environment. Add this to the beginning of the script

[php]error_reporting(E_ALL);
ini_set(‘display_errors’, 1);
[/php]

The white page is usually caused by a «500 internal server error»

Yes, I get the following:

Warning: mysqli::__construct(): (HY000/1045): Access denied for user ‘root’@‘localhost’ (using password: YES) in /Applications/MAMP/htdocs/test.php on line 10

Warning: mysqli::prepare(): Couldn’t fetch mysqli in /Applications/MAMP/htdocs/test.php on line 19

Warning: mysqli::close(): Couldn’t fetch mysqli in /Applications/MAMP/htdocs/test.php on line 31

Now I really screwed it up! I saw that the error read “using password” so I went to the phpMyAdmin and changed settings to “No password”. Now the localhost is showing the form, but when I try to go to phpMyAdmin again, it locks me out saying:

"Error. Cannot connect. Invalid settings:
phpMyAdmin tried to connect to the MySQL server, and the server rejected the connection. You should check the host, username and password in your configuration and make sure that they correspond to the information given by the administrator of the MySQL server. "

and when I try to use the form, i get the same error message as prior post.

I fixed the above password issue by reloading MAMP, but the original error message stands.

Well, is the password for root, root? The error is saying your credentials are wrong.

According to MAMP, yes the password for root is root. I don’t know if that is represented in the code though. I see this line 10 from the book:

$link = new mysqli(‘localhost’, ‘root’, ‘test’);

but it was written for XAMPP. I assume I need another line of code that provides credentials, or I need to change the server settings.

You need to change test, to root.

“test” is the db name, so I added “root” like you said, but in-between, and it worked. Like this:
$link = new mysqli(‘localhost’, ‘root’, ‘root’, ‘test’);

Thanks for your help!

So, your password was wrong. The order is: connection, username, password, database name

Sponsor our Newsletter | Privacy Policy | Terms of Service