473,545 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what is the correct way to handle empty database returns?

If I do something like :

$result = mysql_query($qu ery);
$dbArray = dbResultIntoKey Array($result);
and this is the function:
function dbResultIntoKey Array($result) {
while ($row = mysql_fetch_obj ect($result, MYSQL_ASSOC)) {
$dbArray[] = $row;
}
return $dbArray;
}
then if the query came back empty I get an error message: not a valid
mysql resource.

What is the correct way of handling this? If I was dealing with an
array I'd start the function with something like:

if (is_array($arra y)) {

}

and then I wouldn't have to worry about error messages if it turned
the variable wasn't an array. What is the similar test for database
returns?
Jul 16 '05 #1
3 7449
Hi lawrence!
On 14 Sep 2003 20:38:09 -0700, lk******@geocit ies.com (lawrence)
wrote:
If I do something like :

$result = mysql_query($qu ery);
$dbArray = dbResultIntoKey Array($result);
and this is the function:
function dbResultIntoKey Array($result) {
while ($row = mysql_fetch_obj ect($result, MYSQL_ASSOC)) {
$dbArray[] = $row;
}
return $dbArray;
}
then if the query came back empty I get an error message: not a valid
mysql resource.

What is the correct way of handling this? If I was dealing with an
array I'd start the function with something like:

if (is_array($arra y)) {

}

and then I wouldn't have to worry about error messages if it turned
the variable wasn't an array. What is the similar test for database
returns?


if (is_resource($r esult))
HTH, Jochen
--
Jochen Daum - CANS Ltd.
PHP DB Edit Toolkit -- PHP scripts for building
database editing interfaces.
http://sourceforge.net/projects/phpdbedittk/
Jul 16 '05 #2
On 14 Sep 2003 20:38:09 -0700, lk******@geocit ies.com (lawrence) wrote:
If I do something like :

$result = mysql_query($qu ery);
You've ignored possible errors here.
$dbArray = dbResultIntoKey Array($result);
and this is the function:
function dbResultIntoKey Array($result) {
while ($row = mysql_fetch_obj ect($result, MYSQL_ASSOC)) {
$dbArray[] = $row;
}
return $dbArray;
}

then if the query came back empty I get an error message: not a valid
mysql resource.
No, that'd be because an error occurred.

An empty result set is a perfectly valid result set, and will not give this
error.
What is the correct way of handling this? If I was dealing with an
array I'd start the function with something like:

if (is_array($arra y)) {

}

and then I wouldn't have to worry about error messages if it turned
the variable wasn't an array. What is the similar test for database
returns?


$result = mysql_query($qu ery);

if (!$result) { /* error, details available from mysql_error() */ }

mysql_query returns either a resource identifier after excuting the query
(which may or may not have any rows), or FALSE in the case of a fatal error.

--
Andy Hassall (an**@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 16 '05 #3
Andy Hassall <an**@andyh.co. uk> wrote in message news:<c9******* *************** **********@4ax. com>...
On 14 Sep 2003 20:38:09 -0700, lk******@geocit ies.com (lawrence) wrote:
If I do something like :

$result = mysql_query($qu ery);
You've ignored possible errors here.
$dbArray = dbResultIntoKey Array($result);
and this is the function:
function dbResultIntoKey Array($result) {
while ($row = mysql_fetch_obj ect($result, MYSQL_ASSOC)) {
$dbArray[] = $row;
}

return $dbArray;
}

then if the query came back empty I get an error message: not a valid
mysql resource.


No, that'd be because an error occurred.

An empty result set is a perfectly valid result set, and will not give this
error.


Are you sure? The error only occurs when the result set is empty.
Otherwise everything works fine.



What is the correct way of handling this? If I was dealing with an
array I'd start the function with something like:

if (is_array($arra y)) {

}

and then I wouldn't have to worry about error messages if it turned
the variable wasn't an array. What is the similar test for database
returns?


$result = mysql_query($qu ery);

if (!$result) { /* error, details available from mysql_error() */ }

mysql_query returns either a resource identifier after excuting the query
(which may or may not have any rows), or FALSE in the case of a fatal error.



Thanks. I'll use this test you write out from now on.
Jul 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
8874
by: tornado | last post by:
Hi all, I am pretty new to PHP. I was reading PHP manual and trying out the example from 2nd chapter (A simple Tutorial). When i try to print the variable as given in the example it returns a empty value instead of returning the browser type. Here is the line which i am using in my code and from manual: <?php echo $_SERVER; ?>
5
7090
by: Adrian Parker | last post by:
Hi. I have a date time picker in my program which uses ADO to read from an Access database. It works perfectly, unless the database is empty (no records) when opened. When you try to open an empty database, the date time picker returns error 545. Any attempts to progmatically add new records after the empty database has been opened...
125
14575
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
2
2507
by: serge | last post by:
/* This is a long post. You can paste the whole message in the SQL Query Analyzer. I have a scenario where there are records with values pointing to wrong records and I need to fix them using an Update statement. I have a sample code to reproduce my problem. To simplify the scenario I am trying to use Order related tables to explain a...
9
5366
by: Thelma Lubkin | last post by:
I've been looking at code that handles string manipulation and I keep seeing variable names, and function names, too, followed by a '$'. I've also found a variable followed by a '%' symbol, but I've only seen that once. Can someone please explain this for me? I haven't been able to find anything about it. thanks, --thelma
15
6715
by: Greenhorn | last post by:
Hi, when a function doesn't specify a return type ,value what value is returned. In the below programme, the function sample()is returning the value passed to 'k'. sample(int); main() { int i = 0,j; j = sample(0);
6
1437
by: RichG | last post by:
Does anyone remember this: Set datapath to "c:\somewhere" dNetUse "SomeTable" do some db work I asked the question a couple of days ago about how to attach a recordset or some form of data to a datagrid. I was using DAO. The lack of responses seemed to indicate that the question was out of the range of an answer. So I started looking...
6
2442
by: \Frank\ | last post by:
I trying to learn what a Bitmap is. Not a Managed Bitmap Object but one that, for example, comes from the clipboard with CF_BITMAP. I'm guessing that a CompatableBitmap is an array of indices that point to the colors in a Palette of the display driver. So if I get a bitmap via CF_BITMAP I need to also get a Palette off the clipboard and...
3
12263
ADezii
by: ADezii | last post by:
Null as it relates to database development is one of life's little mysteries and a topic of total confusion for novices who venture out into the database world. A Null Value is not zero (0), a zero (0) length string, an empty Field, or no value at all - so exactly what is Null? The purpose of this Topic is hopefully to explain what a Null Value...
0
7664
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7918
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7436
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5981
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4958
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1897
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1022
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.