PHP通知,返回“;“数组”;而不是价值观
终于第一次跳进了一些PHP,我写了这个程序,我被卡住了。我找遍了整个地方大约两个小时,想找到一个解决办法 基本上,我连接到本地数据库,试图从我的“songs”表中获取所有行,并按它们的名称显示它们。我没有得到它们的名字,而是得到一个通知,上面写着“注意:第47行C:\xampp\htdocs\musiclibrary\index.php中的数组到字符串转换” 我当前的输出如下所示:PHP通知,返回“;“数组”;而不是价值观,php,mysql,arrays,Php,Mysql,Arrays,终于第一次跳进了一些PHP,我写了这个程序,我被卡住了。我找遍了整个地方大约两个小时,想找到一个解决办法 基本上,我连接到本地数据库,试图从我的“songs”表中获取所有行,并按它们的名称显示它们。我没有得到它们的名字,而是得到一个通知,上面写着“注意:第47行C:\xampp\htdocs\musiclibrary\index.php中的数组到字符串转换” 我当前的输出如下所示: Title Artist Genre Array Array Array Array Array
Title Artist Genre
Array Array Array
Array Array Array
Array
然后我的代码是
<?php
// Require configuration file
require_once 'config.php';
// Connect to the database
$db_server = mysqli_connect($db_hostname, $db_username, $db_password);
// Check for database connection error
if(!$db_server)
{
die("Unable to connect to MySQL: " . mysql_error());
}
// Select a database
// The mysqli_select_db() function is used to change the default database for the connection.
mysqli_select_db($db_server, $db_database);
$prompt = array('Story title', 'Time', 'Person');
$prompt = array('Story title', 'Time', 'Person');
// Page title
echo "<h1>My Music Collection</h1>";
// Get music collection
$query = "SELECT * FROM songs";
$result = mysqli_query($db_server, $query);
$rows = mysqli_num_rows($result);
// If rows exist
if($rows > 0)
{
// Create HTML table
echo "<table>";
echo "<tr><th>Title</th><th>Artist</th><th>Genre</th></tr>";
// Loop through each row in the database table
for($j = 0; $j < $rows; $j++)
{
// Build HTML table row
//PROBLEM LIES HERE ON THESE MYSQL_FETCH_ASSOC PARTS
echo "<tr>";
echo "<td>" . mysqli_fetch_assoc($result,$j,'title') . "</td>";
echo "<td>" . mysqli_fetch_assoc($result,$j,'artist') . "</td>";
echo "<td>" . mysqli_fetch_assoc($result,$j,'genre') . "</td>";
echo "</tr>";
}
echo "</table>";
}
// If there are no songs in the database table
else
{
echo "There are currently no songs on file.";
}
?>
有什么解决方案可以输出数据库中的行名称吗?谢谢 使用下面的代码替换您的代码
$values=mysqli\u fetch\u assoc($result)代码>
echo”“代码>
echo”“$值['title']。"";代码>
echo”“$值['artist']。"";代码>
echo”“$值['genre')]。"";代码>
使用下面的代码替换您的代码
$values=mysqli\u fetch\u assoc($result)代码>
echo”“代码>
echo”“$值['title']。"";代码>
echo”“$值['artist']。"";代码>
echo”“$值['genre')]。"";代码>
if($rows > 0){
echo "<table>";
echo "<tr><th>Title</th><th>Artist</th><th>Genre</th></tr>";
// Loop through each row in the database table
while($row = $result->mysqli_fetch_assoc()){
echo "<tr>";
foreach($row as $key => $value){
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['artist'] . "</td>";
echo "<td>" . $row['genre'] . "</td>";
}
echo "</tr>";
}
echo "</table>";
}
if($rows>0){
回声“;
呼应“TitleArtistGenre”;
//循环遍历数据库表中的每一行
而($row=$result->mysqli\u fetch\u assoc()){
回声“;
foreach($key=>$value的行){
回显“$row['title']”;
回显“$row['artist']”;
回显“$row['genre']”;
}
回声“;
}
回声“;
}
希望有帮助 您需要循环mysqli\u fetch\u assoc函数对行进行计数,然后循环行结果以获取值
代码如下:
if($rows > 0){
echo "<table>";
echo "<tr><th>Title</th><th>Artist</th><th>Genre</th></tr>";
// Loop through each row in the database table
while($row = $result->mysqli_fetch_assoc()){
echo "<tr>";
foreach($row as $key => $value){
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['artist'] . "</td>";
echo "<td>" . $row['genre'] . "</td>";
}
echo "</tr>";
}
echo "</table>";
}
if($rows>0){
回声“;
呼应“TitleArtistGenre”;
//循环遍历数据库表中的每一行
而($row=$result->mysqli\u fetch\u assoc()){
回声“;
foreach($key=>$value的行){
回显“$row['title']”;
回显“$row['artist']”;
回显“$row['genre']”;
}
回声“;
}
回声“;
}
希望有帮助 mysqli\u fetch\u assoc()函数只接受一个参数
数组mysqli_fetch_assoc(mysqli_result$result)
正确的方法是:
$query = "SELECT * FROM songs";
if ($result = mysqli_query($db_server, $query)) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['artist'] . "</td>";
echo "<td>" . $row['genre'] . "</td>";
echo "</tr>";
}
mysqli_free_result($result);
}
$query=“从歌曲中选择*”;
如果($result=mysqli\u query($db\u server,$query)){
while($row=mysqli\u fetch\u assoc($result)){
回声“;
回显“$row['title']”;
回显“$row['artist']”;
回显“$row['genre']”;
回声“;
}
mysqli_免费_结果($result);
}
mysqli\u fetch\u assoc()函数只接受一个参数
数组mysqli_fetch_assoc(mysqli_result$result)
正确的方法是:
$query = "SELECT * FROM songs";
if ($result = mysqli_query($db_server, $query)) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['artist'] . "</td>";
echo "<td>" . $row['genre'] . "</td>";
echo "</tr>";
}
mysqli_free_result($result);
}
$query=“从歌曲中选择*”;
如果($result=mysqli\u query($db\u server,$query)){
while($row=mysqli\u fetch\u assoc($result)){
回声“;
回显“$row['title']”;
回显“$row['artist']”;
回显“$row['genre']”;
回声“;
}
mysqli_免费_结果($result);
}
mysqli\u fetch\u assoc
本身返回一个数组,其中包含来自查询的数据。您将要提取它。我在哪里可以学习提取它?mysqli\u fetch\u assoc()将结果作为关联数组返回。打印的方式不正确。您应该首先将mysqli_fetch_assoc()分配给一个变量,并使用数组键打印值。mysqli_fetch_assoc
本身返回一个数组,其中包含来自查询的数据。您将要提取它。我在哪里可以学习提取它?mysqli\u fetch\u assoc()将结果作为关联数组返回。打印的方式不正确。您应该首先将mysqli_fetch_assoc()分配给一个变量,并使用数组键打印值。