mysql_num_rows error

Discussion in 'PHP' started by Waxxy, Mar 13, 2008.

  1. #1
    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>';

    }

    ?>
     
    Waxxy, Mar 13, 2008 IP
  2. daboss

    daboss Guest

    Messages:
    2,249
    Likes Received:
    151
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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):
     
    daboss, Mar 13, 2008 IP
  3. pondlife

    pondlife Peon

    Messages:
    898
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Shouldn't this:

    be like this:

    ?
     
    pondlife, Mar 13, 2008 IP
  4. The Critic

    The Critic Peon

    Messages:
    392
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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.
     
    The Critic, Mar 13, 2008 IP
    pondlife likes this.
  5. GreatMetro

    GreatMetro Peon

    Messages:
    117
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Try:


    $result = mysql_query($query, $connection) or die(mysql_error());
     
    GreatMetro, Mar 13, 2008 IP