简体   繁体   English

将数据库信息动态加载到 html/php 页面中

[英]Dynamically loading database information into a html/php page

EDIT: My issue was using deprecated functions with an updated PHP version but for anyone trying the same thing with PHP 5.5 or lower follow the accepted answer编辑:我的问题是在更新的 PHP 版本中使用已弃用的函数,但对于使用 PHP 5.5 或更低版本尝试相同操作的任何人,请遵循已接受的答案

I'm looking to be able to fill a table with information from my database.我希望能够用我的数据库中的信息填充表格。 I am fairly new to PHP web development and I am kinda stuck here at the moment我对 PHP Web 开发还很陌生,目前我有点卡在这里

loader.php加载器.php

<?php   
$db = mysql_connect("localhost", "root", "") or die("Couldn't Connect");

mysql_select_db("dbName") or die("Couldn't find database");

$query = "SELECT * from dbTable";

    while($row = mysql_fetch_assoc($query))
    {
        $value1 = $row['value1'];
        $value2 = $row['value2'];
        $value3 = $row['value3'];
        $value4 = $row['value4'];

        $values = "<tr><td>".$value1."</td><td>".$value2."</td><td>".$value3."</td><td>".$value4."</td></tr>";
    }
?>

page.php页面.php

<?php
    require 'loader.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<h1>Title</h1>

<table>
    <tr>
        <th>1st Value</th>
        <th>2nd Value</th>
        <th>3rd Value</th>
        <th>4th Value</th>
    </tr>
        <?php $values ?>
</table>

</body>
</html>

I need to be able to load the $values data into below the heading inside the table in page.php.我需要能够将 $values 数据加载到 page.php 中表格内的标题下方。 It seems like this should work but something is not correct.看起来这应该有效,但有些地方不正确。

If anyone could give me some insight to why this isn't working, it would be greatly appreciated.如果有人能让我了解为什么这不起作用,我将不胜感激。

John约翰

Oh boy!好家伙! Ok, if you are new to php, then this is a good chance to learn some good practices first.好的,如果您是 php 新手,那么这是一个先学习一些好的实践的好机会。 First of all, the php code you have written is deprecated.首先,您编写的php代码已被弃用。 Use PDO instead.请改用 PDO。 Here is a good tutorial .这是一个很好的教程

Secondly make use of a framework but this is not necessary.其次使用框架,但这不是必需的。 It basically comes with lot of good features.它基本上带有很多好的功能。 So in the long run, you will end of writing less code and its easy to maintain.因此,从长远来看,您将编写更少的代码并且易于维护。

Thirdly as others have suggested, now you know what is the error in your code.第三,正如其他人所建议的那样,现在您知道代码中的错误是什么。

EDIT:编辑:

In your case, please change the code to this:在您的情况下,请将代码更改为:

$db = mysql_connect("localhost", "root", "") or die("Couldn't Connect");

mysql_select_db("dbName") or die("Couldn't find database");

//new edit below
$query = mysql_query("SELECT * from dbTable");
$values = "";
while($row = mysql_fetch_assoc($query))
{
    $value1 = $row['value1'];
    $value2 = $row['value2'];
    $value3 = $row['value3'];
    $value4 = $row['value4'];

    //notice the '.' below.
    $values .= "<tr><td>".$value1."</td><td>".$value2."</td><td>".$value3."</td><td>".$value4."</td></tr>";
}

您需要更改 $values 以在 page.php 中回显 $values

If you want to display a variable $values, you need to use echo.如果要显示变量 $values,则需要使用 echo。

 <?php echo $values ?>

EDIT编辑

You need add: $query = mysql_query($query);您需要添加: $query = mysql_query($query); and edit sql + add $values.并编辑 sql + 添加 $values。 Result:结果:

$db = mysql_connect("localhost", "root", "") or die("Couldn't Connect");

mysql_select_db("dbName") or die("Couldn't find database");

$query = "SELECT * from `dbTable`";
$query = mysql_query($query);
$values = '';

while($row = mysql_fetch_assoc($query))
{
    $value1 = $row['value1'];
    $value2 = $row['value2'];
    $value3 = $row['value3'];
    $value4 = $row['value4'];

    $values .= "<tr><td>".$value1."</td><td>".$value2."</td><td>".$value3."</td><td>".$value4."</td></tr>";
}

Try this.尝试这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM