Jump to content

PHP mysql error help


mat3000000

Recommended Posts

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Number LIKE '%TEST%' OR Name LIKE '%TEST%'' at line 1

 

Is the error message i get...  Here is my code...

<?php 
  $search=$_POST['searchform']; 
  if(!$search) die('Please enter a search');
  //connect  to the database 
  $db=mysql_connect  ("xxxxxx", "xxxx",  "xxxx") or die ('I cannot connect to the database  because: ' . mysql_error()); 
  //-select  the database to use 
  $mydb=mysql_select_db("wadkin"); 
  //-query  the database table 
  $sql="SELECT  ID, Stock Number, Name FROM Contacts WHERE Stock Number LIKE '%" . $search .  "%' OR Name LIKE '%" . $search ."%'"; 
  //-run  the query against the mysql query function 
  $result=mysql_query($sql) or die (mysql_error()); 
  //-create  while loop and loop through result set 
  while($row=mysql_fetch_array($result)){ 
          $Stock  =$row['Stock Number']; 
          $Name=$row['Name']; 
          $ID=$row['ID']; 

  //-display the result of the array 
  echo "<ul>\n"; 
  echo "<li>" . "<a  href=\"search.php?id=$ID\">"   .$Stock . " " . $Name .  "</a></li>\n"; 
  echo "</ul>"; 
  
} 

?> 

 

Can someone correct for me please????!!!!!

 

Link to comment
https://forums.phpfreaks.com/topic/217035-php-mysql-error-help/
Share on other sites

I'm not sure if you can use spaces in a column name, I've never thought to try it, but if you can it definitely needs to be quoted with backticks:

 

$sql="SELECT  ID, `Stock Number`, Name FROM Contacts WHERE `Stock Number` LIKE '%" . $search .  "%' OR Name LIKE '%" . $search ."%'"; 

if you do use spaces in column name you need to backtick (```) them so that they are "acceptable" to mysql, this works if you use reserved words too, though it isn't a preferred method, it does the trick...

 

and your sql should have the OR part parenthesised I think:-

$sql="SELECT  ID, `Stock Number`, `Name` FROM `Contacts` WHERE `Stock Number` (LIKE '%".$search . "%' OR `Name` LIKE '%".$search."%' )"; 

 

Though i could be wrong there, it's late and I'm quite tired...

 

Rw

 

Thanks for your replies, but now i have another problem...

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIKE '%f%' OR `Name` LIKE '%f%' )' at line 1

 

Help??!!  :confused:

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.