Search code examples
phpmysqldatabasephpmyadminmysql-error-1064

Error in SQL Query regarding inverted commas


I am running following query in php :

$link = $_GET['link'];
$query = "SELECT DISTINCT sub_category FROM phonebook ORDER BY sub_category WHERE category = '$link' "; 
echo $query;
$sql = mysql_query($query) or die(mysql_error());

And it is giving following error:

SELECT DISTINCT sub_category FROM phonebook ORDER BY sub_category WHERE category = 'Administration' 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 'WHERE category = 'Administration'' at line 1

I am not able to understand what is the error in the inverted commas if any?


Solution

  • Try the following:

    $query = "SELECT DISTINCT sub_category FROM phonebook ORDER BY sub_category WHERE category = '$link' ";
    

    Into

    $query = "SELECT DISTINCT sub_category FROM phonebook WHERE category = '".$link."' ORDER BY sub_category ";
    

    You made the mistake "sub_caregory":

    $query = "SELECT DISTINCT sub_caregory FROM phonebook WHERE category = '".$link."' ORDER BY sub_caregory ";