473,498 Members | 891 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

where to look when mysql_query returns false?



www.php.net says:
>>>>>>>> Only for SELECT,SHOW,EXPLAIN or DESCRIBE statements mysql_query()
returns a resource identifier or FALSE if the query was not executed
correctly. For other type of SQL statements, mysql_query() returns TRUE
on success and FALSE on error. A non-FALSE return value means that the
query was legal and could be executed by the server. It does not
indicate anything about the number of rows affected or returned. It is
perfectly possible for a query to succeed but affect no rows or return
no rows.>>>>>>>>>


So if zero rows come back the function will still return true? It only
returns false if something much more serious happened? My script below
is failing somewhere and I'm trying to trouble shoot.



// 11-16-04 - Costin set it up so we do one set of actions for
SELECT, SHOW, EXPLAIN, and DESCRIBE
// queries, and another for all others. For these 4 we return a
link id, otherwise we simply
// return true or false.
if (stristr($query, 'SELECT') || stristr($query, 'SHOW') ||
stristr($query, 'EXPLAIN') || stristr($query, 'DESCRIBE')) {
$this->pp_queryid = @ mysql_query($query, $this->pp_linkid);
if ($this->pp_queryid) {
$this->notifyObject->notify("McDatastoreConnector",
"databaseQuerySuccess");
$this->pp_firstquery = 1;
return $this->pp_queryid;
} else {
$this->resultsObject->error("In query(), in
McDatastoreConnectorMySql, we were not able to run our query.",
"McDatastoreConnectorMySql");
$this->notifyObject->notify("McDatastoreConnector",
"databaseQueryError");
$this->error();
}
} else {
$queried = @ mysql_query($query, $this->pp_linkid);
if ($queried) {
$this->notifyObject->notify("McDatastoreConnector",
"databaseQuerySuccess");
return true;
} else {
$this->resultsObject->error("In query(), in
McDatastoreConnectorMySql, we were not able to run our query.",
"McDatastoreConnectorMySql");
$this->notifyObject->notify("McDatastoreConnector",
"databaseQueryError");
$this->error();
}
}

Jul 17 '05 #1
5 8764
Hi

It would help if you could tell us which part of the code was failing. What
is the value of $query you are using and what results are you expecting for
that query?

--
Paul Barfoot
<lk******@geocities.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...


www.php.net says:
>>>>>>>>> Only for SELECT,SHOW,EXPLAIN or DESCRIBE statements mysql_query()
returns a resource identifier or FALSE if the query was not executed
correctly. For other type of SQL statements, mysql_query() returns TRUE
on success and FALSE on error. A non-FALSE return value means that the
query was legal and could be executed by the server. It does not
indicate anything about the number of rows affected or returned. It is
perfectly possible for a query to succeed but affect no rows or return
no rows.>>>>>>>>>>


So if zero rows come back the function will still return true? It only
returns false if something much more serious happened? My script below
is failing somewhere and I'm trying to trouble shoot.



// 11-16-04 - Costin set it up so we do one set of actions for
SELECT, SHOW, EXPLAIN, and DESCRIBE
// queries, and another for all others. For these 4 we return a
link id, otherwise we simply
// return true or false.
if (stristr($query, 'SELECT') || stristr($query, 'SHOW') ||
stristr($query, 'EXPLAIN') || stristr($query, 'DESCRIBE')) {
$this->pp_queryid = @ mysql_query($query, $this->pp_linkid);
if ($this->pp_queryid) {
$this->notifyObject->notify("McDatastoreConnector",
"databaseQuerySuccess");
$this->pp_firstquery = 1;
return $this->pp_queryid;
} else {
$this->resultsObject->error("In query(), in
McDatastoreConnectorMySql, we were not able to run our query.",
"McDatastoreConnectorMySql");
$this->notifyObject->notify("McDatastoreConnector",
"databaseQueryError");
$this->error();
}
} else {
$queried = @ mysql_query($query, $this->pp_linkid);
if ($queried) {
$this->notifyObject->notify("McDatastoreConnector",
"databaseQuerySuccess");
return true;
} else {
$this->resultsObject->error("In query(), in
McDatastoreConnectorMySql, we were not able to run our query.",
"McDatastoreConnectorMySql");
$this->notifyObject->notify("McDatastoreConnector",
"databaseQueryError");
$this->error();
}
}

Jul 17 '05 #2
lk******@geocities.com wrote:
<snip>
So if zero rows come back the function will still return true? It only
returns false if something much more serious happened? My script below
is failing somewhere and I'm trying to trouble shoot.


First thing you should do is indent your code and remove the '@'s from
function calls.

Second thing is using the mysql_error() function after (almost) all other
mysql_*() calls, for example:

$results = mysql_query();
if ($results) {
/* query was ok -- may have returned zero rows */
} else {
$errmsg = 'Query error: ' . mysql_error();
/* now do something with $errmsg */
}

--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Jul 17 '05 #3
<lk******@geocities.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...


www.php.net says:
>>>>>>>>> Only for SELECT,SHOW,EXPLAIN or DESCRIBE statements mysql_query()
returns a resource identifier or FALSE if the query was not executed
correctly. For other type of SQL statements, mysql_query() returns TRUE
on success and FALSE on error. A non-FALSE return value means that the
query was legal and could be executed by the server. It does not
indicate anything about the number of rows affected or returned. It is
perfectly possible for a query to succeed but affect no rows or return
no rows.>>>>>>>>>>


So if zero rows come back the function will still return true? It only
returns false if something much more serious happened? My script below
is failing somewhere and I'm trying to trouble shoot.


The errors that MySQL returns has nothing to do with what rows and how many
rows are returned. If 'mysql_errno()' returns 0 there was no problem
performing the query (no problems with the query syntax). If it returns
other than 0 then there was. You must use 'mysql_num_rows' or
'mysql_affected_rows':

http://us2.php.net/manual/en/functio...l-num-rows.php
"mysql_num_rows() returns the number of rows in a result set. This command
is only valid for SELECT statements. To retrieve the number of rows affected
by a INSERT, UPDATE or DELETE query, use mysql_affected_rows()."

You should always do something like so:

....
$result = mysql_query($query,$database);
if ((mysql_errno($database) > 0)
{
// query error - do stuff here
}
else
{
// query good - check for results
$num_results = mysql_num_results($database);
if ($num_results == 0)
{
// no results returned - do stuff here
}
else
{
// there are results - do stuff here
}
}
....

Basic, I know but you should get the idea. If you are programming for the
web then you'll see you're errors on the web pages. If you want you can
create a log for informational purposes... try this:

// set these lines at the start (top) of your script
define('LOGFILE',true); // set to false to turn off logging.
$u_id = uniqid('uid');
define('UID',$u_id);

function logfile($txt)
{
if (LOGFILE)
{
$txt = date("G:i:s - ").UID.' - '.$txt.chr(13);
$lf = 'path\\to\\logfile'.date(' D M j - Y').'.txt';
$fp = fopen($lf,'a');
fwrite($fp,$txt,1024);
fclose($fp);
}
}
// ---

then try:

logfile("\n\r--- start of request ---");
....
$result = mysql_query($query,$database);
if ((mysql_errno($database) > 0)
{
// query error - do stuff here
logfile('MySQL: There was an error with the query ->
'.mysql_errno($database).': '.mysql_error($database));
}
else
{
// query good - check for results
$num_results = mysql_num_results($database);
logfile('INFO: Query has been run.');
if ($num_results == 0)
{
// no results returned - do stuff here
logfile('INFO: No results found from query,');
logfile("QUERY: $query");
// by logging the query you can check your values
}
else
{
// there are results - do stuff here
logfile("INFO: Results found -> $num_results");
}
}
....

Norman
---
Avatar Hosting at www.easyavatar.com
Jul 17 '05 #4
That is a very good tip. As near as I can see, there was no real error,
I was creating a false one by the way I'd written my error test in PHP.
Please tell me if my theory is false about this. I now take your advice
and try to capture the mysql_error and I put that in the error message.
It comes up blank, suggesting that there has been no mysql error. My
test was this line:

if (is_resource($this->pp_queryid)) {

Can I assume this fails if there were zero rows returned? You can see
the line in context here:

if (stristr($query, 'SELECT') || stristr($query, 'SHOW') ||
stristr($query, 'EXPLAIN') || stristr($query, 'DESCRIBE')) {
$this->pp_queryid = @ mysql_query($query, $this->pp_linkid);
if (is_resource($this->pp_queryid)) {
$this->notifyObject->notify("McDatastoreConnector",
"databaseQuerySuccess");
$this->pp_firstquery = 1;
return $this->pp_queryid;
} else {
$errmsg = 'Query error: ' . mysql_error();
$this->resultsObject->error("In query(), in
McDatastoreConnectorMySql, we were not able to run our query. $errmsg
.. The query was: '$query' .", "McDatastoreConnectorMySql");
$this->notifyObject->notify("McDatastoreConnector",
"databaseQueryError");
$this->error();
}
} else {

Jul 17 '05 #5
lk******@geocities.com wrote:
if (is_resource($this->pp_queryid)) {

Can I assume this fails if there were zero rows returned?
No.
You can see the line in context here: if (stristr($query, 'SELECT') || stristr($query, 'SHOW') ||
stristr($query, 'EXPLAIN') || stristr($query, 'DESCRIBE')) {
$this->pp_queryid = @ mysql_query($query, $this->pp_linkid);

^^^
Remove this '@' from there and everywhere else it appears in your code.

If the mysql_connect() failed for some reason, you will not know about
it (if it also has an '@') and then you can't expect the mysql_query()
to work.
$conn = mysql_connect() or die('Connection error: ' . mysql_error());

--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Jul 17 '05 #6

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

Similar topics

23
5638
by: ian justice | last post by:
Before i post actual code, as i need a speedyish reply. Can i first ask if anyone knows off the top of their head, if there is a likely obvious cause to the following problem. For the moment i've...
2
2262
by: Troy Lynch | last post by:
I'm looking into writing a Category Hierarchy on a site I've written and am looking for some information about writing this kind of code. Maybe some samples or a book. Any information would be...
3
1633
by: kiqyou_vf | last post by:
hi guys, first of all i'm really new to php so i apologize ahead of time if this is a noob question. ok, so i purchased and read the PHP for the Wolrd Wide Web by Larry Ullman and started to...
5
4065
by: Rachel Weeden | last post by:
I'm working on an ASP Web application, and am having syntax issues in a WHERE statement I'm trying to write that uses the CInt Function on a field. Basically, I want to select records using...
9
1427
by: ward | last post by:
Good morning. I created an edit_task page that allows the user to edit tasks for the database. I entered some text and used some hard returns. Put when I went to view the tasks (using a PHP...
2
2337
by: boyleyc | last post by:
Hi all the following code works perfectly well. Basically it populates a series of check boxes on my form, depending on whether dlookup finds an associated record. The problem i have is that...
3
3300
by: saundra | last post by:
This appears to be the code that is giving me trouble. Can someone help <? include("common.php"); require_once("$HeaderFile"); if(empty($_GET)){ $Start = '0'; }else{ $Start = $_GET;
9
1838
by: lukk3tt0 | last post by:
Hi guys, I found this script: http://hvassing.com/2007/simple-php-login-script-using-session-and-mysql/#comment-31549 but if I try to recall, in a page I created, the variable "username" that...
0
7124
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6998
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7163
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,...
0
7200
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...
1
4904
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4586
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...
0
3090
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...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1416
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 ...

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.