Solved

Getting mysql_num_rows(): supplied argument is not a valid MySQL result resource in error

Posted on 2008-06-26
14
612 Views
Last Modified: 2013-12-12
Hi Experts

I am gettting the following error

<b>Warning</b>:  mysql_num_rows(): supplied argument is not a valid MySQL result resource in <b>/home/xxx/public_htmlxxx/</b> on line <b>64</b><br />

the line of code it is refering to is

$result = $dbHan->myQuery("SELECT lb_companyName, lb_defaultUser FROM uksectors WHERE REPLACE(lb_postcodeSector,'$field','') like '".substr($pc,0,$len)."'");
        if(mysql_num_rows($result) == 0){

Line 64 is the start of the if statement

Now this works perfect on my local machine but throughs the error on the live server. The only changes made where the database connections and these are correct. Please could some one look at the code snippet and see if anything is not correct.

The same error appears on line 64 and 69 and both are related to this

if(mysql_num_rows($result)==0){



Thanks for any help

Matt

<?php

require_once("classes.php");  // require the classes file to have access to the DBHandler methods
 

// if the GET variable passed in from the form is not empty, proceed

if(!empty($_GET['postcode'])){

    //sleep(2);   // this is just a pause in the system to show the gif - uncomment to make sure gif is shown

    $xml = "<result>";     // create $xml variable and assign it the opening tag for the xml document that needs to be passed back to the javascript 

    $pc = trim(htmlspecialchars($_GET['postcode']));  // get rid of any hack jscript chars and leading or trailing white-spaces and assign the value to $pc

    $len = strlen($pc);    // get the length of the entered postcode

    // if there is a space found in the postcode set the search parameter to an empty string and set min variable for forloop to 3

    if(stristr($pc, ' ')){ 

        $field = '';

        $min = 3;

    } else {

        // is there is no space found, set search parameter for mysql function to a space, and set min variable to 2

        $field = ' ';

        $min = 2;

    }

    // if the postcode length is large, start here

    if($len > 6){

        $len = 6;  // regardless of how many chars there are in the postcode, start matching at 6 chars, which is the largest size of a postcode in the DB

        // using substr function in query string to cut the postcode size down to 6 chars

        ########  add in ,lb_caseOwner when field is added

        $result = $dbHan->myQuery("SELECT lb_companyName, lb_defaultUser FROM uksectors WHERE REPLACE(lb_postcodeSector,'$field','') like '".substr($pc,0,$len)."'");

        // if there were no search results, go here

        if(mysql_num_rows($result) == 0){

            // loop backwards, shortneing the size of the entered postcode by one char every iteration, until the min size a postcode in the db can be is reached

            for($i=$len; $i>$min; $i--){

                ########  add in ,lb_caseOwner when field is added

                $result = $dbHan->myQuery("SELECT lb_companyName, lb_defaultUser FROM uksectors WHERE REPLACE(lb_postcodeSector,'$field','') like '".substr($pc,0,$i)."'");

                // if there were no results, continue to the next iteration of the loop

                if(mysql_num_rows($result)==0){

                    continue;

                }

                else if(mysql_num_rows($result)==1){

                    // if there was one match, fetch the result set

                    $row = mysql_fetch_assoc($result);

                    // add the result to the xml variable to be loaded into the xml document

                    ########  add in <caseOwner>$row[caseOwner] when field is added to table

                    $xml .= "<companyName>$row[lb_companyName]</companyName>";

					$xml .= "<caseOwner>$row[lb_defaultUser]</caseOwner>";

                    break;  // break out of the loop

                }

            }

            

            // after the loop has finished, if the $xml variable is unchanged, set a default company name of 'Pending'

            if($xml == "<result>"){

                $xml .= "<companyName>Pending</companyName>";

				$xml .= "<caseOwner>Pending</caseOwner>";    

            }

            

        } else if(mysql_num_rows($result) == 1){

            // else if there was one result, fetch the result set and load it into the variable

            $row = mysql_fetch_assoc($result);

            ########  add in <caseOwner>$row[caseOwner] when field is added to table

            $xml .= "<companyName>$row[lb_companyName]</companyName>";

			$xml .= "<caseOwner>$row[lb_defaultUser]</caseOwner>";    

        }

    } else {    // if the length of the postcode was lesss than 6 chars

        // same logic as above for remainder of else - we just had a different starting point

        // this logic is only here to cut down on loop iterations above

        ########  add in ,lb_caseOwner when field is added

        $result = $dbHan->myQuery("SELECT lb_companyName, lb_defaultUser FROM uksectors WHERE REPLACE(lb_postcodeSector,'$field','') like '".substr($pc,0,$len)."'");

        if(mysql_num_rows($result) == 0){

            

            for($i=$len; $i>$min; $i--){

                ########  add in ,lb_caseOwner when field is added

                $result = $dbHan->myQuery("SELECT lb_companyName, lb_defaultUser FROM uksectors WHERE REPLACE(lb_postcodeSector,'$field','') like '".substr($pc,0,$i)."'");

                if(mysql_num_rows($result)==0){

                    continue;

                }

                else if(mysql_num_rows($result)==1){

                    $row = mysql_fetch_assoc($result);

                    ########  add in <caseOwner>$row[caseOwner] when field is added to table

                    $xml .= "<companyName>$row[lb_companyName]</companyName>";

					$xml .= "<caseOwner>$row[lb_defaultUser]</caseOwner>";

                    break;

                }

            }

            

            if($xml == "<result>"){

                $xml .= "<companyName>Pending</companyName>";

				$xml .= "<caseOwner>Pending</caseOwner>";    

            }

            

        } else if(mysql_num_rows($result) == 1){

            $row = mysql_fetch_assoc($result);

            ########  add in <caseOwner>$row[caseOwner] when field is added to table

            $xml .= "<companyName>$row[lb_companyName]</companyName>";

			$xml .= "<caseOwner>$row[lb_defaultUser]</caseOwner>";    

        }    

    }

    

    // add a closing tag to the xml variable for the xml document

    $xml .= "</result>";

    // declare header document content type to be text/xml

    header('Content-type: text/xml'); 

    echo $xml;  // ehco the document to the awaiting javascript ajax script

}

/*else {

    $xml = "<result>";

    $xml .= "<companyName>Pending</companyName>";

    $xml .= "</result>";

    header('Content-type: text/xml'); 

    echo $xml;    

}*/
 

?>

Open in new window

0
Comment
Question by:matthewdacruz
  • 6
  • 4
  • 3
  • +1
14 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21873543
that error means that the query failed to run.
check out the mysql error generated, and the sql itself:

$query = "SELECT lb_companyName, lb_defaultUser FROM uksectors WHERE REPLACE(lb_postcodeSector,'$field','') like '".substr($pc,0,$len)."'";
$result = $dbHan->myQuery($query);
if (!$result)
{
  print "query failed: $query <br/> " . mysql_error();
}
else
{
        if(mysql_num_rows($result) == 0){ 
}

Open in new window

0
 

Author Comment

by:matthewdacruz
ID: 21873577
Do I add this to the page?
0
 

Author Comment

by:matthewdacruz
ID: 21873640
Added the code and get the following error

<br />

<b>Parse error</b>:  syntax error, unexpected $end in <b>/home/psv1rtua/public_html/beta/global/fetchcompanyname

.php</b> on line <b>121</b><br />
<?php

require_once("classes.php");  // require the classes file to have access to the DBHandler methods
 

// if the GET variable passed in from the form is not empty, proceed

if(!empty($_GET['postcode'])){

    //sleep(2);   // this is just a pause in the system to show the gif - uncomment to make sure gif is shown

    $xml = "<result>";     // create $xml variable and assign it the opening tag for the xml document that needs to be passed back to the javascript 

    $pc = trim(htmlspecialchars($_GET['postcode']));  // get rid of any hack jscript chars and leading or trailing white-spaces and assign the value to $pc

    $len = strlen($pc);    // get the length of the entered postcode

    // if there is a space found in the postcode set the search parameter to an empty string and set min variable for forloop to 3

    if(stristr($pc, ' ')){ 

        $field = '';

        $min = 3;

    } else {

        // is there is no space found, set search parameter for mysql function to a space, and set min variable to 2

        $field = ' ';

        $min = 2;

    }

    // if the postcode length is large, start here

    if($len > 6){

        $len = 6;  // regardless of how many chars there are in the postcode, start matching at 6 chars, which is the largest size of a postcode in the DB

        // using substr function in query string to cut the postcode size down to 6 chars

        ########  add in ,lb_caseOwner when field is added

        $result = $dbHan->myQuery("SELECT lb_companyName, lb_defaultUser FROM uksectors WHERE REPLACE(lb_postcodeSector,'$field','') like '".substr($pc,0,$len)."'");

        // if there were no search results, go here

        if(mysql_num_rows($result) == 0){

            // loop backwards, shortneing the size of the entered postcode by one char every iteration, until the min size a postcode in the db can be is reached

            for($i=$len; $i>$min; $i--){

                ########  add in ,lb_caseOwner when field is added

                $result = $dbHan->myQuery("SELECT lb_companyName, lb_defaultUser FROM uksectors WHERE REPLACE(lb_postcodeSector,'$field','') like '".substr($pc,0,$i)."'");

                // if there were no results, continue to the next iteration of the loop

                if(mysql_num_rows($result)==0){

                    continue;

                }

                else if(mysql_num_rows($result)==1){

                    // if there was one match, fetch the result set

                    $row = mysql_fetch_assoc($result);

                    // add the result to the xml variable to be loaded into the xml document

                    ########  add in <caseOwner>$row[caseOwner] when field is added to table

                    $xml .= "<companyName>$row[lb_companyName]</companyName>";

					$xml .= "<caseOwner>$row[lb_defaultUser]</caseOwner>";

                    break;  // break out of the loop

                }

            }

            

            // after the loop has finished, if the $xml variable is unchanged, set a default company name of 'Pending'

            if($xml == "<result>"){

                $xml .= "<companyName>Pending</companyName>";

				$xml .= "<caseOwner>Pending</caseOwner>";    

            }

            

        } else if(mysql_num_rows($result) == 1){

            // else if there was one result, fetch the result set and load it into the variable

            $row = mysql_fetch_assoc($result);

            ########  add in <caseOwner>$row[caseOwner] when field is added to table

            $xml .= "<companyName>$row[lb_companyName]</companyName>";

			$xml .= "<caseOwner>$row[lb_defaultUser]</caseOwner>";    

        }

    } else {    // if the length of the postcode was lesss than 6 chars

        // same logic as above for remainder of else - we just had a different starting point

        // this logic is only here to cut down on loop iterations above

        ########  add in ,lb_caseOwner when field is added

        $result = $dbHan->myQuery("SELECT lb_companyName, lb_defaultUser FROM uksectors WHERE REPLACE(lb_postcodeSector,'$field','') like '".substr($pc,0,$len)."'");

		

		

		// test query

		$query = "SELECT lb_companyName, lb_defaultUser FROM uksectors WHERE REPLACE(lb_postcodeSector,'$field','') like '".substr($pc,0,$len)."'";

		$result = $dbHan->myQuery($query);

		if (!$result)

		{

		  print "query failed: $query <br/> " . mysql_error();

		}

		else

		{

        if(mysql_num_rows($result) == 0){

            

            for($i=$len; $i>$min; $i--){

                ########  add in ,lb_caseOwner when field is added

                $result = $dbHan->myQuery("SELECT lb_companyName, lb_defaultUser FROM uksectors WHERE REPLACE(lb_postcodeSector,'$field','') like '".substr($pc,0,$i)."'");

                if(mysql_num_rows($result)==0){

                    continue;

                }

                else if(mysql_num_rows($result)==1){

                    $row = mysql_fetch_assoc($result);

                    ########  add in <caseOwner>$row[caseOwner] when field is added to table

                    $xml .= "<companyName>$row[lb_companyName]</companyName>";

					$xml .= "<caseOwner>$row[lb_defaultUser]</caseOwner>";

                    break;

                }

            }

            

            if($xml == "<result>"){

                $xml .= "<companyName>Pending</companyName>";

				$xml .= "<caseOwner>Pending</caseOwner>";    

            }

            

        } else if(mysql_num_rows($result) == 1){

            $row = mysql_fetch_assoc($result);

            ########  add in <caseOwner>$row[caseOwner] when field is added to table

            $xml .= "<companyName>$row[lb_companyName]</companyName>";

			$xml .= "<caseOwner>$row[lb_defaultUser]</caseOwner>";    

        }    

    }

    

    // add a closing tag to the xml variable for the xml document

    $xml .= "</result>";

    // declare header document content type to be text/xml

    header('Content-type: text/xml'); 

    echo $xml;  // ehco the document to the awaiting javascript ajax script

}

/*else {

    $xml = "<result>";

    $xml .= "<companyName>Pending</companyName>";

    $xml .= "</result>";

    header('Content-type: text/xml'); 

    echo $xml;    

}*/
 

?>

Open in new window

0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 300 total points
ID: 21873666
yes, for debugging.
0
 
LVL 20

Assisted Solution

by:virmaior
virmaior earned 100 total points
ID: 21874435
are you sure that $dbHan is using the mysql extension and returning a mysql result?
0
 

Author Comment

by:matthewdacruz
ID: 21878650
I am not sure, I have just added the code angelII gave me and got more errors. I am new to php and not fully sure how everything works yet
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21878671
what errors?
if it worked before, there must have changed "something".
based on the errors, we shall find out.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Assisted Solution

by:cannot-be-changed
cannot-be-changed earned 100 total points
ID: 21882313
mysql_num_rows(): supplied argument is not a valid MySQL result resource
Is more often than not an invalid reference name used in the num rows parenthesis (). I.E. The query is assigned to var name $query, but the num rows is requesting a result on how many rows where found for a unkown/non-existant query.



"Unexpected $end" in PHP errors is 'usually' where you don't have all required curly braces (not always, just usually)

In your case, on line 8 you have opened a curly brace but not closed it. The closing curly brace on line 10 is the close brace for the opened one on line 9
You have not stated where you want this clause to end, so put a } where you want this clause to end.
Also, on line 9, you have not set PHP to do anything should mysql_num_rows == 0.
Perhaps tell it to echo something like "no rows found"
It just shows you the query was fine but no rows found, rather than a blank screen which confirms nothing really.

As virmaior asked,  are you sure that $dbHan is using the mysql extension and returning a mysql result?
Fix the missing curly brace first as nothing else will work with that problem, then run it again and see what happens now.
And perhaps check the "classes.php" file for the $dbHan (I presume that's where it is)
0
 

Author Comment

by:matthewdacruz
ID: 21932607
Hi All,

we found the problem. It ended up being that the script was written for php 5 and our server was using php 4
0
 

Author Comment

by:matthewdacruz
ID: 21932620
How do i close this question?
0
 

Expert Comment

by:cannot-be-changed
ID: 21934091
There are a few ways to end a question, depending on if you feel an expert has given you the answer, or perhaps a few members have contributed etc.


Have a read of this to help you decide

http://www.experts-exchange.com/help.jsp#hi331
0
 

Expert Comment

by:cannot-be-changed
ID: 21956093
Well, for one I gave an answer to a question correctly which was "how do you close a question"
Although it wasn't the "original" question it was a valid question which received a complete and accurate answer.

Thataside, angelIII posted a potential fix to the issue, which mathewdacruz hadproblems with the fix and then angelIII assisted in the implementationof their provided fix.
Then virmaior also added some advice whichwas also a good point and could well have been a resolve to the issuemathewdacruz was having.
Then angelIII once again assisted with aproblem with his original proposed code fix that mathewdacruz washaving, then I submitted a lengthily description of what is the usualcause of mathewdacruz original problem.

In short, myself,angelIII and virmaior had given possible solutions and advice whichwhere all relative to the original issue and could well have been theresolve to the problem.

As it turned out none of the contributing people assisting where close to finding the problem or a resolve, but as with all troubleshooting, it cantheoretically be one of a hundred things, or a combination of many.
Asit turned out it was an issue that could have been suggested, I.E.mathewdacruz was using a script which was not designed for the PHPversion Apache was running.

But as I said, it could always been one of hundreds of problems due tothe nature and architecture of servers, their software such as Apacheand PHP and then down to the PHP scripts themselves. Often it is notthe actual PHP code that is the problem, in fact it often turns out tobe a problem with variable data being sent to the script in question.I.E. the script posted by the author is fine and it just isn'treceiving correct data.

I don't suggest I should receive the points myself, as I trulydidn't find the resolve or assist in the path to finding the problem.Nor do I really suggest angelIII or virmaior should get them, for thesame reasons as me and because it is up to them if they wish to contestthe withdrawal of the points being awarded.

I'm just making apoint that are we to understand our time and helping others here isonly rewarded should we find the EXACT problem and offer the solution?Or are we to be rewarded for our spending time trying to assist othersin finding what could be potentially hundreds of possible problems.

It's just a point I thought I would bring up. Should the author wish tochange their mind and share the points out to all those who TRIED toassist them, then that is up to them. Otherwise I don't contest thisand remain firm in my opinion that it is up to the author and the sitestaff to determine on what merits rewards should be awarded to peoplehelping.
 

Also, glad you got it sorted mathewdacruz. It was in fact a simpleissue that is easily overlooked in many cases. As is the same with manyissues in a coding environment.

0
 

Expert Comment

by:cannot-be-changed
ID: 21956110
And wher have all my spaces gone between words in my above post?
0
 

Author Closing Comment

by:matthewdacruz
ID: 31470923
Thaks for the help guys
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now