Welcome to Dream.In.Code
Become a PHP Expert!

Join 149,999 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,313 people online right now. Registration is fast and FREE... Join Now!




Warning: mysql_num_row(): supplied argument is not a valid MySQL resul

 
Reply to this topicStart new topic

Warning: mysql_num_row(): supplied argument is not a valid MySQL resul

revense
8 Dec, 2006 - 08:34 PM
Post #1

New D.I.C Head
*

Joined: 8 Dec, 2006
Posts: 1


My Contributions
Maybe this is a stupid question but I just started to learn PHP so, please help me to fix what wrong with my code

CODE
<?
//membuat session
//session_destroy();

// variabel yang diperlukan untuk akses database
$user = "root";
$pass = "admin";
$db = "pengaduan";
$server = "localhost";
// membuat koneksi
$koneksi = mysql_connect($server, $user, $pass);
// memeriksa koneksi

// membuka database
mysql_select_db($db);
// membuat query
$query = "SELECT * FROM LOGIN WHERE (USERNAME ='".$_POST['username']."') and (PASSWORDLOGIN='". md5($_POST['password'])."')";
// mengeksekusi query

$hasil = mysql_query($query);
$jumlahHasil = mysql_num_rows($hasil);

if($jumlahHasil <> 1)
    {
?>
    <script language=javascript>
    alert('Password atau Username salah !!')
    location.replace('index.php');
    </script>
<?
    }

else
    {
    $data = mysql_fetch_array($hasil);
    extract ($data);
    $passwordAsli = $_POST['password'];
    if($_POST['password'] == $passwordAsli)
        {
        session_start();
        $_SESSION['auth']="yes";
        $_SESSION['user']=$_POST['username'];
        $_SESSION['password']=$_POST['password'];
        $_SESSION['role']=$data[4];
        //echo $data[4];
        //echo $_SESSION['role'];
        //echo "<head><meta http-equiv='refresh' content='0;url=\"menu.php\"'></head>";
        ?>
        <script language=javascript>
        alert('Login Berhasil !!')
        location.replace('menu.php');
        </script>
        <?
        }
    else
        {
        echo("Password untuk user $USERNAME salah<br>");
        }
    }
?>


When I try this script, I get this warning...

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

will you help me what wrong
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Warning: Mysql_num_row(): Supplied Argument Is Not A Valid MySQL Resul
9 Dec, 2006 - 05:30 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,355



Thanked: 51 times
Dream Kudos: 25
My Contributions
Have you checked to see if anything is being returned from your query? Or that it has executed properly? mysql_num_rows will only take a valid resource as a parameter, and if the query failed, then no valid resource will be returned.

Try this:
CODE

$hasil = mysql_query($query);
if (!$hasil) {
   echo "Query Error: " . mysql_error() . "\n";
}
else
{
   echo "Query is fine\n";
}

User is online!Profile CardPM
+Quote Post

manilodisan
RE: Warning: Mysql_num_row(): Supplied Argument Is Not A Valid MySQL Resul
10 Dec, 2006 - 01:10 PM
Post #3

New D.I.C Head
*

Joined: 10 Dec, 2006
Posts: 2


My Contributions
try replacing your query line with this ones:


CODE
$query = "SELECT * FROM LOGIN WHERE (USERNAME ='".$_POST['username']."') and (PASSWORDLOGIN='". md5($_POST['password'])."')";
$hasil = mysql_query($query) or die(mysql_error());
$row_hasil = mysql_fetch_assoc($hasil);
$jumlahHasil = mysql_num_rows($hasil);

User is offlineProfile CardPM
+Quote Post

tork
RE: Warning: Mysql_num_row(): Supplied Argument Is Not A Valid MySQL Resul
14 Dec, 2006 - 03:09 PM
Post #4

New D.I.C Head
*

Joined: 14 Dec, 2006
Posts: 20


My Contributions
the error that you stated: Warning:mysql_num_rows():supplied argument is not a valid Mysql result resource

In my time so far in PHP, this error appears when your query results in nothing. Make sure you know that there will be a result before you do mysql_num_rows or carry out:

CODE
if ($result){

$count = mysql_num_rows($result)

}


i do this as a safety precaution.

QUOTE(revense @ 8 Dec, 2006 - 09:34 PM) *

Maybe this is a stupid question but I just started to learn PHP so, please help me to fix what wrong with my code

code


<?
//membuat session
//session_destroy();

// variabel yang diperlukan untuk akses database
$user = "root";
$pass = "admin";
$db = "pengaduan";
$server = "localhost";
// membuat koneksi
$koneksi = mysql_connect($server, $user, $pass);
// memeriksa koneksi

// membuka database
mysql_select_db($db);
// membuat query
$query = "SELECT * FROM LOGIN WHERE (USERNAME ='".$_POST['username']."') and (PASSWORDLOGIN='". md5($_POST['password'])."')";
// mengeksekusi query

$hasil = mysql_query($query);
$jumlahHasil = mysql_num_rows($hasil);

if($jumlahHasil <> 1)
{
?>
<script language=javascript>
alert('Password atau Username salah !!')
location.replace('index.php');
</script>
<?
}

else
{
$data = mysql_fetch_array($hasil);
extract ($data);
$passwordAsli = $_POST['password'];
if($_POST['password'] == $passwordAsli)
{
session_start();
$_SESSION['auth']="yes";
$_SESSION['user']=$_POST['username'];
$_SESSION['password']=$_POST['password'];
$_SESSION['role']=$data[4];
//echo $data[4];
//echo $_SESSION['role'];
//echo "<head><meta http-equiv='refresh' content='0;url=\"menu.php\"'></head>";
?>
<script language=javascript>
alert('Login Berhasil !!')
location.replace('menu.php');
</script>
<?
}
else
{
echo("Password untuk user $USERNAME salah<br>");
}
}
?>
/code

When I try this script, I get this warning...

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

will you help me what wrong


User is offlineProfile CardPM
+Quote Post

Chemo
RE: Warning: Mysql_num_row(): Supplied Argument Is Not A Valid MySQL Resul
20 Dec, 2006 - 05:40 PM
Post #5

New D.I.C Head
*

Joined: 17 Dec, 2006
Posts: 2


My Contributions
Try this:
CODE

<?php
/**
* Simple refactor for "revense" Dreamincode.net forum member
*
* Login script
*
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @version 1.0
* @link http://www.oscommerce-freelancers.com/ osCommerce-Freelancers
* @copyright Copyright 2006, Bobby Easland
* @author Bobby Easland
* @filesource
*/

    /**
     * Define the DB connection params
     */
    define('DB_USER', 'root');
    define('DB_PASS', 'admin');
    define('DB_NAME', 'pengaduan');
    define('DB_HOST', 'localhost');
    
    /**
     * Connect to the DB
     */
    $dbLink = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
    
    /**
     * Basic connection sanity check
     */
    if (!$dbLink) {
      die( 'Could not connect to database -> ' . mysql_errno() . ': ' . mysql_error() );
    }

    /**
     * Select the DB
     */
    $dbSelect = mysql_select_db(DB_NAME, $dbLink);
    
    /**
     * Basic DB select sanity check
     */
    if (!$dbSelect) {
         die( 'Cannot select DB -> ' . mysql_errno() . ': ' . mysql_error() );
    }    
    
    /**
     * Initialize the SQL
     */
     $sql = "SELECT *
             FROM LOGIN
                     WHERE USERNAME = '".mysql_real_escape_string($_POST['username'])."'
                         AND PASSWORDLOGIN = '". md5(mysql_real_escape_string($_POST['password']))."'
                     LIMIT 1";
    
    /**
     * Execute query
     */
    $query = mysql_query($sql, $dbLink);
    
    /**
     * Basic sanity check
     */
    if (!$query){
        die( 'Could not execute query -> ' . mysql_errno() . ': ' . mysql_error()  );
    }
    
    /**
     * Get the number of rows...should be 1 per the SQL limit
     */
    $rowsReturned = mysql_num_rows($query);

    if($rowsReturned !== 1) {
    ?>
        <script language=javascript>
        alert('Password atau Username salah !!')
        location.replace('index.php');
        </script>
    <?
    } else {
        $data = mysql_fetch_assoc($query);
        extract($data);
        $passwordAsli = $_POST['password'];
        if( $_POST['password'] == $passwordAsli ){
            session_start();
            $_SESSION['auth'] = "yes";
            $_SESSION['user'] = $_POST['username'];
            $_SESSION['password'] = $_POST['password'];
            $_SESSION['role'] = $data[4];
        ?>
        <script language=javascript>
        alert('Login Berhasil !!')
        location.replace('menu.php');
        </script>
        <?
        } else {
            echo("Password untuk user $USERNAME salah<br>");
        }
    }
?>

User is offlineProfile CardPM
+Quote Post

AdamG
RE: Warning: Mysql_num_row(): Supplied Argument Is Not A Valid MySQL Resul
21 Dec, 2006 - 07:57 AM
Post #6

D.I.C Head
Group Icon

Joined: 12 Jan, 2005
Posts: 94



Thanked: 1 times
Dream Kudos: 25
My Contributions
As stated above, I can almost guarantee the problem is your query. Adding "or die(mysql_error())" will display the SQL error received and help troubleshoot your problem.

--Adam
User is offlineProfile CardPM
+Quote Post

maxbox
RE: Warning: Mysql_num_row(): Supplied Argument Is Not A Valid MySQL Resul
11 Dec, 2008 - 05:36 PM
Post #7

New D.I.C Head
*

Joined: 7 Nov, 2008
Posts: 3

You can try with something like this


CODE
if (mysql_num_rows($query) <> 1 )



{

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 08:27PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month