【问题标题】:Array to string conversion and Undefined variable in PHP (When used in HTML)PHP中的数组到字符串的转换和未定义的变量(在HTML中使用时)
【发布时间】:2018-03-06 16:25:57
【问题描述】:

我一直在用 HTML 编写 PHP 的代码 sn-p,它在另一个数组中迭代一个数组(来自 mysql 的数据)。但是当我运行代码时,它给出了以下两个错误。

未定义的变量 数组到字符串的转换...... 以下是我的代码。

$sql = "SELECT * FROM person";
$result = mysqli_query($conn, $sql);
$resultDataSet = array();
if (mysqli_num_rows($result) > 0) {

while ($row = mysqli_fetch_array($result)) {
    array_push($resultDataSet, $row);
}
if ($type == "HTML") {
    $htmlSerialize = "
        <table>
            <tr>
                <th>Name</th>
                <th>Age</th>
                <th>City</th>
            </tr>
            <?php foreach($resultDataSet as $value): ?>
             <tr>
                <th><?php=?".$value['Name']." ?></th>
                <th><?php=?".$value['Age']." ?></th>
                <th><?php=?".$value['City']." ?></th>
            </tr>
            <?php endforeach; ?>
        </table>";


    echo $htmlSerialize;

}

以下是错误。

我犯了什么错误?我该如何解决这个问题?

已编辑

以下是 $resultDataSet 的 var 转储

array (size=2)
  0 => 
    array (size=8)
      0 => string '1' (length=1)
      'ID' => string '1' (length=1)
      1 => string 'Akila' (length=5)
      'Name' => string 'Akila' (length=5)
      2 => string '22' (length=2)
      'Age' => string '22' (length=2)
      3 => string 'Mount Lavinia' (length=13)
      'City' => string 'Mount Lavinia' (length=13)
  1 => 
    array (size=8)
      0 => string '2' (length=1)
      'ID' => string '2' (length=1)
      1 => string 'Randil' (length=6)
      'Name' => string 'Randil' (length=6)
      2 => string '23' (length=2)
      'Age' => string '23' (length=2)
      3 => string 'Colombo' (length=7)
      'City' => string 'Colombo' (length=7)

【问题讨论】:

  • $value 来自哪里,包含什么内容?
  • 很难判断查询返回了什么。查看var_dump($resultDataSet) 的输出。可能是列名都是小写的。
  • @magnus 我是 PHP 初学者。 '$value' 不是 for 循环为每次迭代分配的对象吗?
  • 1.我没有看到您在任何循环中都有该代码。在这段代码中有一个循环,即在使用$value 之前要关闭的while-循环。 2. 变量名称将是您设置的名称。我建议花一些时间阅读一些基本的 PHP 教程并阅读手册。
  • @AdamAzad 我更新了问题

标签: php html mysql


【解决方案1】:

错误报告value 未定义。 PHP 不解析也不编译由引号包裹的代码——字符串。在我将循环与 HTML 输出分开的地方试试这个。

if ($type == "HTML") {

   // Start by opening the table and header
   $htmlSerialize = '
        <table>
            <tr>
                <th>Name</th>
                <th>Age</th>
                <th>City</th>
            </tr>';

    // loop over the results and append each row to $htmlSerialize
    foreach($resultDataSet as $value):

        $htmlSerialize .= '
                 <tr>
                    <th>'. $value['Name'] .'</th>
                    <th>'. $value['Age'] .'</th>
                    <th>'. $value['City'] .'</th>
                </tr>';

    endforeach;

    // close the table
    $htmlSerialize .= '</table>';

    // flush results
    echo $htmlSerialize;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-20
    • 2017-10-13
    • 2011-11-27
    • 1970-01-01
    • 1970-01-01
    • 2021-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多