简体   繁体   中英

SQL issue within php

Thanks a lot for your answers, they really helped me a lot!

I'm curious of what I'm doing wrong: I can't get any values from a SQL statement.

My database table structure: 在此输入图像描述

My phpcode:

include('config.php');
$id = $_GET['id'];

$query = "SELECT * FROM upload WHERE id = '$id'";
echo $query."<br/>";


$result = mysql_query($query) or die('error');
print_r($result);
echo $result['id'];

I get the following when testing:

"SELECT * FROM upload WHERE id = '1'

Resource id #2"

But there IS an id with value '2', but why doesn't it show in my html? Only with a 'while' statement I get the desired results:

while($results = mysql_fetch_array($result)) 
    {
        echo $results['filetitle'];
    }

Is this while statement necessary with a single result? I mean, there can only be one ID.

A resource is a resource. It contains a number of rows. It isn't special-cased for when there is exactly one row returned.

You don't have to use while if you know there is exactly one result, but you still need to use mysql_fetch_array or some other method to extract the first row from it.

$result is a matrix, having each row an output. So to access to id you firstly have to indicate the row.

For example, with $result[ 0 ][ 'id' ] .

However, it is quite better to do it through the while($results = mysql_fetch_array($result)) expression.

SELECT语句返回Resource ID # ,你必须使用mysql_fetch_array, mysql_fetch_assoc函数迭代你的结果。

尝试这个

$result[0]['id'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM