tags:

views:

318

answers:

4

Im trying to select the title column from a particular row

$eventid = $_GET['id'];
$field = $_GET['field'];
$result = mysql_query("SELECT $field FROM `events` WHERE `id` = '$eventid' ");
echo $result;

all i get is Resource id #19

How should i do this? What is best method?

+3  A: 

Try this:

echo mysql_result($result, 0);

This is enough because you are only fetching one field of one row.

Franz
says parameter count invalid
Patrick
`mysql_result($result, 0)` for the first (and only) row.
jensgram
True. Edited it. But shouldn't this usually work for the first row if I don't explicitly state it.
Franz
@Franz Nope, the second argument is not optional, cf. http://php.net/manual/en/function.mysql-result.php
jensgram
Ah, I see. Ok. Thanks.
Franz
+1  A: 

And escape your values with mysql_real_escape_string since PHP6 won't do that for you anymore! :)

Ben Fransen
+2  A: 
$eventid = $_GET['id'];
$field = $_GET['field'];
$result = mysql_query("SELECT $field FROM `events` WHERE `id` = '$eventid' ");
$row = mysql_fetch_array($result);
echo $row[$field];

but beware of sql injection cause you are using $_GET directly in a query

dfilkovi
+1  A: 

Read the manual, it covers it very well: http://php.net/manual/en/function.mysql-query.php

Usually you do something like this:

while ($row = mysql_fetch_assoc($result)) {
  echo $row['firstname'];
  echo $row['lastname'];
  echo $row['address'];
  echo $row['age'];
}
TheGrandWazoo