PHP/MYSQL Query - error response

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jarow
Forum Commoner
Posts: 83
Joined: Tue Jan 28, 2003 2:58 am

PHP/MYSQL Query - error response

Post by jarow »

I am getting the following error response:

You have an error in your SQL syntax near 'LIKE '%varphylum%' AND clase LIKE '%varclase%' AND orden LIKE '%varord%' AND fam' at line 1


for the following code:

Code: Select all

require_once('../Connections/connfauna.php'); 

mysql_select_db($database_connfauna, $connfauna);
$query_rsadd = "SELECT * FROM fauna";
if(isset($_GET['phylum'])){   
    $varphylum = $_GET['phylum'];
    $query_rsadd .="WHERE phylum LIKE '%varphylum%'";
if(isset($_GET['clase'])){
      $varclass = $_GET['clase'];
      $query_rsadd .=" AND clase LIKE '%varclase%'";
if(isset($_GET['orden'])){
        $varord = $_GET['orden'];
        $query_rsadd .=" AND orden LIKE '%varord%'";
if(isset($_GET['familia'])){
          $varfam = $_GET['familia'];
          $query_rsadd .=" AND familia LIKE '%varfam%'";
if(isset($_GET['genero'])){
            $vargen = $_GET['genero'];
            $query_rsadd .=" AND genero LIKE '%vargen%'";    
          }    
        }        
      }    
    }
}
$rsadd = mysql_query($query_rsadd, $connfauna) or die(mysql_error());
$row_rsadd = mysql_fetch_assoc($rsadd);
$totalRows_rsadd = mysql_num_rows($rsadd);


Any ideas why the error message?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

might be easier if we see the whole query

Code: Select all

$rsadd = mysql_query($query_rsadd, $connfauna) or die($query_rsadd. ': '.mysql_error());
User avatar
coolpravin
Forum Newbie
Posts: 20
Joined: Mon May 19, 2003 12:56 am
Location: India

Give the query

Post by coolpravin »

$rsadd = mysql_query($query_rsadd, $connfauna) or die(mysql_error());
$row_rsadd = mysql_fetch_assoc($rsadd);
$totalRows_rsadd = mysql_num_rows($rsadd);
Before this do
echo "query is ".$query_rsadd;
THen place it on the forum here.
Then it will be easy to debug. :roll:
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

Code: Select all

function mysqlquery($query){
  if(!$result = mysql_query($query)){
    echo "mysqlerror: ".mysql_error()."<BR>";
    echo "query: ".$query;
    exit;
  }
  return $result;
}
If you now use mysqlquery instead of mysql_query you dont have to worry about error handelling and your not makeing the msg and the dump with each mysql_query.
jarow
Forum Commoner
Posts: 83
Joined: Tue Jan 28, 2003 2:58 am

Post by jarow »

Thanks for the response...when I did as you suggested I saw that I was missing a space between fauna and WHERE.

After I corrected that I got no error response but neither did any of the values post in the echos.

Does anything occur to you why that might be? Here's the entire page:
(By the way the method of the form that posts to this page is GET)

Code: Select all

<?php
<?php require_once('../Connections/connfauna.php'); ?>
<?php
mysql_select_db($database_connfauna, $connfauna);
$query_rsadd = "SELECT * FROM fauna";
if(isset($_GET['phylum'])){   
    $varphylum = $_GET['phylum'];
    $query_rsadd .=" WHERE phylum LIKE '%varphylum%'";
if(isset($_GET['clase'])){
      $varclass = $_GET['clase'];
      $query_rsadd .=" AND clase LIKE '%varclase%'";
if(isset($_GET['orden'])){
        $varord = $_GET['orden'];
        $query_rsadd .=" AND orden LIKE '%varord%'";
if(isset($_GET['familia'])){
          $varfam = $_GET['familia'];
          $query_rsadd .=" AND familia LIKE '%varfam%'";
if(isset($_GET['genero'])){
            $vargen = $_GET['genero'];
            $query_rsadd .=" AND genero LIKE '%vargen%'";    
          }    
        }        
      }    
    }
}
$rsadd = mysql_query($query_rsadd, $connfauna) or die(mysql_error());
$row_rsadd = mysql_fetch_assoc($rsadd);
$totalRows_rsadd = mysql_num_rows($rsadd);

?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<p><?php echo $row_rsadd['phylum']; ?> </p>
<p><?php echo $row_rsadd['clase']; ?></p>
<p> <?php echo $row_rsadd['orden']; ?> </p>
<p><?php echo $row_rsadd['familia']; ?> </p>
<p><?php echo $row_rsadd['genero']; ?> </p>
<p><?php echo $row_rsadd['especie']; ?> </p>
</body>
</html>?>
Any ideas would be most appreciated
Last edited by jarow on Sat May 24, 2003 7:49 am, edited 1 time in total.
User avatar
coolpravin
Forum Newbie
Posts: 20
Joined: Mon May 19, 2003 12:56 am
Location: India

It seems right (check space)

Post by coolpravin »

The query
SELECT * FROM faunaWHERE phylum LIKE '%varphylum%' AND clase LIKE '%varclase%' AND orden LIKE '%varord%' AND familia LIKE '%varfam%' AND genero LIKE '%vargen%'
seems to be very correct.

Check if there is a space beteween fauna and WHERE
These should be space.
But it seems absent in your query posted above.
Is this solves your problem?
Otherwise you have to give database also so that i can check
live execution of it.
But the query seems 100% right.
jarow
Forum Commoner
Posts: 83
Joined: Tue Jan 28, 2003 2:58 am

Post by jarow »

Thanks coolpravin...right about the space between fauna and WHERE

but for some reason can't get it to work...edited my previous message with the entire page...

Does anything out of the ordinary seem to be there in your opinion? Any clues why the results don't print out on the page?

Many thanks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

$varclass = $_GET['clase'];
$query_rsadd .=" AND clase LIKE '%varclase%'";
probably you want the parameter substituted in your query string. If so you have to mark it for php as variable, also check for typos varclase != varclass

Code: Select all

$varclass = $_GET['clase'];
$query_rsadd .=" AND clase LIKE '%$varclass%'";
or without an additional variable

Code: Select all

$query_rsadd .=" AND clase LIKE '%$_GET[clase]%'";
User avatar
coolpravin
Forum Newbie
Posts: 20
Joined: Mon May 19, 2003 12:56 am
Location: India

Is Resultset empty?

Post by coolpravin »

May be this is happening cause your result set is empty.

Now once again after

Code: Select all

<?php
$totalRows_rsadd = mysql_num_rows($rsadd); 
?>
Use

Code: Select all

<?php
echo "Rows are ".$totalRows_rsadd;
?>
If this is 0 then there is problem in your query
As volka said you may have missing the variable names and putting the
names directly.If so use the proper variable names.
Post Reply