您好,欢迎访问一九零五行业门户网

如何编写PHP脚本,其中使用LIKE子句来匹配MySQL表中的数据?

我们可以在 php 函数中使用 where...like 子句的类似语法 - mysql_query()。 该函数用于执行 sql 命令以及稍后的另一个 php 函数– 如果 where...like 子句与 select 命令一起使用,mysql_fetch_array() 可用于获取所有选定的数据。
但是如果 where... like 子句与 delete 或 update 命令一起使用,则不需要进一步调用 php 函数。
为了说明这一点,我们有以下示例 -
示例 在此示例中,我们正在编写一个 php 脚本,它将返回名为 'tutorial_tbl' 表中作者姓名包含 'jay' 的所有记录 -
<?php $dbhost = 'localhost:3036'; $dbuser = 'root'; $dbpass = 'rootpassword'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('could not connect: ' . mysql_error()); } $sql = 'select tutorial_id, tutorial_title, tutorial_author, submission_date from tutorials_tbl where tutorial_author like "%jay%"'; mysql_select_db('tutorials'); $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('could not get data: ' . mysql_error()); } while($row = mysql_fetch_array($retval, mysql_assoc)) { echo "tutorial id :{$row['tutorial_id']} <br> ". "title: {$row['tutorial_title']} <br> ". "author: {$row['tutorial_author']} <br> ". "submission date : {$row['submission_date']} <br> ". "--------------------------------<br>"; } echo "fetched data successfully
"; mysql_close($conn);?>
以上就是如何编写php脚本,其中使用like子句来匹配mysql表中的数据?的详细内容。
其它类似信息

推荐信息