Page 1 of 1

ADODB PHP question

Posted: Sun Feb 20, 2005 6:45 pm
by neophyte
I'm trying to learn ADODB. I'm having trouble getting a record set. I only have one row in a test database. I've been RTF but I'm not getting anywhere. Maybe I'm not connected. Although that doesn't seem to be the case. I tried if ($db) and that tested true.

Code: Select all

$db = &NEWADOConnection(DB_TYPE);
$db->Connect(DB_HOST, DB_USER, DB_PASSWD, DB_NAME);
$db->SetFetchMode(ADODB_FETCH_ASSOC);
$rs = $db->Execute($sql);
$rs->GetAssoc();
print_r($rs->fields);
Any help or tips would surely be appreciated...

Tanks!

Posted: Sun Feb 20, 2005 6:59 pm
by timvw
this is how i use adodb...

Code: Select all

$db = NewADOConnection(DATA_DSN) or trigger_error('Failed to connect to database.', E_USER_ERROR);
$db->SetFetchMode(ADODB_FETCH_ASSOC);
$query = "SELECT $what $from $where $groupby $having $orderby";
$result = $db->SelectLimit($query, $rows_per_page, $offset) or trigger_error($db->ErrorMsg(), E_USER_ERROR);
$rows = array();
while ($row = $result->FetchRow())
{
  $rowsї] = $row;
}


print_r($rows);

Posted: Sun Feb 20, 2005 8:09 pm
by neophyte

Code: Select all

$db->SetFetchMode(ADODB_FETCH_ASSOC);
$result = $db->GetAll($sql) or trigger_error($db->ErrorMsg(), E_USER_ERROR); 
$rows = array();
while ($row = $result->FetchRow())
{
  $rowsї] = $row;
}
Here's what I'm trying now. But I'm getting this message:
Fatal error: Call to a member function on a non-object in /home/ritterr/public_html/rf/index.php on line 21
Line 21 is:

Code: Select all

while ($row = $result->FetchRow())
Any ideas?

Thanks for your tips timvw. I'm definitely connected now. But I tried your example you posted. I kept getting errors... I'm not sure (aside from $query) what the arguments in SelectLimit are for...

Posted: Sun Feb 20, 2005 9:35 pm
by timvw
from the manual
http://phplens.com/lens/adodb/docs-adodb.htm
GetAll($sql)

Executes the SQL and returns the all the rows as a 2-dimensional array. The recordset is discarded for you automatically. If an error occurs, false is returned
SelectLimit($sql,$numrows=-1,$offset=-1,$inputarr=false)

Returns a recordset if successful. Returns false otherwise. Performs a select statement, simulating PostgreSQL's SELECT statement, LIMIT $numrows OFFSET $offset clause.
this means that if you $db->GetAll you recieve an array.. you can iterate through that arary with foreach

Code: Select all

$rows = $db->GetAll($query);
foreach($rows as $row)
{
  print_r($row);
  echo "<br>";
&#125;

Posted: Mon Feb 21, 2005 6:13 am
by neophyte
Thanks for the tip I finally got a dataset using this:

Code: Select all

$sql = "SELECT id, title, brief, story, date FROM rf_news ORDER BY date LIMIT 5";
$rs = $db->Execute($sql);
if (!$records = $rs->GetAssoc())
&#123;
	end_script(__LINE__, __FILE__);		 
&#125;
I think the part of ADODB that is confusing is the returned recordset object. This is very different than other DB classes I've used before. In the example I posted the object is created by the Execute() call. In yours the object is created with SelectLimit().

Somehow I missed this from the manual:
Each recordset saves and uses whatever fetch mode was set when the recordset was created in Execute() or SelectLimit().
and this...
Whenever we want to query the database, we call the ADOConnection.Execute() function. This returns an ADORecordSet object
Once I figured out this the rest has been pretty clear sailing.

Thanks for you help timvw! :wink: