hello all.
Im really new to SQL but have some knowledge of PHP.
I was writing some very simple code to pick some records from a DB. I use phpMyAdmin.
here is my code:
CODE
<?php
$searchTerm=(int)trim($_POST['st2']); //searchTerm and searchType are coming from a
$searchType=trim($_POST['st']); // basic html form where for example
//searchType is id field (or name) and searchTerm
// is corresponding name or id
@$db=mysql_connect("localhost","root","");
if(!$db) {
echo "Error";
exit;
}
else {
mysql_select_db("adventuregame",$db);
$query="SELECT * FROM heroes WHERE ".$searchType." LIKE ".$searchTerm;
$result=mysql_query($query,$db);
echo "<table border=1>";
//get row data as an associative array
while($row=mysql_fetch_assoc($result))
{
echo "<tr>";
foreach($row as $col=>$value)
echo "<td>$value</td>";
echo "</tr>";
}
echo "</table>";
}
?>
it works OK for id field ( when i write id and 2 for example) but not for name. btw id is stored as integer in DB and name is as string. I guess the problem is in query.
I also tried to erase the cast in searchTerm,it still works for id..but still doesn't work for name. Gives a warning saying "Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in ...\search.php on line 18" and doesn't write anything on the browser. Any ideas and corrections will be appreciated. Thanks.