4

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

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.

Manishearth
  • 14,882
  • 8
  • 59
  • 76
Lisa
  • 897
  • 9
  • 27
  • 4
    You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Apr 17 '13 at 11:56
  • You don't run the query. You echo a string which it does perfectly. I don't see the actual connection to the database happening anywhere. – sashkello Apr 17 '13 at 11:56
  • 1
    $row = mysql_fetch_array($result); echo $row['id']; – Damien Legros Apr 17 '13 at 11:57
  • Alright, `mysql_query()` returns a mysql resource and not the results, to get the desired results you need to use `mysql_fetch_array` or `mysql_fetch_assoc` etc..refer [this](http://stackoverflow.com/a/4795059/2274209) – abhij89 Apr 17 '13 at 12:01
  • The connection to the database is written in config.php. `$connect = mysql_connect($localhost, $dbuser, $dbpass) or die($db_error1); mysql_select_db("$dbname", $connect); ` ($dbname etcetera aren't empty, but I won't put them here obviously, haha. Connection works fine by the way, used it with other arguments. Just curious why I have to use a while statement here.) – Lisa Apr 17 '13 at 12:01
  • You don't need a while(), the thing is `mysql_query()` doesn't fetch data, `mysql_fetch_array()` does – Damien Legros Apr 17 '13 at 12:05

4 Answers4

2

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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Thanks, all your answers really helped! I know this is the 'old' SQL way, but I never learned the mySQLi properly. Once I have this working I want to practice with mySQLi :) – Lisa Apr 17 '13 at 12:13
1

$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.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • `$result = mysql_query($query) or die('error'); echo $result[0]['id'];` This one doesn't work, it won't return anything.. :) Or am I doing something wrong? – Lisa Apr 17 '13 at 12:06
  • Oh, you are right! If you want to check all matrix hierarchy, type `print_r($result)`. – fedorqui Apr 17 '13 at 12:12
1

SELECT statement return Resource ID #, you have to iterate your result using mysql_fetch_array, mysql_fetch_assoc functions.

Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
0

try with this

$result[0]['id'];
chandresh_cool
  • 11,753
  • 3
  • 30
  • 45