Asaf
Asaf

Reputation: 557

PHP print from database without 'while'

I need to print one row from a table so a while loop isn't necessary, is there any other method?

Upvotes: 0

Views: 2080

Answers (4)

Matt Montag
Matt Montag

Reputation: 7490

$results = mysql_query("SELECT * FROM my_table WHERE user_id = 1234");
$row = mysql_fetch_assoc($results);
echo ($row['user_id']);

Upvotes: 1

greg0ire
greg0ire

Reputation: 23265

Do what you would have done inside the condition of your loop, and you'll be fine.

Upvotes: 0

Brad Christie
Brad Christie

Reputation: 101614

if (($dbResult = mysql_query("SELECT ... FROM ... LIMIT 1")) !== false)
{
  $row = mysql_fetch_array($dbResult);
  echo $row['Column_Name'];
}

Just fetch one row, no need to always loop a retrieval.

Upvotes: 1

RiaD
RiaD

Reputation: 47658

You need not while.

Just do your while condition outside while 1 time.

i.e

$a=mysql_fetch_row($sql);
//use $a

instead of

while($a=mysql_fetch_row($sql)){
    //use $a
}

Upvotes: 3

Related Questions