【问题标题】:PHP output script just putting 'row' instead of mysql rows?PHP 输出脚本只放置“行”而不是 mysql 行?
【发布时间】:2015-07-13 07:29:37
【问题描述】:

这是我打印东西的脚本,所以我可以列个清单:

output.php = 这应该从数据库中获取“名称”列并回显包含“名称”列的表。然后它将尝试使用 filename.php 过滤“名称”​​行。

<?php
include 'go.php';
$sql = "SELECT Name FROM utf";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     echo "<div class=\"CSSTableGenerator style=\"width:600px;height:150px;\"><table><tr><th>Name</th></tr></div>";
    // output data of each row
    while($row = $result->fetch_assoc()) {
        include 'filename2.php';
        echo "<tr><td>".$pics."</td></tr>";
    }
    echo "</table>";
} else {
    echo "0 results";
}
$conn->close();
?>

我的 filename2.php:这应该需要 seo function 并将行转换为我可以输出的内容。我感觉问题出在这里?

<?php

    require_once ('seo.php');
    $name= $row["Price"];
    $pics = seo ($name);

?>

我的 seo.php:这会删除所有的空格和坏字符并优化它们的名称:

<?php
function seo($string){
    $string = str_replace(array('[\', \']'), '', $string);
    $string = preg_replace('/\[.*\]/U', '', $string);
    $string = preg_replace('/&(amp;)?#?[a-z0-9]+;/i', '-', $string);
    $string = htmlentities($string, ENT_COMPAT, 'utf-8');
    $string = preg_replace('/&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig|quot|rsquo);/i', '\\1', $string );
    $string = preg_replace(array('/[^a-z0-9]/i', '/[-]+/') , '-', $string);
    return strtolower(trim($string, '-'));
}

?>

输出只是

没有别的了。我在这里做错了什么?


在一点不傻之后修复了代码:

<?php

include 'go.php';
include 'seo.php';
$sql = "SELECT Name FROM utf";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     echo "<div class=\"CSSTableGenerator style=\"width:600px;height:150px;\"><table><tr><th>Name</th></tr></div>";
    // output data of each row
    while($row = $result->fetch_assoc()) {
            $name = ('$row["Name"]');
            $seo2 = seo ($row["Name"]);
            $ext = '.jpg';
            $pics = $seo2 . $ext ;
        echo "<tr><td>".$pics."</td></tr>";
    }
    echo "</table>";
} else {
    echo "0 results";
}
$conn->close();
?>

不要喝酒/吸烟并尝试编码!

【问题讨论】:

  • 我不明白 - 如果您只选择名称,为什么要从结果中获取价格?
  • 您在循环中回显$pics,而不是$row,您在期待什么?
  • 先尝试统一这些文件。在具有require_once 的脚本上使用include 也是没有意义的。尝试将文件转换为output.php 中的函数。

标签: php mysql


【解决方案1】:

我做错了什么

几乎所有东西。

  1. 没有$row["Price"]; 这样的东西,因为您不选择Price
  2. 那个奇怪的包含调用是怎么回事?每次迭代都会重复。
  3. $pics 始终为空,因此每次迭代都会得到空白行。

为什么$pics 总是空的?因为

$pics = seo ($name); // $name is blank, because of bullet 1.

【讨论】:

  • 感谢您的建议!我是初学者,所以放轻松 :) 我修正了我的代码:&lt;?php require_once ('seo.php'); $seo2 = seo ($row["Name"]); $ext = '.jpg'; $pics = $seo2 . $ext ; ?&gt;,它现在可以工作了。你对 include 调用有什么建议?
  • 只在主文件中包含 seo.php 一次。然后在你的循环中操作这些变量,不需要 filename2.php
【解决方案2】:

您没有从查询中选择“价格”..

【讨论】:

    猜你喜欢
    • 2014-04-29
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    • 2010-12-30
    • 1970-01-01
    • 2016-10-02
    • 2023-04-07
    • 1970-01-01
    相关资源
    最近更新 更多