What does this error mean?

Hey,

I was just wondering what this error meant on this page:-

http://www.glofamily.com/inner/member-discounts/

If you see the Dropdown next to the "Refine your search here : " section…

And click on the search button you will see an error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /domains/glofamily.com/http/inner/member-discounts/index.php on line 87

Which is on this line:-


while($row = mysql_fetch_array($search)){...

As far as i’m aware it’s not recognizing the $search? Any ideas?

Thanks

It means that the variable $search does not contain a correct resource provided by the mysql_query function.

for more info see:
http://php.net/manual/en/function.mysql-fetch-array.php

One of the reasons could be that there was an error on your DB query or something… That’s the place to start looking :slight_smile:

$search should be the result of executing a database query

Likely, this query contains an error, so it does not contain a result set from which you can extract a row using mysql_fetch_array

You should check the results of your mysql_query() calls before using them to catch mistakes/typos

Thanks :slight_smile:

Well i have tried looking but cant seem to find the error… I have the following code:

Function


    public function selectByCategory($category){

        $sql = "SELECT * FROM tbl_discounts WHERE 
        deleted = 0 AND category = $category";
        $result = mysql_query($sql) or mysql_error();
        return $result;
    }

Then on the page itself i have:


			if(isset($_POST['submit']))
			{
			    	$search = Discount::selectByCategory($_POST['category']);
			        $i = 0;
			        while($row = mysql_fetch_array($search)){
			        ?>
			        <tr>
					<td class="td-company"><?=$row['company_name']?></td>
					<td class="td-discount"><?=$row['discount']?></td>
					
					<? if (isset($_SESSION['loggedin'])) {?>
					<td class="td-redeem" style="text-align:center"><?=$row['code']?></td>
					<?
					} else { ?>
					<td class="td-redeem"><a href="<?=$siteaddress?>join-now"><img src="../images/join.jpg" alt="" border="0"/></a> 
					<? } ?>		
					</td>
					</tr>
			        <?  
			        }
			}

I have executed the SQL itself and it comes up like so:

SELECT * FROM tbl_discounts WHERE deleted = 0 AND category = Health & Leisure

Which is correct…

Any ideas what i am doing wrong?
:confused:

That is not correct. String values need to be enclosed in single quotes else the parser can’t tell what words are supposed to be literal strings and what words are supposed to be part of the query syntax.

SELECT * FROM tbl_discounts WHERE deleted = 0 AND category = ‘Health & Leisure’

Yes, I agree with fristi. It looks like a problem with the query syntax

			SELECT * FROM tbl_discounts WHERE 
        deleted = 0 AND category = Health & Leisure

Did you enclose the “Health & Leisure”?

EDIT: Seems I’m slow today and Dan beat me too it :wink: Time for a coffee.

Hmmmm. It also gives errors for

SELECT * FROM tbl_discounts WHERE deleted = 0 AND category = Hotels
SELECT * FROM tbl_discounts WHERE deleted = 0 AND category = Lifestyle
SELECT * FROM tbl_discounts WHERE deleted = 0 AND category = Restaurants
SELECT * FROM tbl_discounts WHERE deleted = 0 AND category = Retai

I still think it’s a good idea to enclose the values. And using LIKE instead of = should work better for strings.

It is not optional. String literals are always enclosed in single quotes.

LIKE should not be substituted for equality unless you intend to use wildcards.

Hey,

Thanks guys it works now it was the sql i missed out the single quotes :slight_smile:

Much appreciated

Thanks again


  $result = mysql_query($sql) or mysql_error();

mysql_error() returns a string. If you want the string to be output(so you can see it), you need to do use echo/print/die etc…