i keep getting this everytime i type a russian word in my translator Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in E:\webareas\cm541\translation.php on line 28 the code being <? $trans = $_POST["translate"]; include("connection.php"); $connection = @mysql_connect($db_host, $db_user, $db_password) or die("Error Connecting"); mysql_select_db($db_name, $connection); $query = "select* FROM Dictionary where russian ='$trans'"; $result= mysql_query($query, $connection); $num_rows = mysql_num_rows($result); for ($i =0; $i< mysql_num_rows($result); $i++) { $english = mysql_result($result, $i, "english"); $russian = mysql_result($result, $i, "russian"); echo '<tr> <td width="550" height="30"align="center" valign="middle"><a href="'.$english.'?page=">'.$english.'/'.$russian.'</a></td> </tr>'; } ?>
the problem is that your resultset returned from the query is invalid try changing: $result= mysql_query($query, $connection); $num_rows = mysql_num_rows($result); for ($i =0; $i< mysql_num_rows($result); $i++) Code (markup): to $result= mysql_query($query, $connection); if($result) { $num_rows = mysql_num_rows($result); for ($i =0; $i< mysql_num_rows($result); $i++) ... } else { // code for not valid resultset - e.g. use the msql_error() function here } Code (markup):
Actually, it works both ways, although this is technically the correct way to do it if not concatenating: $query = "select * FROM Dictionary where russian ='{$trans}'"; PHP: I see a couple of things wrong with your code. First, you don't sanitize your user input, leaving you open to SQL injection. You need to change this: $trans = $_POST["translate"]; PHP: to this: $trans = mysql_real_escape_string($_POST["translate"]); PHP: You should probably validate it too, but this should be enough. Also, using mysql_result is inefficient if you are retrieving data from more than one field. Change this: $num_rows = mysql_num_rows($result); for ($i =0; $i< mysql_num_rows($result); $i++) { $english = mysql_result($result, $i, "english"); $russian = mysql_result($result, $i, "russian"); echo '<tr> <td width="550" height="30"align="center" valign="middle"><a href="'.$english.'?page=">'.$english.'/'.$russian.'</a></td> </tr>'; } PHP: to this: while($row=@mysql_fetch_assoc($result)) { echo '<tr> <td width="550" height="30"align="center" valign="middle"><a href="'.$row['english'].'?page=">'.$row['english'].'/'.$row['russian'].'</a></td> </tr>'; } PHP: If you are only pulling two fields out of your table, you can optimize your query by changing it to this: $query = "select english,russian FROM Dictionary where russian ='{$trans}'"; PHP: Are the Russian characters in Cyrillic or Roman characters? Using the wrong character set may be causing the error you are seeing.