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.