gpt4 book ai didi

PHP 注意,返回 "array array array"而不是值

转载 作者:行者123 更新时间:2023-11-29 06:48:45 25 4
gpt4 key购买 nike

终于第一次接触 PHP,我编写了这个程序,但我陷入了困境。我到处搜索了大约2个小时才找到解决方案。

基本上,我正在连接到本地数据库并尝试从“歌曲”表中获取所有行,并按名称显示它们。我没有得到他们的名字,而是收到一条通知,上面写着“注意:第 47 行 C:\xampp\htdocs\musiclibrary\index.php 中的数组到字符串转换”

我当前的输出如下所示:

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.";
}

?>

有什么解决方案可以输出数据库中的行名称吗?谢谢!

最佳答案

使用下面的代码替换您的代码

  1. 首先将值提取到数组中
  2. 显示表格行中的值

    $values = mysqli_fetch_assoc($result);

    echo "<tr>";

    echo "<td>" . $values ['title']. "</td>";

    echo "<td>" . $values ['artist'] . "</td>";

    echo "<td>" . $values ['genre')]. "</td>";

关于PHP 注意,返回 "array array array"而不是值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48393923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com