& Error inside mysql query

Asked

Viewed 45 times

1

Colleagues.

I have a table that inside the field Days tail: Monday & Tuesday

But when giving query inside PHP, does not return anything, even if the query is correct:

   $sql = mysqli_query($con,"SELECT * FROM semana WHERE Dias = 'Segundas & Terças'");

When we play directly within Mysql, it returns the result, but not within PHP. I believe the problem is in & Personally I’ve never been there. Someone would know how to fix?

  • $days = "Mondays & Tuesdays"; mysqli_query($con,"SELECT * FROM week WHERE Days = '".$days."'"); try this way

  • Which charset of DB?

3 answers

5


You can do it this way:

$Dias = "Segundas & Terças";
$sql = mysqli_query($con,"SELECT * FROM semana WHERE Dias = '".$Dias."'");

Passing the variable before.

  • thank you André.....

  • 1

    For nothing, just mark my answer as right, please.

4

Fox, to avoid many interventions in your code, I recommend using the like function:

$sql = mysqli_query($con,"SELECT * FROM semana WHERE Dias LIKE 'Segundas%Terças'");

2

There are two ways CORRECT to solve this.

1) Escaping (\) the special character.

$sql = mysqli_query($con,"SELECT * FROM semana WHERE Dias = 'Segundas \& Terças'");

2) Using the mysqli_real_escape_string: $dias = mysqli_real_escape_string("Segundas & Terças"); $sql = mysqli_query($con,"SELECT * FROM semana WHERE Dias = '{$dias}'");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.