Reputation: 43
In mysql I want to display matched information with values... But the code I use select information separately and I get a bad result. What I want is to wrap the "select" functions together so they both look for specific information in mysql database.
Here's the code I use:
$name = explode(',',$name); //splits search
$query = "SELECT * FROM lucky WHERE name LIKE '%" . implode("%' AND name LIKE '%", $name) . "%'";
$sname = explode(',',$sname); //splits search
$query = "SELECT * FROM lucky WHERE sname LIKE '%" . implode("%' AND sname LIKE '%", $sname) . "%'";
$result = $mysqli->query($query);
while($row = $result->fetch_assoc()) {
echo "<table id='box'>";
echo "<tr;>";
echo "<td id='text''>Name:</td><td id='haha'>" . $row['name']. "</td>";
echo "<td id='text'>Second Name:</td><td id='haha'>" . $row['sname'] . "</td>";
echo "</tr;>";
echo "</table>";
} else { echo "No mathed information" ; }
Thanks in advance :)
UPADATE
Thanks to Spencer for his help!!!
Here's the code that I use now. Please use this to select matched information from the database!!!
$query = SELECT *
FROM lucky
WHERE name LIKE '$name'
AND sname LIKE '$sname'
$result = $mysqli->query($query);
while($row = $result->fetch_assoc()) {
echo "<table id='box'>";
echo "<tr;>";
echo "<td id='text''>Name:</td><td id='haha'>" . $row['name']. "</td>";
echo "<td id='text'>Second Name:</td><td id='haha'>" . $row['sname'] . "</td>";
echo "</tr;>";
echo "</table>";
} else { echo "No mathed information" ; }
Upvotes: 0
Views: 48
Reputation: 108370
If your $sname
string contains the value "jack,jill
", then the query would only return rows that have a name
column value that contains both of those strings, for example: 'jack and jill'
and 'jillojellojacko'
would match. But the query will not return rows where the name
column contains 'jack'
but doesn't contain 'jill
'.
If your intent is to search for rows that have either of the values matching, for example
$name = 'fee,fi,fo'
$sname = 'fum'
That is, any rows where name
column contains either 'fee'
, or 'fi'
, or 'fo'
, or sname
column contains 'fum'
, you could use a query of the form:
SELECT t.*
FROM lucky t
WHERE t.name LIKE '%fee%'
OR t.name LIKE '%fi%'
OR t.name LIKE '%fo%'
OR t.sname LIKE '%fum%'
If you replace all those OR
s with AND
s, then a row will need to satisfy all of those predicates to be returned. If you want a combination of AND
and OR
, then use parens to specify the order of precedence...
Upvotes: 1