I like the PEAR::DB abstraoction library for this:
It's easy to use, and gives you a lot of options on how you can get data 
back.

You can find more info at 
http://pear.php.net/manual/en/package.database.db.php
There are also a bunch of tutorials, but here's everything you need to 
make it work.
(You will need the PEAR library in your path -- it usually is by default.)

<?php

// Include the PEAR DB Abstraction class
require_once('DB.php');

// Set the values for your DSN
$dsn = "mysql://user:[EMAIL PROTECTED]/database";

// Instantiate the class
$objDB = DB::Connect($dsn);

/* Tell PearDB to set the FetchMode to an Associative Array
*  Other possibilites are: 
*  DB_FETCHMODE_ORDERED - Ordered Array
*  DB_FETCHMODE_OBJECT - Object
*  More Info: 
http://pear.php.net/manual/en/package.database.db.db-common.setfetchmode.php
*/

$objDB->setMode(DB_FETCHMODE_ASSOC);

// Limit query, just to be sure we're getting one row.
$sql = "SELECT something FROM tablename WHERE a = '".$a."' LIMIT 0,1";

// Since we know we're getting a single value, we can use the getRow() method.
$result = $objDB->getRow($sql);

var_dump($result);
?>

Output: array('something' => 'your value');

Setmode lets you choose what you want back in the way of data.
You can choose from Ordered Array, Associative Array and Object.

I really hate this thing -- I think it's a stupid way to have to get data out 
of a query:

       while ($row = mysql_fetch_array($result) ) {
          $x = $row['x'];
       }

With PEAR::DB, you can just do:
$result = $objDB->getAll($sql);

You get the whole dataset back in an Array or an Object... no fuss, no muss.

I left out some error checking for clarity, but it's always a good idea 
to check for errors like this:

$result = $objDB->query($sql);
if (PEAR::isError($result)) {
       // do something
}

-Jeromie

>03292005 1506 GMT-6
>
>Ok. This might be a odd question to some but, in my learning of the user 
>of php with mysql, this is how I learned to get values...
>
>$sql = "SELECT x FROM tablename WHERE a = '".$a."'";
>    $result = mysql_query($sql);
>       while ($row = mysql_fetch_array($result) ) {
>          $x = $row['x'];
>       }
>
>What I want to know is, if I am just getting ONE result back, can I 
>write this differently? I have used this for so long to get all results 
>that I never thought to ask.
>
>Wade
>
>
>[Non-text portions of this message have been removed]
>
>
>
>The PHP_mySQL group is dedicated to learn more about the PHP_mySQL web 
>database possibilities through group learning.  
>Yahoo! Groups Links
>
>
>
> 
>
>
>
>  
>


The PHP_mySQL group is dedicated to learn more about the PHP_mySQL web database 
possibilities through group learning.  
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/php_mysql/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



Reply via email to