Error with & within query mysql

1

Colleagues.

I have a table that within the Days field brings: Second & Tuesday

But when querying within PHP, it 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 inside Mysql, it returns the result, but within PHP it does not. I believe the problem is in & I've never been particularly troubled. Would anyone know how to solve it?

    
asked by anonymous 09.08.2016 / 18:14

3 answers

5

You can do this as follows:

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

Passing the variable before.

    
09.08.2016 / 18:16
4

Fox, to avoid many interventions in your code, I recommend that you use the like function:

$sql = mysqli_query($con,"SELECT * FROM semana WHERE Dias LIKE 'Segundas%Terças'");
    
09.08.2016 / 18:39
2

There are two ROUTE ways to resolve this.

1) Escaping (\) or 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}'");

    
09.08.2016 / 20:33