Jared
Jared

Reputation: 1793

How do I return data when doing specific MySQL query using PHP?

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.

Upvotes: 0

Views: 54

Answers (3)

SparK
SparK

Reputation: 5211

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

Upvotes: 1

AboQutiesh
AboQutiesh

Reputation: 1716

use it like this :

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

Upvotes: 1

Mathlight
Mathlight

Reputation: 6653

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

Upvotes: 4

Related Questions