将SQL数据库中的数据显示到php / html表中

时间:2017-11-12 17:21:57

标签: php html mysql phpmyadmin

我一直试图模仿这个link中的解决方案但是没有成功。我的目的是从SQL数据库导入数据并将它们放入html表中,以便我可以使用CSS来修改表的外观。有人可以帮帮我吗?

谢谢。

代码

<?php

$mysqli = NEW mysqli('localhost','username','password','database');

require('/home/database/public_html/wp-load.php');
$id = get_the_ID();

$resultSet = $mysqli->query("SELECT * FROM sweepstake_data WHERE item_id = $id");

if($resultSet->num_rows !=0){

echo "<table>"; // start a table tag in the HTML

    while($rows = $resultSet->fetch_assoc())
    {
        $description = $rows['description'];
        $links = $rows['links'];
        $category = $rows['category'];
        $eligibility = $rows['eligibility'];
        $start_date = $rows['start_date'];
        $end_date = $rows['end_date'];
        $entry_frequency = $rows['entry_frequency'];
        $prizes = $rows['prizes'];
        $victory_prizes = $rows['victory_prizes'];
        $additional_comments = $rows['additional_comments'];

        "<tr><td>" . echo $description != "" ? "<p>Name: $description<br />" : "" ;
        "<tr><td>" . echo $links != "" ? "Link: <a href=$links>Click here</a> <br />" : "" ;
        "<tr><td>" . echo $category != "" ? "Category: $category<br />" : "" ;
        "<tr><td>" . echo $eligibility != "" ? "Eligibility: $eligibility<br />" : "" ;
        "<tr><td>" . echo $start_date != "" ? "Start date:$start_date<br />" : "" ;
        "<tr><td>" . echo $end_date != "" ? "End date: $end_date<br />" : "" ;
        "<tr><td>" . echo $entry_frequency != "" ? "Entry frequency: $entry_frequency<br />" : "" ;
        "<tr><td>" . echo $prizes != "" ? "Prizes: $prizes<br />" : "" ;
        "<tr><td>" . echo $victory_prizes != "" ? "Victory prizes: $victory_prizes<br />" : "" ;
        "<tr><td>" . echo $additional_comments != "" ? "Additional comments: $additional_comments<br />" : "" ;

    }

echo "</table>"; //Close the table in HTML

}else {
    echo "No results.";
}

?>

2 个答案:

答案 0 :(得分:0)

使用以下代码。您的问题是打印值

<?php

$mysqli = NEW mysqli('localhost','username','password','database');

require('/home/database/public_html/wp-load.php');
$id = get_the_ID();

$resultSet = $mysqli->query("SELECT * FROM sweepstake_data WHERE item_id = $id");

if($resultSet->num_rows !=0){

echo "<table>"; // start a table tag in the HTML

    while($rows = $resultSet->fetch_assoc())
    {
        $description = $rows['description'];
        $links = $rows['links'];
        $category = $rows['category'];
        $eligibility = $rows['eligibility'];
        $start_date = $rows['start_date'];
        $end_date = $rows['end_date'];
        $entry_frequency = $rows['entry_frequency'];
        $prizes = $rows['prizes'];
        $victory_prizes = $rows['victory_prizes'];
        $additional_comments = $rows['additional_comments'];

        echo   "<tr><td>";
        $description != "" ? "<p>Name: $description<br />" : "" ;
         echo "<tr><td>";
        echo $links != "" ? "Link: <a href=$links>Click here</a> <br />" : "" ;
        echo "<tr><td>";
        echo $category != "" ? "Category: $category<br />" : "" ;
         echo "<tr><td>";
         echo $eligibility != "" ? "Eligibility: $eligibility<br />" : "" ;
        echo "<tr><td>" ;
         echo $start_date != "" ? "Start date:$start_date<br />" : "" ;
        echo "<tr><td>" ;
        echo $end_date != "" ? "End date: $end_date<br />" : "" ;
        echo "<tr><td>";
        echo $entry_frequency != "" ? "Entry frequency: $entry_frequency<br />" : "" ;
       echo  "<tr><td>";
       echo $prizes != "" ? "Prizes: $prizes<br />" : "" ;
        echo "<tr><td>";
        echo $victory_prizes != "" ? "Victory prizes: $victory_prizes<br />" : "" ;
        echo "<tr><td>";
        echo $additional_comments != "" ? "Additional comments: $additional_comments<br />" : "" ;

    }

echo "</table>"; //Close the table in HTML

}else {
    echo "No results.";
}

?>

答案 1 :(得分:0)

使用准备好的查询代替正常。

此代码可以使用:

<?php

$mysqli = NEW mysqli('localhost','username','password','database');

require('/home/database/public_html/wp-load.php');
$id = get_the_ID();

$resultSet = $mysqli->prepare("SELECT * FROM sweepstake_data WHERE item_id=?");
$resultSet->bind_param('i',$id);
$resultSet->execute();
$result = $resultSet->get_result();


if($result->num_rows !=0){

echo "<table>"; // start a table tag in the HTML

while($rows = $result->fetch_assoc())
{
    $description = $rows['description'];
    $links = $rows['links'];
    $category = $rows['category'];
    $eligibility = $rows['eligibility'];
    $start_date = $rows['start_date'];
    $end_date = $rows['end_date'];
    $entry_frequency = $rows['entry_frequency'];
    $prizes = $rows['prizes'];
    $victory_prizes = $rows['victory_prizes'];
    $additional_comments = $rows['additional_comments'];

    echo   "<tr><td>";
    $description != "" ? "<p>Name: $description<br />" : "" ;
     echo "<tr><td>";
    echo $links != "" ? "Link: <a href=$links>Click here</a> <br />" : "" ;
    echo "<tr><td>";
    echo $category != "" ? "Category: $category<br />" : "" ;
     echo "<tr><td>";
     echo $eligibility != "" ? "Eligibility: $eligibility<br />" : "" ;
    echo "<tr><td>" ;
     echo $start_date != "" ? "Start date:$start_date<br />" : "" ;
    echo "<tr><td>" ;
    echo $end_date != "" ? "End date: $end_date<br />" : "" ;
    echo "<tr><td>";
    echo $entry_frequency != "" ? "Entry frequency: $entry_frequency<br />" : "" ;
   echo  "<tr><td>";
   echo $prizes != "" ? "Prizes: $prizes<br />" : "" ;
    echo "<tr><td>";
    echo $victory_prizes != "" ? "Victory prizes: $victory_prizes<br />" : "" ;
    echo "<tr><td>";
    echo $additional_comments != "" ? "Additional comments: $additional_comments<br />" : "" ;

}

echo "</table>"; //Close the table in HTML

}else {
echo "No results.";
}

?>