duanke3985
2015-07-13 07:29
浏览 39
已采纳

PHP输出脚本只是放'行'而不是mysql行?

This is my script for printing out stuff so I can make a list:

output.php = This is supposed to fetch the 'Name' column from the database and echo a table with the column 'Name'. Then it will try to filter the 'Name' Row with with 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();
?>

My filename2.php: This is supposed to require the seo function and translate rows into something I can output. I have a feeling the issue is here?

<?php

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

?>

My seo.php:This removes all of the spaces and bad characters and optimizes their name:

<?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, '-'));
}

?>

The out put is just

Row

Row

Row

Row

Nothing else. What am I doing wrong here?


Fixed the code after a little bit of not being stupid:

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

Don't drink/smoke and try to code !

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doushi1847 2015-07-13 07:38
    已采纳

    What am I doing wrong here

    Almost everything.

    1. There is no such thing as $row["Price"]; since you don't SELECT Price.
    2. What's up with that weird include call? Will be repeated for each iteration.
    3. $pics is always empty, hence you get blank rows for each iteration.

    Why is $pics always empty? Because

    $pics = seo ($name); // $name is blank, because of bullet 1.
    
    打赏 评论
  • doutuichan2681 2015-07-13 07:39

    You are not selecting 'Price' from your query..

    打赏 评论

相关推荐 更多相似问题