Using mysqli_
, the connection comes first for everything. Whereas the connection comes as the last parameter with mysql_
functions and you may have confused those by having used mysql_
functions in the past and have decided to start using mysqli_
now.
Let's start with where you've commented out:
/*
$db_selected = mysqli_select_db("music" , $con);
*/
Either you change that to:
$db_selected = mysqli_select_db($con, "music");
Or change:
$con=mysqli_connect("localhost", "root", "");
to:
$con=mysqli_connect("localhost", "root", "", "music");
as I've made it that way below, using all four parameters in one line.
Important sidenote:
Make sure that both database and table are indeed called music
.
If your database is a different name than music
, then that's what you need to use for the fourth parameter and not the table name.
<body>
<?php
$con=mysqli_connect("localhost", "root", "", "music");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$strSQL = "SELECT * FROM music";
$rs = mysqli_query($con, $strSQL);
while($row = mysqli_fetch_array($rs, MYSQLI_BOTH)) {
// Write the value of the column FirstName (which is now in the array $row)
echo $row['artist'] . "<br />";
echo $row['title'] . "<br />";
echo $row['format'] . "<br />";
echo $row['notes'] . "<br />";
}
?>
Having used or die(mysqli_error($con))
to mysqli_query()
would have signaled the error.
More precisely:
$rs = mysqli_query($con, $strSQL) or die(mysqli_error($con));