empty query results

Discussion forum for MOD Writers regarding MOD Development.
Locked
net83it
Registered User
Posts: 500
Joined: Sun Jan 15, 2006 2:42 pm
Location: Sicily
Contact:

empty query results

Post by net83it » Sun Feb 21, 2010 10:28 am

hello

the following code is part of a php page that performs a search on topics title

Code: Select all

$sql = "SELECT 
pt.topic_id, pt.topic_title
FROM ". TOPICS_TABLE." pt 
WHERE pt.topic_title like '$key'
ORDER BY pt.topic_id";

$db->sql_query($sql);
$result = $db->sql_query($sql);

if (mysql_num_rows($result) == 0)
{
	trigger_error('NO_SEARCH_RESULTS');
}

while ($row = $db->sql_fetchrow($result))
{
   $template->assign_block_vars('cerca_list', array(
      'TOPIC_ID'              => $row['topic_id'],
      //'POST_ID'               => $row['post_id'],
      'TOPIC_TITLE'      => $row['topic_title'],
      'TOPIC_LINK'      => append_sid("{$phpbb_root_path}viewtopic.$phpEx", 't='.$row['topic_id']),
      //'POST_AUTHOR'      => $row['username'],
   ));
}
i have a problem with this part

Code: Select all

if (mysql_num_rows($result) == 0)
{
	trigger_error('NO_SEARCH_RESULTS');
}
i want to show the NO_SEARCH_RESULTS error message if the query result is empty, but i get a script error if i use mysql_num_row

please help me. how can i detect if $results is empty?

thank you

User avatar
m157y
Registered User
Posts: 482
Joined: Mon Apr 30, 2007 9:39 am
Location: Russia, Moscow, Khimki
Contact:

Re: empty query results

Post by m157y » Sun Feb 21, 2010 2:26 pm

I think it'll be better if you'll use something like this:
at php:

Code: Select all

$sql = "SELECT 
   pt.topic_id, pt.topic_title
   FROM ". TOPICS_TABLE." pt 
   WHERE pt.topic_title like '$key'
   ORDER BY pt.topic_id";
$db->sql_query($sql);
$result = $db->sql_query($sql);

while ($row = $db->sql_fetchrow($result))
{
   $template->assign_block_vars('cerca_list', array(
      'TOPIC_ID'              => $row['topic_id'],
      //'POST_ID'               => $row['post_id'],
      'TOPIC_TITLE'      => $row['topic_title'],
      'TOPIC_LINK'      => append_sid("{$phpbb_root_path}viewtopic.$phpEx", 't='.$row['topic_id']),
      //'POST_AUTHOR'      => $row['username'],
   ));
}
at template:

Code: Select all

<!-- BEGIN cerca_list -->
your_routine_here
<!-- BEGINELSE -->
{L_NO_SEARCH_RESULTS}
<!-- END cerca_list -->
m157y aka Misty
NO SUPPORT VIA PM
KarmaMOD for phpBB 3.0.x | bbAJAX
Follow me on twitter

kmklr72
Registered User
Posts: 477
Joined: Sat Dec 29, 2007 9:37 pm
Location: St. Louis, USA
Name: Kevin

Re: empty query results

Post by kmklr72 » Mon Feb 22, 2010 1:58 am

I've always used:

Code: Select all

if (!$db->sql_fetchrow($result)) 
Seems to work fine for me.

User avatar
m157y
Registered User
Posts: 482
Joined: Mon Apr 30, 2007 9:39 am
Location: Russia, Moscow, Khimki
Contact:

Re: empty query results

Post by m157y » Mon Feb 22, 2010 2:13 am

Khm. I hope you mean

Code: Select all

if (!($row = $db->sql_fetchrow($result)))
Or else you'll lose first row at sql results.
And a littler question.
Why you don't like code posted by me?
m157y aka Misty
NO SUPPORT VIA PM
KarmaMOD for phpBB 3.0.x | bbAJAX
Follow me on twitter

net83it
Registered User
Posts: 500
Joined: Sun Jan 15, 2006 2:42 pm
Location: Sicily
Contact:

Re: empty query results

Post by net83it » Mon Feb 22, 2010 7:56 pm

both

Code: Select all

if (!($row = $db->sql_fetchrow($result)))

Code: Select all

if (!$db->sql_fetchrow($result))  
skip the first row

i'll try with the template solution

User avatar
m157y
Registered User
Posts: 482
Joined: Mon Apr 30, 2007 9:39 am
Location: Russia, Moscow, Khimki
Contact:

Re: empty query results

Post by m157y » Mon Feb 22, 2010 8:03 pm

2 net83it
You wrong.
With

Code: Select all

if (!($row = $db->sql_fetchrow($result)))
You need use this loop:

Code: Select all

do
{
}
while (somthing);
But i don't understand why you don't like my first post idea. It works perfectly and without any error.
m157y aka Misty
NO SUPPORT VIA PM
KarmaMOD for phpBB 3.0.x | bbAJAX
Follow me on twitter

Locked

Return to “[3.0.x] MOD Writers Discussion”