Jump to content

mysql_num_rows()


Thadeus

Recommended Posts

Hello,

 

I am having a problem..

 

I get this message " Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in... " whenever i am trying to work my log in script.

 

The script is:

 

<?php

$host="localhost";

$username="root";

$password="pass";

$db_name="webDB";

$table_name="webmembers";

 

mysql_connect($host, $username, $password) or die ('Unable to connect');

mysql_select_db($db_name) or die ('Error');

 

$uname= $_POST['uname'];

$pword= $_POST['pword'];

 

$urname= stripslashes($uname);

$pword= stripslashes($pword);

$uname= mysql_real_escape_string($uname);

$pword= mysql_real_escape_string($pword);

 

$sql= "SELECT *

FROM '$table_name'

WHERE username: '$uname' and password: '$pword'";

 

$result=mysql_query($sql);

 

$count= mysql_num_rows($result); <----------- problem

 

if ($count == 1)

{

session_register($uname);

session_register($pword);

header("location: loginsuccess.php");

}

else

{

echo $result,$count;

echo "Wrong Username and Password";

}

mysql_close();

 

 

?>

 

and

 

"loginsuccess.php"

 

<?php

session_start();

if(!sesssion_is_registered($uname))

{

header("location: mainpage.php");

}

 

?>

<html>

<head>

</head>

<body>

Log in Successful

</body>

</html>

 

why do i get this warning? what am i doing wrong?

Is there any simpler way to create a log in page but being secure as well?

 

Thank in advance.

Link to comment
Share on other sites

  <?php
    $host="localhost";
    $username="root";
    $password="pass";
    $db_name="webDB";
    $table_name="webmembers";

    $link=mysql_connect($host, $username, $password) or die ('Unable to connect');
    mysql_select_db($db_name) or die ('Error');

    $uname= $_POST['uname'];
    $pword= $_POST['pword'];

    $urname= stripslashes($uname);
    $pword= stripslashes($pword);
    $uname= mysql_real_escape_string($uname);
    $pword= mysql_real_escape_string($pword);

    $sql= "SELECT *
    FROM '$table_name'
    WHERE username: '$uname' and password: '$pword'";

    $result=mysql_query($sql);

    $count= mysql_num_rows($result,$link); //fixed you need the connection or else it will fail.
    if ($count == 1)
    {
    session_register($uname);
    session_register($pword);
    header("location: loginsuccess.php");
    }
    else
    {
    echo $result,$count;
    echo "Wrong Username and Password";
    }
    mysql_close();


    ?>

 

Fixed you need the link  when you call mysql_num_rows or else it will fail.

Link to comment
Share on other sites

mysql_num_rows() does NOT accept more than one parameter.

 

The error means that your query failed due to an error. If you had searched for that error you would have learned this and that by using some error checking logic, such as echoing mysql_error(), would tell you why it failed.

 

Your query has a few problems in it. You have single-quotes around the table name, making it a string instead of a table name (remove the single-quotes from around the table name) and you have some semi-colons : where you should be using equal signs (change them to equal signs.)

 

 

Link to comment
Share on other sites

"  Unknown column 'username' in 'where clause'  "

 

That was the error. Thank you for the mysql_error() i had forgot it exists. :S

 

The mysql_num_rows() indeed does not take more than 1 parameter.

 

Although i have fixed the first problem another one came up.

 

Please help me in this one as well.

 

I got this " Fatal error: Call to undefined function sesssion_is_registered() in... " by using this code:

 

" <?php

session_start();

if(!sesssion_is_registered($uname))

    {

        header("location: mainpage.php");

    }

 

?>

<html>

    <head>

    </head>

    <body>

      Log in Successful

    </body>

</html>  "

 

Any suggestions please on how to fix it?

 

Thanks again for my previous problem. :)

 

 

 

 

Link to comment
Share on other sites

"  Unknown column 'username' in 'where clause'  "

 

That was the error. Thank you for the mysql_error() i had forgot it exists. :S

 

The mysql_num_rows() indeed does not take more than 1 parameter.

 

Although i have fixed the first problem another one came up.

 

Please help me in this one as well.

 

I got this " Fatal error: Call to undefined function sesssion_is_registered() in... " by using this code:

 

" <?php

session_start();

if(!sesssion_is_registered($uname))

    {

        header("location: mainpage.php");

    }

 

?>

   

   

   

      Log in Successful

   

  "

 

Any suggestions please on how to fix it?

 

Thanks again for my previous problem. :)

 

 

This issue has nothing to do with your initial question that was answered.  Please close this one and more it solved using the "Mark Solved" button at the bottom.  Then make a new thread for your new problem.  It helps others who search the forum for previous solutions.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.