开发者

MYSQL: Limit Word Length for MySql Insert

开发者 https://www.devze.com 2022-12-25 09:09 出处:网络
every search query is saved in my database, but I want to Limit the Chracterlength for one single word: odisafuoiwerjsdkle --> length too much --> dont write in the database

every search query is saved in my database, but I want to Limit the Chracterlength for one single word: odisafuoiwerjsdkle --> length too much --> dont write in the database

my actually code is:

$search = $_GET['q']; 


    if (!($sql = mysql_query ('' . 'SELECT * 开发者_如何学PythonFROM `history` WHERE `Query`=\'' . $search . '\'')))    {
      exit ('<b>SQL ERROR:</b> 102, Cannot write history.');
      ;
    }

    while ($row = mysql_fetch_array ($sql))    {
      $ID = '' . $row['ID'];
    }

    if ($ID == '')
    {
      mysql_query ('' . 'INSERT INTO history (Query) values (\'' . $search . '\')');
    }

    if (!($sql = mysql_query ('SELECT * FROM `history` ORDER BY `ID` ASC LIMIT 1')))
    {
      exit ('<b>SQL ERROR:</b> 102, Cannot write history.');
      ;
    }

    while ($row = mysql_fetch_array ($sql)) {
      $first_id = '' . $row['ID'];
    }

    if (!($sql = mysql_query ('SELECT * FROM `history`')))
    {
      exit ('<b>SQL ERROR:</b> 102, Cannot write history.');
      ;
    }


One option would be using a trigger in the table. But, if you are expecting a lot of traffic on your search engine it might not scale very well. So, using client side (PHP in your case) constraints might be a better choice.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号