从数据库中选择并打印所有表

时间:2015-09-21 17:54:06

标签: php html mysql

这是我的代码我没有错误,但我的页面上没有显示数据。一些帮助请...我的代码有什么问题?谢谢你mucH

<?php 
            if(logged_in() === true){

            $q=mysql_query("select *  from `admin` order by `id` desc");

            echo "<table cellspacing=0 cellpadding=0 border=1><tr><td>Id</td><td>User</td><td>Password</td><td>Permission</td><td>delete</td><td>edit</td></tr>";

            while($date=mysql_fetch_row($q));
            echo "<tr><td>{$date[0]}</td><td>{$date[1]}</td><td>{$date[2]}</td><td>{$date[3]}</td><form method=post action=delete.php><td><input type=hidden name=id value='{$date[0]}'><input type=submit name=sterge value=delete></td></form><form action=edit.php method=get><td><input type=hidden name=id value='{$date[0]}'><input type=submit name=edit value=edit></td></td></form></tr>";

            echo "</table>";    

            }else {

            include 'includes/widgets/home.php';
            }

 ?>

2 个答案:

答案 0 :(得分:3)

一些事情,如下面的更改,它适用于回来的数据。我伪造logged_in()函数总是返回true。

首先,你马上过了一个分号。所以这很糟糕。我将结尾</table>放在while块之外。

另外,请远离select *。拼写出列名。我建议您在结果集中使用列名而不是序号值。

此外,请转到mysqlipdo Jay在评论中为您提供了上述链接。

$dbname = 'myDB';
$dbuser = 'GuySmiley';
$dbpass = 'anchovies';
$dbhost = 'localhost';
$link = mysql_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'");
mysql_select_db($dbname) or die("Could not open the db '$dbname'");


if(logged_in() === true){

$q=mysql_query("select *  from `admin` order by `id` desc");
if($q === FALSE) { 
    die(mysql_error()); // this is for non-production code
}


echo "<table cellspacing=0 cellpadding=0 border=1><tr><td>Id</td><td>User</td><td>Password</td><td>Permission</td><td>delete</td><td>edit</td></tr>";

while($date=mysql_fetch_row($q)) 
{
    echo "<tr><td>{$date[0]}</td><td>{$date[1]}</td><td>{$date[2]}</td><td>{$date[3]}</td><form method=post action=delete.php><td><input type=hidden name=id value='{$date[0]}'><input type=submit name=sterge value=delete></td></form><form action=edit.php method=get><td><input type=hidden name=id value='{$date[0]}'><input type=submit name=edit value=edit></td></td></form></tr>";
}
echo "</table>";
}
else {
    echo "you are not logged in<br>";
}

enter image description here

因此它会输出您的数据(通过启用错误报告,开发代码会轻松很多)。

启用错误报告

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);

使用包含在异常处理

中的代码的非弃用mysql库进行开发

顺便说一下,上面测试的模式是

create table admin
(   id int auto_increment primary key,
    col1 int not null,
    col2 int not null,
    col3 int not null
);
insert admin (col1,col2,col3) values (11,111,1111),(22,222,2222);

答案 1 :(得分:1)

有点像

while($date=mysql_fetch_row($q));
       echo "<tr><td>{$date[0]}</td><td>{$date[1]}</td><td>{$date[2]}</td><td>{$date[3]}</td><form method=post action=delete.php><td><input type=hidden name=id value='{$date[0]}'><input type=submit name=sterge value=delete></td></form><form action=edit.php method=get><td><input type=hidden name=id value='{$date[0]}'><input type=submit name=edit value=edit></td></td></form></tr>";

        echo "</table>"; 

应该是

while($date=mysql_fetch_row($q)){
        echo "<tr><td>{$date[0]}</td><td>{$date[1]}</td><td>{$date[2]}</td><td>{$date[3]}</td><form method=post action=delete.php><td><input type=hidden name=id value='{$date[0]}'><input type=submit name=sterge value=delete></td></form><form action=edit.php method=get><td><input type=hidden name=id value='{$date[0]}'><input type=submit name=edit value=edit></td></td></form></tr>";

        echo "</table>"; 
}

注意分号而不是while循环的大括号