0

Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

How would you print the names of a table where a value in a field matches the value of a variable. i have a bit of code:

$query = "select * from event WHERE eventname = $eventname";
$result = mysql_query($query);
$numcolumn = mysql_num_fields($result);
for ( $i = 8; $i < $numcolumn; $i++ ) {
$columnnames = mysql_field_name($result, $i);
echo $columnnames . "<br />";
}

this gives me the following error:

Warning: mysql_num_fields() expects parameter 1 to be resource, boolean given in /var/www/sportevent/eventform.php on line 196
Community
  • 1
  • 1
SebastianOpperman
  • 6,339
  • 6
  • 29
  • 36
  • 2
    You need to wrap the $eventname in single quotes because it makes $result = false right now. Then you should read up on prepared statements. – Paul Stanley Oct 10 '11 at 13:49

2 Answers2

1

use

$result = mysql_query($query);

if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}

$numcolumn = mysql_num_fields($result);
...
diEcho
  • 52,196
  • 40
  • 166
  • 239
0

You query failed; you should catch that and print an error message.

if (!$result) {
    die('Invalid query: ' . mysql_error());
}

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements /* */ or FALSE on error.

-> http://www.php.net/manual/en/function.mysql-query.php

MasterCassim
  • 8,874
  • 3
  • 23
  • 30