使用php [关闭]从网页上的数据库显示信息

Trying to get information from my database displayed in a webpage. Here is my code:

<body>
<?php
$con=mysqli_connect("localhost", "root", "");
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

/*
$db_selected = mysqli_select_db("music" , $con);
*/

$strSQL = "SELECT * FROM music";

$rs = mysqli_query($strSQL, $con);

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 />";

  }
?> 

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));

You didn't really ask for something, but the answer to your question is:

change this line to:

$con=mysqli_connect("localhost", "root", "","music"); // If the db is called music

and you need to put this line/variables in another direction:

$rs = mysqli_query($strSQL, $con);

needs to be:

$rs = mysqli_query($con, $strSQL);

And put the correct table in your select statement, then it will work.