MySQL中的模糊查询

MySQL中的mysqli模糊查询

<?php
$content='3';
$mysqli = new MYSQLI( 'localhost', 'root', '12345678', 'ceshi', 3306 ); //实例化mysqli
$query="select * from dunling_chat where content like '%".$content."%' ";
$result=$mysqli->prepare($query);
@$result->bind_param("s",$content); //绑定参数

$result->execute();
$result->bind_result($id,$nicheng,$content,$time); //绑定结果
while ($result->fetch()) {
echo $id."<br />";
echo $nicheng."<br />";
echo $content."<br />";
echo $time."<br />";
}
$result->close();
$mysqli->close();
?>

 

 

 

MySQL中的pdo模糊查询


<?php

/////php操作pdo实现查询

header( "Content-type: text/html; charset=utf-8" );
$dbms = 'mysql';
$user = 'root';
$pwd = '12345678';
$dbName = 'ceshi';
$host = 'localhost';
$charset = 'utf8';
$dsn = "$dbms:host=$host;dbname=$dbName;charset=$charset";
try {
$pdo = new PDO( $dsn, $user, $pwd );
} catch ( Exception $e ) {
echo $e->getMessage();
}

$content = '23';
//查询
$sql = "select * from dunling_chat where content like '%".$content."%' ";
//准备sql模板
$stmt = $pdo->prepare( $sql );

//绑定参数
$stmt->bindValue( 1, $content );
//执行预处理语句
$stmt->execute();
//推荐这种方式来获取查询结果
while ( $row = $stmt->fetch() ) {
echo $row[ 'id' ] . "<br />";
echo $row[ 'nicheng' ] . "<br />";
echo $row[ 'content' ] . "<br />";
echo $row[ 'time' ] . "<br />";
}
//释放查询结果
$stmt = null;
//关闭连接
$pdo = null;

?>

posted @ 2019-09-18 10:34  2538  阅读(599)  评论(0编辑  收藏  举报