jealy Posted August 30, 2008 Share Posted August 30, 2008 Hey guys. I'm trying to make a simple page that will query a mysql table for names of movies. I have most of it done however when you click on the letter, it shows all of the results with the letter in the name, I want it to show results that BEGIN with the letter, it's hard to explain but... http://camo.jealy.co.uk = the page Here's the line; $sql="SELECT Name FROM films WHERE Name LIKE '%" . $letter . "%'"; I just pretty much need to know the syntax or whatever they're called to find it if it begins with the $letter, instead of if it's "LIKE" it. Link to comment https://forums.phpfreaks.com/topic/122007-solved-query-an-sql-table-in-php/ Share on other sites More sharing options...
toplay Posted August 30, 2008 Share Posted August 30, 2008 Changing this part: LIKE '%" . $letter . "%'" to this: LIKE $letter . "%'" will find everything starting with whatever $letter value is. Link to comment https://forums.phpfreaks.com/topic/122007-solved-query-an-sql-table-in-php/#findComment-629763 Share on other sites More sharing options...
jealy Posted August 30, 2008 Author Share Posted August 30, 2008 Thanks for your reply. I tried that and got an error. Parse error: syntax error, unexpected T_VARIABLE On this line; $sql="SELECT Name FROM films WHERE Name LIKE "$letter"%'"; >.< Link to comment https://forums.phpfreaks.com/topic/122007-solved-query-an-sql-table-in-php/#findComment-629771 Share on other sites More sharing options...
jealy Posted August 30, 2008 Author Share Posted August 30, 2008 Fiddling around with it a bit I got the errors to go away however the query isn't showing any results, nor mysql connection errors. $sql="SELECT Name FROM films WHERE Name LIKE " . $letter . "%'"; Link to comment https://forums.phpfreaks.com/topic/122007-solved-query-an-sql-table-in-php/#findComment-629779 Share on other sites More sharing options...
fenway Posted August 30, 2008 Share Posted August 30, 2008 Could you echo sql? Link to comment https://forums.phpfreaks.com/topic/122007-solved-query-an-sql-table-in-php/#findComment-629816 Share on other sites More sharing options...
jealy Posted August 30, 2008 Author Share Posted August 30, 2008 May sound noobish but how do I go about doing that? Link to comment https://forums.phpfreaks.com/topic/122007-solved-query-an-sql-table-in-php/#findComment-629824 Share on other sites More sharing options...
fenway Posted August 30, 2008 Share Posted August 30, 2008 Echo the $sql variable in PHP... and I assume you're catching errors in mysql_query() and checking mysql_error(). Link to comment https://forums.phpfreaks.com/topic/122007-solved-query-an-sql-table-in-php/#findComment-629828 Share on other sites More sharing options...
toplay Posted August 30, 2008 Share Posted August 30, 2008 $sql = "SELECT Name FROM films WHERE Name LIKE '$letter%' "; Link to comment https://forums.phpfreaks.com/topic/122007-solved-query-an-sql-table-in-php/#findComment-629849 Share on other sites More sharing options...
jealy Posted August 30, 2008 Author Share Posted August 30, 2008 $sql = "SELECT Name FROM films WHERE Name LIKE '$letter%' "; Perfect, thanks. Link to comment https://forums.phpfreaks.com/topic/122007-solved-query-an-sql-table-in-php/#findComment-629864 Share on other sites More sharing options...
jealy Posted August 30, 2008 Author Share Posted August 30, 2008 Okay, now i'm trying to add something into my database using PHP. I seem to be getting an error with this, anyone know what it is? $query="INSERT INTO films ("Name", "Genre", "Year") VALUES ('".$Name."', '".$Genre."', '".$Year."')"; Link to comment https://forums.phpfreaks.com/topic/122007-solved-query-an-sql-table-in-php/#findComment-629881 Share on other sites More sharing options...
toplay Posted August 30, 2008 Share Posted August 30, 2008 Read up about using quotes in PHP (and MySQL). $query="INSERT INTO films (Name, Genre, Year) VALUES ('$Name', '$Genre', '$Year')"; Link to comment https://forums.phpfreaks.com/topic/122007-solved-query-an-sql-table-in-php/#findComment-629900 Share on other sites More sharing options...
jealy Posted August 30, 2008 Author Share Posted August 30, 2008 I shall do, thanks again! Link to comment https://forums.phpfreaks.com/topic/122007-solved-query-an-sql-table-in-php/#findComment-629902 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.