Jump to content

mysql_fetch_row Warning:


JDawg

Recommended Posts

I hope this question makes sense. Does mysql_fetch_row function have a limit on how many subsequent times in can be used in a script? I have a script in which I use the mysql_fetch_row function several times throughout the script. The first and second times I use it, it returns the results perfectly. But it gives me this: Warning: Supplied argument is not a valid MySQL result resource, on the third and fourth time I try using that function.

 

I tried using mysql_free_result because I have mysql_fetch_row($result). This hasn\'t helped, and it may not even be using it for the right reason.

 

Any help is greatly appreciated. Thanks!

 

JDawg

Link to comment
Share on other sites

Thanks for the conformation on the mysql_free_results. I wasn\'t sure if I was even using it in the right place.

 

I would post my code, but there is a ton of it in the script. That could be the problem, I could be doing things the hard way. :P

 

Basically, what I am doing is this.

 

1. I have a select statement:

$sql = \"SELECT * FROM tbl_stores WHERE zipcode = \'\".$zipcode.\"\'\";

 

2. Then I have this code:

$result = mysql_query($sql,$dbcnx);

while ($row = mysql_fetch_row($result)) {

 

3. Then I set all the $row[#] equal to a variable. Then I echo all the data

I want to show

 

Then I have another select statement

 

$sql = \"SELECT * FROM tbl_stores WHERE $relatedzip = zipcode\";

 

$relatedzip is a variable I set to equal a certain $row[#] earlier in the script.

 

Then I have this code again.

 

$result = mysql_query($sql,$dbcnx);

while ($row = mysql_fetch_row($result)) {

 

Then I set the $row[#] = to variables again, and echo the new results.

Now I may be doing this the wrong way, but it is the only way I could think of doing it. I write the all of the red code two more times, only I use different variable each time in the select statement, so it pulls different data. When I try to echo the results it gives me a Warning: Supplied argument is not a valid MySQL result resource. I can comment out the first section of red code and the second set of red code will echo the results. Or I can comment out the first two sections of red code and the third section works.

 

I apologize for the length of this reply. Again, any help is greatly appreciated. I realize there could be a much better way of doing it, so please feel free to restructure the way I am doing it.

 

Thanks,

 

JDawg

Link to comment
Share on other sites

 

1. I have a select statement:  

$sql = \\\"SELECT * FROM tbl_stores WHERE zipcode = \'\\\".$zipcode.\\\"\'\\\";  

since you are using double quotes, no need to use the concatenation:  

\\\"SELECT * FROM tbl_stores WHERE zipcode = \'$zipcode\'\\\"

 

2. Then I have this code:  

$result = mysql_query($sql,$dbcnx);  

while ($row = mysql_fetch_row($result)) {  

good.

 

3. Then I set all the $row[#] equal to a variable. Then I echo all the data  

I want to show  

 

Then I have another select statement  

 

$sql = \\\"SELECT * FROM tbl_stores WHERE $relatedzip = zipcode\\\";  

 

$relatedzip is a variable I set to equal a certain $row[#] earlier in the script.  

 

Then I have this code again.  

 

$result = mysql_query($sql,$dbcnx);  

while ($row = mysql_fetch_row($result)) {  

 

Then I set the $row[#] = to variables again, and echo the new results.  

Now I may be doing this the wrong way, but it is the only way I could think of doing it. I write the all of the red code two more times, only I use different variable each time in the select statement, so it pulls different data.  

i\'m confused on what you\'re trying to do here..

 

 

Link to comment
Share on other sites

I will try to explain it in detail. I am not trying to insult your intelligence, I just don\'t want to leave anything out. Hope this makes sense. Thanks for your help.

 

I am trying to build a store locator database. I have a table that holds the storename, address, city, state, zipcode, relatedzip, relatedzip2, and relatedzip3.

 

When someone types in the zipcode they live in, it first trys to find a match in the zipcode field. If it finds a match it displays the pertinent information for that store.

 

I have relatedzip, relatedzip2, and relatedzip3, because even though a store may not be in the zipcode that the person typed in, they could actually be closer to a store that wasn\'t in there zipcode, than to one that was actually in there zipcode. The zipcodes I have in the relatedzip, relatedzip2, and relatedzip3 all relate to an actual store. So for instance, say someone typed in zipcode 55555. First I selected all from the table where that 55555 matched up with the zipcode field. If there was a match, I displayed the pertinent store data. Then I want to check and see if relatedzip, relatedzip2, and relatedzip3 have zipcodes in them. So for instance, say that row that matched up with 55555, had 66666 in the relatedzip field. I wrote this select statement: $sql = \"SELECT * FROM tbl_stores WHERE $relatedzip = zipcode\"; to check for that. It takes 66666 and searches for a match in the zipcode field, and then displays the data for that store.

 

So what I want to happen is, if relatedzip, relatedzip2, and relatedzip3 all have zipcodes in them, I want to display those results as well. What is happening is, it takes the zipcode they entered, finds a match in the zipcode field, and displays the data. Then it sees if there is anything in relatedzip, if there is, it displays it. Then it looks in relatedzip2, and if there is something in there it is supposed to display it. Well it doesn\'t, unless I comment out the code for relatedzip, then it will display the data for relatedzip2.

 

Once again, sorry for the length of my reply. I really appreciate your help.

 

JDawg

Link to comment
Share on other sites

Just for reference, the error :

Warning: Supplied argument is not a valid MySQL result resource

 

means one thing.. you tried to retrieve results where the query returned FALSE (failed). With knowledge of the built-in MySQL functions you\'ll notice that when you query the database, you will get 1 of 2 possibilities returned:

 

a) a mysql result resource/link

B) FALSE [boolean]

 

A simple way to avoid getting this error is to perform some error checking before attempting to retrieve the results:

 

if ($result != FALSE) {

 #do this

} else {

 # do this [error]

}

 

This method will save you debugging time in the future.. probably a good idea to start doing it.

 

Hope that helps.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.