-1

I am trying to fetch all records in a table with the status '1'. The status field is a TinyInt which im using for true/false. I then want to fetch 1 row from the table at random, then display the rows' fields like below (url, image, alt_text, title).

function userPictures() {

  $query_=mysql_query("SELECT * FROM 'pictures' WHERE 'status' = '1' ORDER BY rand() LIMIT 1");

  while ($row=mysql_fetch_array($query)) {

  echo '<a href="'.$row['url'].'"><img src="'.$row['image'].'" alt="' .$row['alt_text'].'" title="'.$row['alt_text'].'" /></a>"';

  }

  }

When I <?php echo userPictures(); ?>

I get

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /var/www/

As this question is a tired one, the solutions told me to add $result = ... like below.

  $query=mysql_query("SELECT * FROM 'pictures' WHERE 'status' = '1' ORDER BY rand() LIMIT 1");

  $result = mysql_query($query) or die(mysql_error());

  while ($row=mysql_fetch_array($result)) {

  echo '<a href="'.$row['url'].'"><img src="'.$row['image'].'" alt="' .$row['alt_text'].'" title="'.$row['alt_text'].'" /></a>"';

  }

But this returns 'Query was empty'. When I have 4 or more rows with the status field '1' in it. I know the SQL is open to injection, just looking for an answer to why this is happening.

ServerSideSkittles
  • 2,423
  • 9
  • 31
  • 57

1 Answers1

2

Change

$query_=mysql_query("SELECT * FROM 'pictures' WHERE 'status' = '1' ORDER BY rand() LIMIT 1");

to:

  $query=mysql_query("SELECT * FROM pictures WHERE status = '1' ORDER BY rand() LIMIT 1");

Just a small syntax error and removal of single quotes within query.

P.s. There is no reason to echo userPictures(); when userPictures() echoes information within itself.

Fillip Peyton
  • 3,493
  • 1
  • 29
  • 58