首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >mysql_fetch_assoc很奇怪

mysql_fetch_assoc很奇怪
EN

Stack Overflow用户
提问于 2012-02-11 23:44:47
回答 4查看 235关注 0票数 0

可能重复:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result

好的,这是我的密码。

代码语言:javascript
复制
<?php

include 'connect.php';


$id = addslashes($_REQUEST['id']);


$image = "SELECT * FROM images WHERE id=$id";
$result = mysql_query($image);
$imagearray = mysql_fetch_assoc($result);
$realimage = $imagearray['image'];

header("Content-type: image/jpeg");

echo $realimage;

?>

该页面使用GET获取id,但它给出了以下错误:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/a6289454/public_html/get.php on line 9

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-02-11 23:47:54

var_dump(mysql_error());将帮助您找到错误消息。但最有可能的是你需要引号:

代码语言:javascript
复制
$image = "SELECT * FROM `images` WHERE `id`='".$id."'";
票数 3
EN

Stack Overflow用户

发布于 2012-02-11 23:59:34

我认为问题可能出现在addslashes()中,请尝试使用mysql_real_escape_string()。

票数 0
EN

Stack Overflow用户

发布于 2012-02-12 00:10:48

为什么要在$_REQUEST['id']中添加斜杠?它应该是一个整数

试试下面的代码:

代码语言:javascript
复制
if(is_numeric($_REQUEST['id'])){

    $q = "SELECT * FROM images WHERE id = " . $id; // Don't put variables in strings!
    $rs= mysql_query($q) or die("There is an error in the query:<br>" . $q . "<br><br>The error is: ". mysql_error()); // Throw and error if there is one.
    $result = mysql_fetch_assoc($rs);

} else { die('Not a valid ID'); }

另外,为了更好的实践,请使用require_once('connect.php');而不是include 'connect.php';

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9245086

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档