mysqli_fetch_assoc在屏幕上不返回任何内容

时间:2014-07-14 06:06:37

标签: php mysql mysqli

我正面临着mysql_fetch_assoc函数的问题,我需要使用php mysql_fetch_assoc函数获取数据库中的数据,但它不返回任何内容(空白页),下面是我的代码:

功能:

function confirm_query($watever){
    global $connection;
    if (!$watever) {
        die("Database query failed! " . mysqli_error($connection));
    }

}


 function find_admin(){
    global $connection;

    $query  = "select * from admins ";
    $query .= "order by username asc";

    $admin_set = mysqli_query($connection, $query);
    confirm_query($admin_set);
    return $admin_set;

}

管理页面:

 <?php $connection = mysqli_connect($host, $name, $password, $db); ?>
<?php $admin_set = find_admin(); ?>

<table>
        <tr>
            <th style="text-align: left; width: 200px;">Username:</th>
            <th colspan="2" style="text-align: left;">Action:</th>
        </tr>
        <?php while($admin = mysqli_fetch_assoc($admin_set)) { ?>
        <tr>
            <td><?php echo htmlentities($admin["username"]);?></td>
            <td><a href="edit_admin.php?id=<?php echo urldecode($admin["id"]); ?>">Edit</a></td>
            <td><a href="delete_admin.php?id=<?php echo urldecode($admin["id"]); ?>" onclick="return confirm('Are you sure');">Delete</a></td>
        </tr>
        <?php }?>
    </table>

2 个答案:

答案 0 :(得分:0)

检查您的$连接变量

相同代码的结果。

http://gnuhacker.cafe24.com/qna/24730379.php

源代码。

http://gnuhacker.cafe24.com/qna/24730379.phps

<?PHP
/***
 Database Schema : Mysql 5.1.45

CREATE TABLE IF NOT EXISTS `admins` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL,
  `password` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

INSERT INTO `admins` (`id`, `username`, `password`) VALUES (1, 'test1', 'test1'), (2, 'test2', 'test2');

***/
//$connection = mysqli_connect("localhost","username","password","database") or die("Error " . mysqli_error($connection)); 


$host = "localhost";
$name = "user";
$db = "database";
$password ="password";
function confirm_query($watever){
    global $connection;
    if (!$watever) {
        die("Database query failed! " . mysqli_error($connection));
    }
}

 function find_admin(){
    global $connection;

    $query  = "select * from admins ";
    $query .= "order by username asc";

    $admin_set = mysqli_query($connection, $query);
    confirm_query($admin_set);
    return $admin_set;
}
?>
<?php $connection = mysqli_connect($host, $name, $password, $db); ?>
<?php $admin_set = find_admin(); ?>

<table>
<tr>
    <th style="text-align: left; width: 200px;">Username:</th>
    <th colspan="2" style="text-align: left;">Action:</th>
</tr>
<?php while($admin = mysqli_fetch_assoc($admin_set)) { ?>
<tr>
    <td><?php echo htmlentities($admin["username"]);?></td>
    <td><a href="edit_admin.php?id=<?php echo urldecode($admin["id"]); ?>">Edit</a></td>
    <td><a href="delete_admin.php?id=<?php echo urldecode($admin["id"]); ?>" onclick="return confirm('Are you sure');">Delete</a></td>
</tr>
<?php }?>
</table>

答案 1 :(得分:-1)

问题可能出在以下代码

 $admin_set = mysqli_query($connection, $query);

应该像下面的代码

  $admin_set = mysqli_query($query,$connection);

首先,您需要指定查询,然后指定其连接。