user1597438
user1597438

Reputation: 2221

MySql query: Unknown column in 'where clause'

I have a simple select statement with a where clause on my php file like so:

$con=mysqli_connect("hostname", "user", "pw", "db");    
if (mysqli_connect_errno($con))
{
    echo "Failed to connect to mySQL: " . mysqli_connect_error();
}
$getTitle = mysqli_query($con, "SELECT title FROM tblTitle WHERE category='" .$col."'") or die("ERROR: ". mysqli_error($con));

I've changed my code as suggested and added the quotation mark on my variable but now I'm getting an internal server error at the $getTitle line. Am I doing something wrong here? I can't print the error so I can't point out what's going on.

UPDATE: As suggested, I've used var dump on $getTitle and here's the message I got.

object(mysqli_result)#5 (5) { ["current_field"]=> int(0) ["field_count"]=> int(1) ["lengths"]=> NULL ["num_rows"]=> int(1) ["type"]=> int(0) }

Upvotes: 0

Views: 6092

Answers (3)

Atanu
Atanu

Reputation: 67

That means you are having problem with your incorrect permission settings. Please first you check your database as well as file permission.If you are in Linux please change file permission to 644.have a look at your .htaccess file.There must be some error while settings.I think this will resolve your issue.Give it a try. Happy coding :)

Upvotes: 1

Rahul11
Rahul11

Reputation: 264

use like this in php "Select title from tblTitle where category=".$col;

Upvotes: 1

Amir
Amir

Reputation: 4111

$col should be within quote, otherwise it seems like category=music and Mysql assumes that music is a column.

If you are in PHP, use

"Select title from tblTitle where category='".$col."'";

or

"Select title from tblTitle where category='$col'";

Upvotes: 3

Related Questions