Error in SQL like

0

I'm trying to do a select where I want all the data from a person who has the same ip and date equals today's date. The reason is that I need to restrict how many times a person sends contact to my system.

below my code.

        $ip = $_SERVER['REMOTE_ADDR'];
        $data = date('Y-m-d');
$row = mysql_query("SELECT * FROM tbl_CONTATOS where TXT_ENDIP_CONTT = '.$ip.'  and DAT_INCLU_CONTT like '%".$data."%' ");
        $count = mysql_num_rows($row);

         echo $count; die;
    
asked by anonymous 23.06.2015 / 19:55

4 answers

1

You do not need LIKE . You want something like

$db = new PDO('mysql:host=localhost dbname=teste', 'usuario', 'senha');
$query = "SELECT * FROM tbl_CONTATOS where TXT_ENDIP_CONTT = ? and DAT_INCLU_CONTT = ?";
$stmt = $db->prepare($query);
$stmt->bindValue(1, $_SERVER['REMOTE_ADDR']);
$stmt->bindValue(2, date('Y-m-d'));
$result = $stmt->execute();
$count = $result->rowCount();

echo $count; die;
    
23.06.2015 / 20:03
2

The error is not in the like and yes in the concatenation of the variable $ ip

  

As the variable is already inside a double quotationed string you were trying to concatenate with single quotation marks.

Here is an example of how to stay:

    $ip = $_SERVER['REMOTE_ADDR'];
    $data = date('Y-m-d');
    $row = mysql_query("SELECT * FROM tbl_CONTATOS where TXT_ENDIP_CONTT = '$ip'  and DAT_INCLU_CONTT like '%".$data."%' ");
    $count = mysql_num_rows($row);

     echo $count; die;
    
23.06.2015 / 20:01
2

The concatenation of your string is wrong. Maybe this is your mistake.

Replace:

$row = mysql_query("SELECT * FROM tbl_CONTATOS where TXT_ENDIP_CONTT = '.$ip.'  and DAT_INCLU_CONTT like '%".$data."%' ");

By:

$row = mysql_query("SELECT * FROM tbl_CONTATOS where TXT_ENDIP_CONTT = '{$ip}' and DAT_INCLU_CONTT like '%{$data}%'");

I take this opportunity and recommend reading and learning about PDO .

    
23.06.2015 / 20:11
-2

But your date field in the database is type STRING or DATE?

I've worked with banks that could not compare DATE using LIKE.

    
23.06.2015 / 20:01