ADODB PHP question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

ADODB PHP question

Post 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!
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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);
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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...
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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;
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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:
Post Reply