0
<?php
//$query = $_POST['query'];
$query = 'TI';

$conn = mysql_connect($mysql_host, $mysql_user, $mysql_password);
mysql_select_db($mysql_database, $conn);

$q=mysql_query("SELECT * FROM productTbl WHERE productName = '$query%'");
while($e=mysql_fetch_assoc($q))
    $output[]=$e;

print(json_encode($output));
mysql_close();
?>

My user would have to key in a certain string into a textbox from Android and POST to my PHP. On the PHP side, how do I retrieve the rows according to the POST-ed string, let say the string 'TI' ? I tried this, but it returns null.

DroidMatt
  • 183
  • 1
  • 4
  • 20

3 Answers3

2

Use LIKE:


$query = mysql_real_escape_string($query); //better to be safer
SELECT * FROM productTbl WHERE productName LIKE '$query%'

Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
1

Try to use this

$q=mysql_query("SELECT * FROM productTbl WHERE productName LIKE '" . $query . "%'");
botzko
  • 630
  • 3
  • 8
0

for wildcards you have to use the like key word, something like:

<?php
//$query = $_POST['query'];
$query = 'TI';

$conn = mysql_connect($mysql_host, $mysql_user, $mysql_password);
mysql_select_db($mysql_database, $conn);

$q=mysql_query("SELECT * FROM productTbl WHERE productName like '$query%'");
while($e=mysql_fetch_assoc($q))
    $output[]=$e;

print(json_encode($output));
mysql_close();
?>
Vikram
  • 8,235
  • 33
  • 47