Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fisherd
    New Member
    • Nov 2008
    • 3

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in

    When i run this code, i keep getting this message;

    Warning: mysql_num_rows( ): supplied argument is not a valid MySQL result resource in C:\wamp\www\che cklogin.php on line 26

    i use this code to check login details. Here is the code for the pages that i used.

    main_login.php

    [PHP]<html>
    <body>
    <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCC C">
    <tr>
    <form name="form1" method="post" action="checklo gin.php">
    <td>
    <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFF F">
    <tr>
    <td colspan="3"><st rong>Member Login </strong></td>
    </tr>
    <tr>
    <td width="78">User name</td>
    <td width="6">:</td>
    <td width="294"><in put name="myusernam e" type="text" id="myusername" ></td>
    </tr>
    <tr>
    <td>Password</td>
    <td>:</td>
    <td><input name="mypasswor d" type="text" id="mypassword" ></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td><input type="submit" name="Submit" value="Login"></td>
    </tr>
    </table>
    </td>
    </form>
    </tr>
    </table>
    </body>
    </html>[/PHP]

    checklogin.php

    [PHP]<?php
    $host=""; // Host name
    $username=""; // Mysql username
    $password=""; // Mysql password
    $db_name=""; // Database name
    $tbl_name=""; // Table name

    // Connect to server and select databse.
    mysql_connect(" $host", "$username" , "$password" )or die("cannot connect");
    mysql_select_db ("$db_name") or die("cannot select DB");

    // username and password sent from form
    $myusername=$_P OST['myusername'];
    $mypassword=$_P OST['mypassword'];

    // To protect MySQL injection (more detail about MySQL injection)
    $myusername = stripslashes($m yusername);
    $mypassword = stripslashes($m ypassword);
    $myusername = mysql_real_esca pe_string($myus ername);
    $mypassword = mysql_real_esca pe_string($mypa ssword);

    $sql="SELECT * FROM $tbl_name WHERE username='$myus ername' and password='$mypa ssword'";
    $result=mysql_q uery($sql);

    // Mysql_num_row is counting table row
    $count=mysql_nu m_rows($result) ;
    // If result matched $myusername and $mypassword, table row must be 1 row

    if($count==1){
    // Register $myusername, $mypassword and redirect to file "login_success. php"
    session_registe r("myusername") ;
    session_registe r("mypassword") ;
    header("locatio n:login_success .php");
    }
    else {
    echo "Wrong Username or Password";
    }
    ?>[/PHP]

    login_success.p hp

    [PHP]<?
    session_start() ;
    if(!session_is_ registered(myus ername)){
    header("locatio n:main_login.ph p");
    }
    ?>

    <html>
    <body>
    Login Successful
    </body>
    </html>[/PHP]

    could anyone see the error on the check login page.
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Your mysql_result() is probably generating an error or returning false (empty).

    Change line 23 (checklogin.php ) to
    Code:
    $result=mysql_query($sql);
    and see if you get an error.

    Comment

    • fisherd
      New Member
      • Nov 2008
      • 3

      #3
      Originally posted by Markus
      Your mysql_result() is probably generating an error or returning false (empty).

      Change line 23 (checklogin.php ) to
      Code:
      $result=mysql_query($sql);
      and see if you get an error.
      i replaced that line of code and i still get an error. is there any solution
      Sorry, my brain must've slipped.
      Code:
      $result=mysql_query($sql) or die(mysql_error());
      is correct

      Comment

      • fisherd
        New Member
        • Nov 2008
        • 3

        #4
        Originally posted by fisherd
        Sorry, my brain must've slipped.
        Code:
        $result=mysql_query($sql) or die(mysql_error());
        is correct
        thank for the help. i am really greatful

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Originally posted by fisherd
          thank for the help. i am really greatful
          Do you get an error when you add this?

          Comment

          Working...