Error in SQL like

Asked

Viewed 79 times

0

I’m trying to make a select where I want all the data of a person who has the same ip and date equal to today’s date. The reason is that I need to restrict as many times as 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;
  • Read that and that.

4 answers

2

The error is not in the like, but in the concatenation of the variable $ip

Since the variable is already inside a string with double quotes vc was trying to concatenate with single quotes.

Below is an example of how it should look:

    $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;
  • Vlw gave it right!

  • Please consider marking the answer as correct this encourages us to continue helping the community.

  • Okay, sure enough !

  • You don’t need the LIKE; it prevents Mysql from using indexes to speed up your query.

  • Renan @ctgPi is totally correct, consider his tips also on Sqlinjection and on Ikes. For though it works it has better and more correct ways to arrive at a result

2

Concatenating your string is wrong. Maybe this is your error.

Substitute:

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

For:

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

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

1


You don’t need the 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;

-2

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

I have worked with banks that did not accept to compare DATE using LIKE.

  • This is not an answer, please use the comments to ask questions. You should not answer a question with another question.

  • @Hiagosouza ok. But it wasn’t exactly a doubt what I posted. The question I posed was more like stress a point that I considered relevant. So much so that I explained immediately afterwards the reason for the question.

  • ok, yet you had a question. As you said yourself you explained the reason for the "question" so it was not an answer :) .. So use comments to ask questions. :)

Browser other questions tagged

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