For example:

$sql = "SELECT id FROM table WHERE name = '$name'";
$res = mysql_query($sql) or exit (mysql_error());
list ($id) = mysql_fetch_array($res);

  That's it, use list() to make a variable, from mysql_fetch_array(). Very
useful indeed for one line returns :-)

  More than one column:

$sql = "SELECT id, city FROM table WHERE name = '$name'";
$res = mysql_query($sql) or exit (mysql_error());
list ($id_user, $city_user) = mysql_fetch_array($res);

  Vars inside list() can have any name you wish, just remember to put the
same number as the columns in your sql query.

--

Julio Nobrega.

Um dia eu chego lá:
http://sourceforge.net/projects/toca

Ajudei? Salvei? Que tal um presentinho?
http://www.submarino.com.br/wishlistclient.asp?wlid=664176742884


"Erik Price" <[EMAIL PROTECTED]> wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
> I have always retrieved database information using the following general
> format:
>
> <?php
>      mysql_connect($host, $user, $password);
>      mysql_select_db($database);
>      $query = "select * from table";
>      $result = mysql_query($query);
>      while ($row = mysql_fetch_assoc($result)) {
>          echo $row["user_id"];
>          echo $row["fullname"];
>      }
>      mysql_free_result($result);
> ?>
>
> But sometimes I am searching for something specific, with a primary key
> as my WHERE clause.  In other words, I will only EVER get one result.
> In this case, a while loop seems to be overkill, since it will only loop
> once.
>
> Does it seem like a bad idea to just do:
>
> $row = mysql_fetch_assoc($result);
> echo $row['user_id'];
> echo $row['fullname'];
>
> etc?  Or is there a compelling reason to use the loop?  the only reason
> I ask is because I've never seen search query code that -isn't- in a
> loop of some sort.  But it works fine the way I do it, immediately above.
>
>
>
> Thanks,
>
> Erik
>
>
>
>
>
> ----
>
> Erik Price
> Web Developer Temp
> Media Lab, H.H. Brown
> [EMAIL PROTECTED]
>



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to