Page 1 of 1

MySQL output methods

Posted: Thu Oct 05, 2006 6:25 am
by impulse()
I know of two ways to output a query from a MySQL DB. Are there any other ways to do this other than:

Code: Select all

$query = mysql_query("SELECT something FROM somewhere");

while ($results = mysql_fetch_array($query)) {
  echo $results['something'];
}
And

Code: Select all

$query = mysql_query("SELECT something FROM somewhere");
$numRows = mysql_numrows($query);

for ($i = 0; $i < $numRows; $i++) {
$colOne = mysql_results($query, $i, "colName");
$colTwo = mysql_results)$query, $i, "colTwoName");
echo $colOne;
echo $colTwo;
}
Regards,

Posted: Thu Oct 05, 2006 6:27 am
by volka
Lots of. What are you looking for?

Posted: Thu Oct 05, 2006 6:36 am
by impulse()
Nothing inparticular. Just a few methods I can note down. I'm sure they'll come in useful in the future.

Re: MySQL output methods

Posted: Thu Oct 05, 2006 10:14 am
by Luke
This will give you an array of associative arrays with column names as keys and column values as values

Code: Select all

$records = array();
$query = mysql_query("SELECT something FROM somewhere");
while ($row = mysql_fetch_assoc($query)) {
    $records[] = $row;
}
I use a mysql result iterater written by d11wtq... and it's quite slick... (I'm building an active record library on top if it actually -- well it's kind of active record... it's hopefully not going to be quite as restrictive as Active Record usually is). It looks like this:

Code: Select all

require_once('DB.php');
require_once('DB_Result.php');

$db = new DB('localhost', 'user', 'pass', 'db_test');

/*
 create table foo
(
    id int auto_increment primary key,
    one varchar(255),
    two varchar(255)
)
 */

$result = $db->query("select * from foo");

//Go forwards through the resultset
for ($result->first(); !$result->end(); $result->next())
{
        echo $result->one.' '.$result->two.'<br />';
}