这是错误:
警告: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>
答案 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
扩展名。已弃用,请改用PDO
或mysqli
。 mysqli
的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))) { /* ... */ }