0

Here is my PHP MySQL query:

$query = "SELECT falsegoto FROM timeconditions WHERE [timeconditions_id] = 0";
$result =  mysql_query($query);

There should be only a single result from this query, and I'm not sure how display it in PHP? mysql_result() seems to only work with larger data sets?

Any help or explanation would be valued.

Jared
  • 1,795
  • 6
  • 32
  • 55
  • What are those brackets in your query? Why do you think `mysql_result()` only works on large datasets? – PeeHaa Sep 13 '12 at 12:55
  • 2
    Please, don't use `mysql_*` functions for new code. They are no longer maintained and the community has begun the [deprecation process](http://goo.gl/KJveJ). See the [**red box**](http://goo.gl/GPmFd)? Instead you should learn about [prepared statements](http://goo.gl/vn8zQ) and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you can't decide, [this article](http://goo.gl/3gqF9) will help to choose. If you care to learn, [here is a good PDO tutorial](http://goo.gl/vFWnC). – PeeHaa Sep 13 '12 at 12:56

3 Answers3

4

As Peeha mentiod, your using mysql, but it's better to use mysqli

So the code will then look like this:

$query = "SELECT falsegoto FROM timeconditions WHERE [timeconditions_id] = 0";
$result =  mysqli_query($query);
    while($row = myslqi_fetch_assoc($result){
    // DO STUFF
}

I use this for everything. It just loops through every row in the result. and if there's just one row, it while's only one time....

Mathlight
  • 6,436
  • 17
  • 62
  • 107
1

use it like this :

while ($row = mysql_fetch_assoc($result)) {
    echo $row['firstname'];
    echo $row['lastname'];
    echo $row['address'];
    echo $row['age'];
}
AboQutiesh
  • 1,696
  • 2
  • 9
  • 14
1

You have to fetch the row, there are a few methods of doing it: mysql_fetch_object, mysql_fetch_row, mysql_fetch_array and mysql_fetch_assoc.
These methods will read a single line from the result and remove it from the handler, so if you loop the call it will read all the rows, one by one until it reaches the end and returns false.

example:

while($obj = mysql_fetch_object($result)){
    echo $obj->name;
}

PHP.net documentation:
mysql_fetch_object,
mysql_fetch_row,
mysql_fetch_array,
mysql_fetch_assoc

SparK
  • 5,181
  • 2
  • 23
  • 32