例:表名test 字段名name
<?php
// 建立MySQLi连接
$servername = "localhost"; // 数据库服务器名称
$username = "username"; // 数据库用户名
$password = "password"; // 数据库密码
$dbname = "database"; // 数据库名称
$conn = new mysqli($servername, $username, $password, $dbname);
// 检查连接是否成功
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
// 构造SQL查询语句
$search_name = "John"; // 要查找的名字
$sql = "SELECT name FROM test WHERE name LIKE '%" . $search_name . "%'";
// 执行查询语句
$result = $conn->query($sql);
// 检查查询结果是否为空
if ($result->num_rows > 0) {
// 输出每行数据
while($row = $result->fetch_assoc()) {
echo "name: " . $row["name"] . "<br>";
}
} else {
echo "没有结果";
}
// 关闭MySQLi连接
$conn->close();
?>
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
请登录后发表评论
注册
社交帐号登录