[PHP] How *not* to assume MySQL will return data?

2002-05-13 Thread Chris Knipe

Hiya all,

Stock standard, and from most of the examples I've seen, standard MySQL
lookups via PHP utlises a while look to retrieve the data.  All very nice
and well yes... But what if there is no data?

Here's a little example of what I want to do...

// Check for inportaint notices for the contact.
echo table width=\100%\ class=\td\\n;
echo   tr\n;
echo td colspan=\2\ valign=\top\ class=\subhead\\n;
echo   buImportant notes:/u/bbr\n;
echo /td\n;
echo   /tr\n;
$SQL = mysql_query(SELECT MessageID, MessageTitle FROM contactmessages
WHERE ContactID=' . $_SESSION['ContactID'] . ' AND MessageRead='n';);
while ($ContactMessageList = mysql_fetch_array($SQL)) {
  echo   tr\n;
  echo td align=\left\ valign=\top\a
href=\/profile/readmsg.php?MessageID= . $ContactMessageList['MessageID'] .
\ alt=\Read Message\ border=\0\img src=\/lib/images/arrow.gif\
border=\0\/td\n;
  echo td align=\left\ valign=\top\ .
$ContactMessageList['MessageTitle'] . /td\n;
  echo   /tr\n;
}
echo /table\n;
echo br\n;

From this example, this block would be printed evertime.  Whether the
contact has actual messages or not.  How would I go about in a instance like
this, to use a MySQL check and only print the block if $SQL will actually
return data ?

In this case, can I do something like...

if ($SQL = mysql_query(something) {
  print the block
  while (do the actualy mysql lookup) {
print data
  }
}

Spanks everyone :)

--
me



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] How *not* to assume MySQL will return data?

2002-05-13 Thread Chris Knipe

H.

There's no way to do it *without* having to submit two queries to the
database?  I'm thinking load wise here. The query (or block), would be
displayed on every single page in my app - sort of like a shared border type
thing...

Now, when I have say, 20 pages in the site, 250 concurrent users (or active
sessions) on the site - which, can happy very quickly - the load on the
database is going to be -very- high   So I'm kind of looking for a way
how I can do this, with as few queries as possible, to put as little stress
on the database as I can...

But yes, if there is no otherway to do it, then I guess this would have to
do


Kind Regards,

Chris Knipe
MegaLAN Corporate InterNetworking Services
Tel: +27 21 854 7064
Cell: +27 72 434 7582

- Original Message -
From: Nathan [EMAIL PROTECTED]
To: Chris Knipe [EMAIL PROTECTED]
Sent: Monday, May 13, 2002 7:12 PM
Subject: Re: [PHP] How *not* to assume MySQL will return data?


 if (mysql_num_rows($query)  0) {
 print crud
 }
 else {
 echo Query succeeded, but produced no results;
 }

 Is that what you mean?


 - Original Message -
 From: Chris Knipe [EMAIL PROTECTED]
 To: [EMAIL PROTECTED]
 Sent: Monday, May 13, 2002 8:46 AM
 Subject: [PHP] How *not* to assume MySQL will return data?


 Hiya all,

 Stock standard, and from most of the examples I've seen, standard MySQL
 lookups via PHP utlises a while look to retrieve the data.  All very nice
 and well yes... But what if there is no data?

 Here's a little example of what I want to do...

 // Check for inportaint notices for the contact.
 echo table width=\100%\ class=\td\\n;
 echo   tr\n;
 echo td colspan=\2\ valign=\top\ class=\subhead\\n;
 echo   buImportant notes:/u/bbr\n;
 echo /td\n;
 echo   /tr\n;
 $SQL = mysql_query(SELECT MessageID, MessageTitle FROM
contactmessages
 WHERE ContactID=' . $_SESSION['ContactID'] . ' AND MessageRead='n';);
 while ($ContactMessageList = mysql_fetch_array($SQL)) {
   echo   tr\n;
   echo td align=\left\ valign=\top\a
 href=\/profile/readmsg.php?MessageID= . $ContactMessageList['MessageID']
.
 \ alt=\Read Message\ border=\0\img src=\/lib/images/arrow.gif\
 border=\0\/td\n;
   echo td align=\left\ valign=\top\ .
 $ContactMessageList['MessageTitle'] . /td\n;
   echo   /tr\n;
 }
 echo /table\n;
 echo br\n;

 From this example, this block would be printed evertime.  Whether the
 contact has actual messages or not.  How would I go about in a instance
like
 this, to use a MySQL check and only print the block if $SQL will actually
 return data ?

 In this case, can I do something like...

 if ($SQL = mysql_query(something) {
   print the block
   while (do the actualy mysql lookup) {
 print data
   }
 }

 Spanks everyone :)

 --
 me



 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php






-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] How *not* to assume MySQL will return data?

2002-05-13 Thread Jason Wong

On Tuesday 14 May 2002 01:36, Chris Knipe wrote:
 H.

 There's no way to do it *without* having to submit two queries to the
 database?  I'm thinking load wise here. The query (or block), would be
 displayed on every single page in my app - sort of like a shared border
 type thing...

 Now, when I have say, 20 pages in the site, 250 concurrent users (or active
 sessions) on the site - which, can happy very quickly - the load on the
 database is going to be -very- high   So I'm kind of looking for a way
 how I can do this, with as few queries as possible, to put as little stress
 on the database as I can...

 But yes, if there is no otherway to do it, then I guess this would have to
 do

It's just a matter of structuring your code properly. Separate code from HTML. 
Have your regular while-loop reading all the results into an array. If array 
is not empty then print your stuff out, otherwise don't print anything.

-- 
Jason Wong - Gremlins Associates - www.gremlins.com.hk
Open Source Software Systems Integrators
* Web Design  Hosting * Internet  Intranet Applications Development *

/*
Virtual means never knowing where your next byte is coming from.
*/


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] How *not* to assume MySQL will return data?

2002-05-13 Thread 1LT John W. Holmes

$result = mysql_query(...);
if($row = mysql_fetch_array($result))
{
  do {
//loop through results
  }while($row = mysql_fetch_array($result));
}
else
{ echo No rows returned; }

You could also just check mysql_num_rows() to see if any were returned and
base your if off of that.

---John Holmes...
- Original Message -
From: Chris Knipe [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Sent: Monday, May 13, 2002 10:46 AM
Subject: [PHP] How *not* to assume MySQL will return data?


 Hiya all,

 Stock standard, and from most of the examples I've seen, standard MySQL
 lookups via PHP utlises a while look to retrieve the data.  All very nice
 and well yes... But what if there is no data?

 Here's a little example of what I want to do...

 // Check for inportaint notices for the contact.
 echo table width=\100%\ class=\td\\n;
 echo   tr\n;
 echo td colspan=\2\ valign=\top\ class=\subhead\\n;
 echo   buImportant notes:/u/bbr\n;
 echo /td\n;
 echo   /tr\n;
 $SQL = mysql_query(SELECT MessageID, MessageTitle FROM
contactmessages
 WHERE ContactID=' . $_SESSION['ContactID'] . ' AND MessageRead='n';);
 while ($ContactMessageList = mysql_fetch_array($SQL)) {
   echo   tr\n;
   echo td align=\left\ valign=\top\a
 href=\/profile/readmsg.php?MessageID= . $ContactMessageList['MessageID']
.
 \ alt=\Read Message\ border=\0\img src=\/lib/images/arrow.gif\
 border=\0\/td\n;
   echo td align=\left\ valign=\top\ .
 $ContactMessageList['MessageTitle'] . /td\n;
   echo   /tr\n;
 }
 echo /table\n;
 echo br\n;

 From this example, this block would be printed evertime.  Whether the
 contact has actual messages or not.  How would I go about in a instance
like
 this, to use a MySQL check and only print the block if $SQL will actually
 return data ?

 In this case, can I do something like...

 if ($SQL = mysql_query(something) {
   print the block
   while (do the actualy mysql lookup) {
 print data
   }
 }

 Spanks everyone :)

 --
 me



 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php