mat3000000 Posted October 27, 2010 Share Posted October 27, 2010 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 More sharing options...
AbraCadaver Posted October 27, 2010 Share Posted October 27, 2010 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 https://forums.phpfreaks.com/topic/217035-php-mysql-error-help/#findComment-1127238 Share on other sites More sharing options...
rwwd Posted October 27, 2010 Share Posted October 27, 2010 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 https://forums.phpfreaks.com/topic/217035-php-mysql-error-help/#findComment-1127262 Share on other sites More sharing options...
mat3000000 Posted October 31, 2010 Author Share Posted October 31, 2010 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??!! Link to comment https://forums.phpfreaks.com/topic/217035-php-mysql-error-help/#findComment-1128691 Share on other sites More sharing options...
Pikachu2000 Posted October 31, 2010 Share Posted October 31, 2010 Just a misplaced parenthesis . . . $sql="SELECT `ID`, `Stock Number`, `Name` FROM `Contacts` WHERE (`Stock Number` LIKE '%$search%' OR Name LIKE '%$search%')"; Link to comment https://forums.phpfreaks.com/topic/217035-php-mysql-error-help/#findComment-1128711 Share on other sites More sharing options...
mat3000000 Posted October 31, 2010 Author Share Posted October 31, 2010 Thanks to everyone who contributed, I did it now, Cheers for the excellent help! Link to comment https://forums.phpfreaks.com/topic/217035-php-mysql-error-help/#findComment-1128712 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.