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
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 ."%'"; 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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