Php 在每返回一行的新div中显示mysql数据

Php 在每返回一行的新div中显示mysql数据,php,mysql,Php,Mysql,我已经创建了以下内容,但是它没有正确显示数据,我希望数据库中的每行数据都有一个新的div <?php include_once '\inc\header.php'; $db_server = mysql_connect($db_hostname, $db_username, $db_password); if (!$db_server) die("Unable to connect to MySQL: ". mysql_error()); mysql_select_db($db_da

我已经创建了以下内容,但是它没有正确显示数据,我希望数据库中的每行数据都有一个新的div

<?php
include_once '\inc\header.php';

$db_server = mysql_connect($db_hostname, $db_username, $db_password);

if (!$db_server) die("Unable to connect to MySQL: ". mysql_error());

mysql_select_db($db_database)
or die ("Unable to select database: " . mysql_error());

$query = "select * from deal";
$result = mysql_query($query);

if (!$result) die ("Database access failed: " . mysql_error());
?>

<body class="metrouicss" onload="prettyPrint()" style="zoom: 1;">
<div id="container">

<?php
foreach($result as $tile)
    {?>
        <div class="item tile double bg-color-red">
                <?php echo $tile['name']?>
        </div>
    <?php }?>
</div>


<?php 
mysql_close ($db_server);
?>

`


试试看


试试看

mysql\u query()
返回一个结果句柄。它不是一个可以循环的数组,这意味着

foreach($result as $tile) {
这是错误的。应该是

while ($tile = mysql_fetch_assoc($result)) {
相反。

mysql\u query()
返回一个结果句柄。它不是一个可以循环的数组,这意味着

foreach($result as $tile) {
这是错误的。应该是

while ($tile = mysql_fetch_assoc($result)) {

相反。

添加
$result=mysql\u fetch\u数组($result)
下面
如果(!$result)死亡(“数据库访问失败:.mysql_error())


Mysql查询返回一个Mysql资源对象。您需要一些额外的函数,如
mysql\u fetch\u array
来处理它。

Add
$result=mysql\u fetch\u array($result)
下面
如果(!$result)死亡(“数据库访问失败:.mysql_error())


Mysql查询返回一个Mysql资源对象。你需要一些额外的函数,如代码> MySqLyFuffChyLoad 来处理它。

< P>我会考虑移到<代码> MySqLI[/COD]或<代码> PDO。无论如何,另一种方法(使用mysql)是在while循环条件下使用
$row=mysql\u fetch\u assoc($result)

<?php
include_once '\inc\header.php';

$db_server = mysql_connect($db_hostname, $db_username, $db_password);

if (!$db_server) die("Unable to connect to MySQL: ". mysql_error());

mysql_select_db($db_database)
or die ("Unable to select database: " . mysql_error());

$query = "select * from deal";
$result = mysql_query($query);

if (!$result) die ("Database access failed: " . mysql_error());
?>

<body class="metrouicss" onload="prettyPrint()" style="zoom: 1;">
<div id="container">

<?php
while($row = mysql_fetch_assoc($result))
    {?>
        <div class="item tile double bg-color-red">
                <?php echo $row['name']?>
        </div>
    <?php }?>
</div>


<?php 
mysql_close ($db_server);
?>


<代码> > p>我会考虑移到<代码> MySqLI:/COD>或<代码> PDO。无论如何,另一种方法(使用mysql)是在while循环条件下使用
$row=mysql\u fetch\u assoc($result)

<?php
include_once '\inc\header.php';

$db_server = mysql_connect($db_hostname, $db_username, $db_password);

if (!$db_server) die("Unable to connect to MySQL: ". mysql_error());

mysql_select_db($db_database)
or die ("Unable to select database: " . mysql_error());

$query = "select * from deal";
$result = mysql_query($query);

if (!$result) die ("Database access failed: " . mysql_error());
?>

<body class="metrouicss" onload="prettyPrint()" style="zoom: 1;">
<div id="container">

<?php
while($row = mysql_fetch_assoc($result))
    {?>
        <div class="item tile double bg-color-red">
                <?php echo $row['name']?>
        </div>
    <?php }?>
</div>


<?php 
mysql_close ($db_server);
?>


在哪里获取结果?执行
var\u dump($result)
并查看,如果结果包含多个值,则将您的foreach更改为,而($row=mysql\u fetch\u array($result)){..$row['name']}在if(!$result:$result=mysql\u fetch\u array($result)下面添加此行;谢谢大家的帮助,我现在就知道该怎么做了。你在哪里获取结果?执行
var\u dump($result)
并查看,如果result包含多个值,请将你的foreach更改为,而($row=mysql\u fetch\u array($result)){..$row['name']}在if(!$result:$result=mysql\u fetch\u array($result)下面添加这一行;谢谢大家的帮助,我现在知道怎么做了。不要剪切粘贴整个代码块,只更改一行,而不指明您做了什么,或者在哪里做了什么。您的更改是正确的,但由于它完全隐藏在代码中,因此毫无用处。不要剪切和粘贴整个代码块,只更改一行,而不指明您做了什么,或者在哪里做了什么。您的更改是正确的,但由于它完全隐藏在代码中,因此毫无用处。