Page 1 of 1

PHP/MYSQL Query - error response

Posted: Sat May 24, 2003 4:24 am
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?

Posted: Sat May 24, 2003 4:29 am
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());

Give the query

Posted: Sat May 24, 2003 4:50 am
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:

Posted: Sat May 24, 2003 4:55 am
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.

Posted: Sat May 24, 2003 7:33 am
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

It seems right (check space)

Posted: Sat May 24, 2003 7:48 am
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.

Posted: Sat May 24, 2003 8:19 am
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

Posted: Sat May 24, 2003 1:57 pm
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]%'";

Is Resultset empty?

Posted: Sat May 24, 2003 11:39 pm
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.