"$db->sql_numrows($result)" - no longer works?

Discussion of general topics related to the new version and its place in the world. Don't discuss new features, report bugs, ask for support, et cetera. Don't use this to spam for other boards or attack those boards!
Forum rules
Discussion of general topics related to the new release and its place in the world. Don't discuss new features, report bugs, ask for support, et cetera. Don't use this to spam for other boards or attack those boards!
Post Reply
Glorius Hole
Registered User
Posts: 31
Joined: Sun Sep 18, 2005 5:39 pm

"$db->sql_numrows($result)" - no longer works?

Post by Glorius Hole »

"$db->sql_numrows($result)" no longer works. It did in a previous beta (3 and before, since I think I haven't tried 4). When I try and use this for something I'm doing it chucks out an error; "Fatal error: Call to undefined function: sql_numrows() blah blah"

I take it from that error that this particular function has been removed and/or replaced with something else but I don't know what. I'm still just a novice at all this. Many thanks.

User avatar
DoubleJ
Registered User
Posts: 105
Joined: Thu Jan 13, 2005 4:19 pm
Location: The Netherlands
Contact:

Re: "$db->sql_numrows($result)" - no longer works?

Post by DoubleJ »

That function has been removed since not all database types support it.

I have the following workaround, if you are trying to obtain 1 row:

Code: Select all

$sql = "SELECT * FROM " .TABLE. " WHERE foo = foo LIMIT 0, 1";
$result = $db->sql_query($sql);
// check if we have fetched an actual row.
if( !count($row_info = $db->sql_fetchrow($result)) )
{
	trigger_error('NO_ROW_WAS_FOUND');
}
// now you can continue with $row_info as your fetch
However, as mentioned this only works if you fetch 1 row. If you fetch multiple rows, you'll have to do something differently.
Somewhere in viewtopic.php is a way to do a count rows.
DoubleJ - Blah

Glorius Hole
Registered User
Posts: 31
Joined: Sun Sep 18, 2005 5:39 pm

Re: "$db->sql_numrows($result)" - no longer works?

Post by Glorius Hole »

Yea, I do need to check more than one row as its for a 'top 10 topics' sort of thing I have on the main index. I'll scour through viewtopic then until I see something to what I need in that case. Thanks.:)

User avatar
Handyman
Registered User
Posts: 522
Joined: Thu Feb 03, 2005 5:09 am
Location: Where no man has gone before!
Contact:

Re: "$db->sql_numrows($result)" - no longer works?

Post by Handyman »

There are a couple ways to do that... first, phpBB uses the MySQL "count" function which you need 1 extra query for.
You could also do this:

Code: Select all

<?php $sql = "SELECT * FROM " .TABLE. " WHERE foo = foo LIMIT 0, 1";
$result = $db->sql_query($sql);
$rows = $db->sql_fetchrowset($result);
$db->sql_freeresult($result);

$num_rows = count($rows);//here is your result count... do whatever you wish with it.

foreach ($rows as $row)
{
    $template->assign_block_vars('whatever', array(
        'FOO_ID'    => $row['foo_id'],
        'FOO_NAME' => $row['name'],
    ));
}
unset($rows);
so basically the foreach takes the place of the "while" loop.
I think in my previous tests, If I remember correctly, I was not able to put the result into an array, count it and still run the "while" loop... but the above method works.
Last edited by Handyman on Fri Feb 02, 2007 11:12 pm, edited 1 time in total.
My phpBB3 Mods || My Mod Queue
Search Engine Friendly (SEO) URLs || Profile link on Avatar and/or Username || AJAX Chat
Display Posts Anywhere || CashMod || AJAX Quick Edit || AJAX Quick Reply

Image

User avatar
Arty
Former Team Member
Posts: 985
Joined: Wed Mar 06, 2002 2:36 pm
Location: Mars
Contact:

Re: "$db->sql_numrows($result)" - no longer works?

Post by Arty »

That's wrong code. $db->sql_fetchrow() returns only 1 row per call or false if rows ended. Use $db->sql_fetchrowset() instead.

User avatar
Handyman
Registered User
Posts: 522
Joined: Thu Feb 03, 2005 5:09 am
Location: Where no man has gone before!
Contact:

Re: "$db->sql_numrows($result)" - no longer works?

Post by Handyman »

CyberAlien wrote: That's wrong code. $db->sql_fetchrow() returns only 1 row per call or false if rows ended. Use $db->sql_fetchrowset() instead.

whoops... I meant to put rowset :| fixed now.
My phpBB3 Mods || My Mod Queue
Search Engine Friendly (SEO) URLs || Profile link on Avatar and/or Username || AJAX Chat
Display Posts Anywhere || CashMod || AJAX Quick Edit || AJAX Quick Reply

Image

Glorius Hole
Registered User
Posts: 31
Joined: Sun Sep 18, 2005 5:39 pm

Re: "$db->sql_numrows($result)" - no longer works?

Post by Glorius Hole »

Many thanks for the help. That script worked wonders. Plus it's also much more simple now than the way I had it before, which is always a good thing (and I might be able to use that method for something else that I've been doing where as before it wouldn't work).

Thanks again.

Post Reply