Page 1 of 1

fetch array error when using logical operators

Posted: Tue Dec 03, 2002 12:50 am
by Manx
I'm having some issues with a bit of code that you folks might be able to help me with. When I use the logical operator AND to retrieve rows with mysql_fetch_array it throws an exception. I'm enclosing two examples, one with the AND operator, and one without. The one without works fine, the one with the operator fails. The error I get is
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

Here's the code:

Code: Select all

if ( $links == "Operating Systems" ){


$result=mysql_query ( "select * FROM Links WHERE LinkType = 'Operating Systems'" );											
														 while ($row = mysql_fetch_array($result)){ 
    									 			 printf (nl2br("<a class="red" href="http://$row&#1111;3]" target="_blank">$row&#1111;2]</a><p>$row&#1111;4]</p><br>"));	
														 			
&#125;
&#125;
if ( $links == "Small Business Links" )&#123;


$result=mysql_query ( "select * FROM Links WHERE LinkType =  ('Small Business Links') AND (LinkShowHide = Hide)" );											
														 while ($row = mysql_fetch_array($result)) &#123;
    									 			 printf (nl2br("<a class="red" href="http://$row&#1111;3]" target="_blank">$row&#1111;2]</a><p>$row&#1111;4]</p><br>"));	
																	
&#125;
&#125;

Posted: Tue Dec 03, 2002 5:36 am
by volka
try

Code: Select all

$query = "select * FROM Links WHERE LinkType='Small Business Links' AND LinkShowHide= 'Hide'";
$result=mysql_query($query) or die($query.' :'.mysql_error());

Posted: Tue Dec 03, 2002 6:25 am
by twigletmac
Hi, I've played with your code a little and ended up with this which you might like to try:

Code: Select all

<?php
if ($links == 'Operating Systems') { 
	$sql = "SELECT * FROM Links WHERE LinkType = 'Operating Systems'";
	$result = mysql_query($sql) or die(mysql_error().'<p>'.$sql'</p>');
	while ($row = mysql_fetch_array($result)) {
		echo '<a class="red" href="http://'.$row[3].'" target="_blank">'.$row[2].'</a><p>'.nl2br($row[4]).'</p><br>';
	} 
}

if ($links == 'Small Business Links') { 
	$sql = "SELECT * FROM Links WHERE LinkType = 'Small Business Links' AND LinkShowHide = 'Hide'";
	$result = mysql_query($sql) or die(mysql_error().'<p>'.$sql'</p>');
	while ($row = mysql_fetch_array($result)) {
		echo '<a class="red" href="http://'.$row[3].'" target="_blank">'.$row[2].'</a><p>'.nl2br($row[4]).'</p><br>';
	} 
}
?>
I put the SQL statements into their own variables so that they can be echoed out in order to check for errors. I've added some error handling to the mysql_query() call using or die statements and mysql_error(). The problem was most likely with the way you had used parenthesis in the second SQL statement:

Code: Select all

select * FROM Links WHERE LinkType =  ('Small Business Links') AND (LinkShowHide = Hide)
I've changed it so it now looks like this:

Code: Select all

SELECT * FROM Links WHERE LinkType = 'Small Business Links' AND LinkShowHide = 'Hide'
Finally I changed the printf() call to echo() which would be more appropriate for what you are trying to do.

Mac

Edit: meant to post this before I went to my meeting and managed to preview it instead so here it is an hour or so later, sort of expanding on what volka said.

Posted: Tue Dec 03, 2002 7:34 pm
by Manx
Thanks both of you for your help. Glad I wasn't too far off the mark. I changed the select statement (still not sure why it didn't work the way it was), and changed the printf to an echo and it worked well. I appreciate the help.