mysql_fetch_array():提供的参数不是有效的MySQL结果资源... [snip]

时间:2014-10-13 15:40:06

标签: php mysql sql loops while-loop

这是错误:

  

警告:mysql_fetch_array():提供的参数不是第21行/home/a1797699/public_html/event_details.php中的有效MySQL结果资源

这是失败的代码:

<?php
include dirname(__FILE__).'/db_connection.php';
$eventid = $_GET['id'];
$user = $_SESSION['account'];
$sqlevents = "SELECT * FROM events WHERE ID = $eventid";

?>
<html>
<body>

<?php include dirname(__FILE__).'/Header.php'; ?>

<div class="wrapper">
    <div class="contentleft"> <?
        while($event = mysql_fetch_array($sqlevents)){ //===ERROR
            ?>
            <table  align="center" style="color:white;">
                <th colspan="2"><? echo $event['title']; ?></th>
                <tr>
                    <td>Date</td>
                    <td><? echo $event['date']; ?></td>
                </tr>
                <tr>
                    <td>Discription</td>
                    <td><? echo $event['discription']; ?></td>
                </tr>
                <tr>
                    <td>Created On</td>
                    <td><? echo $event['created on']; ?></td>
                </tr>
                <tr>
                    <td>Created By</td>
                    <td><? echo $event['created by']; ?></td>
                </tr>
                <tr>
                    <td>Participants</td>
                    <td>
                    </td>
                </tr>
                <tr>
                    <td colspan="2"><a href="?adduser=<? echo $user; ?>&id=<? echo $eventid; ?    >">Sign up</a></td>
                </tr>
            </table> <?
        } ?>
    </div>
    <div class="contentright">
        <?php include dirname(__FILE__).'/infopannel.php'; ?>
    </div>
</div>

<?php include dirname(__FILE__).'/footer.php'; ?>

</body>
</html>

1 个答案:

答案 0 :(得分:4)

当你激活资源句柄时,你试图在字符串上使用mysql_fetch_array

include dirname(__FILE__).'/db_connection.php';
$eventid = $_GET['id'];
$user = $_SESSION['account'];
$sqlevents = "SELECT * FROM events WHERE ID = $eventid";
$result = mysql_query($sqlevents);

// ....

while($event = mysql_fetch_array($result)){/* ... */ }

但是,如果现在没有人告诉过你,你不应该使用ext/mysql扩展名。已弃用,请改用PDOmysqlimysqli的api ver与ext/mysql类似,支持MySQL的更多功能..但PDO更容易与IMO合作。

与PDO相同:

$db = new PDO($dsn, $user, $pass);
$stmt = $db->prepare('SELECT * FROM events WHERE ID = ?');
$stmt->execute(array($eventid));

while (false !== ($row = $stmt->fetch(PDO::FETCH_ASSOC))) { /* ... */ }