SQL error in php

Thank you very much for your answers, they helped me a lot!

I am wondering what I am doing wrong: I cannot get any values ​​from the SQL statement.

My database table structure: enter image description here

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']; 

When testing, I get the following:

"SELECT * FROM upload WHERE id = '1'

Resource ID No. 2 "

But there is an identifier with a value of "2", but why is it not displayed in my html? Only with the expression β€œwhile” do I get the desired results:

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

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

+4
source share
4 answers

A resource is a resource. It contains several lines. This is not special for the case when exactly one row is returned.

You do not need to use while if you know that there is only one result, but you still need to use mysql_fetch_array or some other method to extract the first row from it.

+2
source

$result is a matrix, each row of which has an output. Therefore, to access id you first need to specify a string.

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

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

+1
source

SELECT return return Resource ID # , you need to repeat the result using mysql_fetch_array, mysql_fetch_assoc functions.

+1
source

try with this

 $result[0]['id']; 
0
source

All Articles