我需要在MYSQL表中搜索可能不在行中的任何字符串,然后将其打印在屏幕上。我可以使用python轻松地做到这一点,但我仍在学习PHP,它与众不同,并且不熟悉它的完成方式。
该行有多组用逗号分隔的单词,因此我需要对与$ string匹配的任何单词使用LIKE
$string = 'text';
$db = mysqli_connect("localhost", "root", "", "data");
$result = mysqli_query("SELECT row FROM table WHERE row LIKE '%$string%'");
if($string == $result){
echo "String found";
}else{
echo "No string found!";
}
答案 0 :(得分:1)
找到了here。
您需要像这样遍历query()返回的结果游标:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();