0

I need to search through a MYSQL table for any string that may or not be in a row and print it out on screen. I could easily do this with python but I am still learning PHP and it's way different and am unfamiliar with how it's done here.

The row have multiple sets of words separated by commas so I need to use LIKE for any word that matches $string

$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!";
}
Shadow
  • 33,525
  • 10
  • 51
  • 64
  • What is the problem you're running into with this? Are you just trying to understand how to execute a query and work with the response in PHP? Your query looks fine (except for the SQL injection potential - look into parameterized queries with mysqli). The problem with your code is that `$result` will contain the result object, and not the actual result value. – RToyo Jul 20 '18 at 21:38
  • I'm going to guess that $result may be something like "some text" and that is not == $string which is "text". This is because database returns "like" results – Andreas Jul 20 '18 at 21:41
  • Why not just query for `SELECT row FROM table WHERE row = '$string'`? – Felippe Duarte Jul 20 '18 at 21:44
  • I didn't know what to put in the if statement but I wanted if $string is equal to any string in $result["row"] and returns either true or false –  Jul 20 '18 at 21:52
  • You have to pass the `$db` handle as the first argument to `mysqli_query()` (or use MySQLi in an OOP manner, look at the [documentation for MySQLi](https://secure.php.net/manual/en/book.mysqli.php)) – Decent Dabbler Jul 20 '18 at 21:54
  • There are multiple strings in the row separated by commas. If I could just search for any word in that row and return the value. For example if the table row has: ( cat, dog, bird ) I want lets say $string = 'dog' and for that string to search the row and find dog while ignoring the rest of cat, bird etc –  Jul 20 '18 at 21:55
  • 1
    @ScarletHarlot You should use `FIND_IN_SET()`. See https://stackoverflow.com/questions/28639692/query-with-multiple-values-in-a-column/28639762#28639762 – Barmar Jul 20 '18 at 21:56
  • 1
    You also need to read a tutorial on how to use `mysqli`. You're missing the connection argument, and you need to call `mysqli_fetch_assoc()` to get the rows of results. – Barmar Jul 20 '18 at 21:57
  • I have tried using mysqli_fetch_assoc( ) and I am still learning but I havn't really thought about the different uses for that and mysqli_fetch_array( ), mysqli_fetch_row( ) ... etc –  Jul 20 '18 at 22:00

1 Answers1

0

Found here.

You need to iterate over the result's cursor returned by query() like so:

<?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();
SteAp
  • 11,853
  • 10
  • 53
  • 88