Since I am new to Mysqli in general I am not sure what to look for so I am posting here.
TABLE = id | firstname | lastname | attribute1 | attribute 2
Example string to search for is "test" The script should search for any row where the firstname or lastname contain "test" or "tést" or "tèst" or a variation of this.
Then is should return all results in JSON format.
How would I go about doing this?
Well, MySQL to JSON is a big stretch.
Let's focus on the MySQL part here.
SELECT * FROM table WHERE firstname LIKE '%test%' OR lastname LIKE '%test%'
You have to do research on your own. Post specific questions or problems here. We can't just do it for you...
What you need to do to search for a string is this
SQL: SELECT * FROM TABLE WHERE firstname LIKE '%test%' OR lastname LIKE '%test%' OR attribute1 LIKE '%test%' OR attribute2 LIKE '%test%'
You can of course put a post variable in the like statement if you want so like '%".$_POST['value']."%'
To put it in json use the php function json_encode
.
SELECCT * FROM <TABLE NAME> WHERE firstname LIKE '%test%' OR lastname LIKE '%test%';
then use fetch array function and apply json_encode()
function on that array to get the json string.
Thanks for the help guys!
I was mainly looking for the SQL query
Using
$results = mysqli_query($mysqli, "SELECT * FROM players WHERE firstname LIKE '%test%' OR lastname LIKE '%test%'");
$myarray = array();
while ($record = mysqli_fetch_assoc($results))
{
//print_r($record);
$temparray = $record;
array_push($myarray, $temparray);
}
echo json_encode($myarray);
I found it by myself. I hope this can help you as alternate coding.
$temp_findcategory = $_POST['findcategory'];
$temp_findkeyword = "%".$_POST['findkeyword']."%";
$search = mysqli_query($conn,"SELECT * FROM table WHERE field1 like '$temp_findkeyword' order by '$temp_findcategory' asc");
$row_search = mysqli_fetch_assoc($search);