Like 條件

  1. LIKE 配合 WHERE 使用,只是將等號改成 LIKE 即可,LIKE 後接的字串配合百分比 % 符號,可進行字串搜尋。例如:LIKE dyw% 表示搜尋 dyw 開頭的字串。
    [root@kvm8 html]# mysql -uroot -p123qwe \
    -e "use dyw; select * from dyw_tbl where dyw_author like 'dyw%';"
    +--------+-----------+------------+-----------------+
    | dyw_id | dyw_title | dyw_author | submission_date |
    +--------+-----------+------------+-----------------+
    |      3 | MySQL     | dywang     | 2014-12-12      |
    +--------+-----------+------------+-----------------+
    
  2. 修改原 selectrecord.php 程式的查詢變數 $sql,WHERE 後的條件改為 LIKE 命令即可。
    [root@kvm8 html]# vim selectlikerecord.php
    [root@kvm8 html]# cat selectlikerecord.php
    <?php
    $dbhost = 'localhost:3306';
    $dbuser = 'root';
    $dbpass = '123qwe';
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    if(! $conn )
    {
      die('Could not connect: ' . mysql_error());
    }
    $sql = 'SELECT dywphp_id, dywphp_title, 
                   dywphp_author, submission_date
            FROM dywphp_tbl
            WHERE dywphp_author LIKE "dyw%"';
    
    mysql_select_db('dywphp');
    $retval = mysql_query( $sql, $conn );
    if(! $retval )
    {
      die('Could not get data: ' . mysql_error());
    }
    while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
    {
        echo "Dywphp ID :{$row['dywphp_id']}  <br> ".
             "Title: {$row['dywphp_title']} <br> ".
             "Author: {$row['dywphp_author']} <br> ".
             "Submission Date : {$row['submission_date']} <br> ".
             "--------------------------------<br>";
    } 
    mysql_free_result($retval);
    echo "Fetched data successfully\n";
    mysql_close($conn);
    ?>
    
  3. 開啟瀏覽器,連上網址 http://kvm8.deyu.wang/selectlikerecord.php,因為剛剛已刪除紀錄,目前沒有任何資料,可以使用 php 程式新增紀錄,再查詢。
    Image selectlikerecord