Jump to content


Photo

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


  • Please log in to reply
2 replies to this topic

#1 codenoob123

codenoob123

    Newbie

  • New Members
  • Pip
  • 2 posts

Posted 08 January 2013 - 04:20 PM

<p>Hello,

I have been following this tutorial and I have an error. Can someone show me where the error stems from and how I can avoid it next time?


<?php

class DB_Functions {

private $db;

// put your code
// constructor
function __construct() {
require_once 'DB_Connect.php';
// connecting to database
$this->db = new DB_Connect();
$this->db->connect();
}

// destructor
function __destruct() {

}

/**
* Storing new user
* returns user details
*/
public function storeUser($name, $email, $password) {
$uuid = uniqid('', true);
$hash = $this->hashSShA($password);
$encrypted_password = $hash["encrypted"]; // encrypted password
$salt = $hash["salt"]; // salt
$result = mysql_query("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES('$uuid','$name','$email','$encrypted_password','$salt',NOW())");
// check for successful store
if ($result) {
// get user details
$uid = mysql_insert_id();
// last inserted id
$result = mysql_query("SELECT * FROM users WHERE uid = $uid");
// return user details
return mysql_fetch_array($result);
} else {
return false;
}
}

/**
* Get user by email and password
*/
public function getUserByEmailAndPassword($email, $password) {
$result = mysql_query("SELECT * FROM users WHERE email = '$email'") or die(mysql_error);
// check for result
$no_of_rows = mysql_num_rows($result);
if ($no_of_rows > 0) {
$result = mysql_fetch_array($result);
$sale = $result['salt'];
$encrypted_password = $result['encrypted_password'];
$hash = $this->checkhashSSHA($salt, $password);
// check for password equality
if ($encrypted_password == $hash) {
// user authentication details are correct
return $result;
}
} else {
// user not found
return false;
}
}

/**
* Check user is existed or not
*/
public function isUserExisted($email) {
$result = mysql_query("SELECT email FROM users WHERE email = '$email'");
$no_of_rows = mysql_num_rows($result);
if ($no_of_rows > 0)
{
// user existed
return true;
} else {
// user not existed
return false;
}
}

/**
* Encrypting password
* @param password
* returns salt and encrypted password
*/
public function hashSSHA($password) {

$salt = sha1(rand());
$salt = substr($sale, 0, 10);
$encrypted = base64_encode(sha1($password . $salt, true) . $salt);
$hash = array("salt" => $salt, "encrypted" => $encrypted);
return $hash;
}


/**
* Decrypting password
* @param salt, password
* returns hash string
*/
public function checkhashSSHA($salt, $password) {

$hash = base64_encode(sha1($password . $salt, true) . $salt);


return $hash;
}

}

?>

my error: mysql_num_rows(): supplied argument is not a valid MySQL result resource

Edited by codenoob123, 08 January 2013 - 04:23 PM.


#2 scootstah

scootstah

    Advanced Member

  • Gurus
  • 2,889 posts
  • LocationUSA

Posted 08 January 2013 - 04:39 PM

It's because the query failed. Use mysql_error to find out why.

Your class is a bit over zealous though. A class called "DB_Functions", to me, sounds like a class for utility database functions. But instead, you have a bunch of user related methods. Would this not be better suited in its own User class?

function __construct() {
require_once 'DB_Connect.php';
// connecting to database
$this->db = new DB_Connect();
$this->db->connect();
}

I have a few issues with this.

require_once 'DB_Connect.php'; This makes your class very inflexible, because now it relies on specific file structure and files being available. What would be better, is if you passed in your class to the constructer.

function __construct(DB_Connect $database) {
	$this->db = $database;
	$this->db->connect();
}

My other issue is that, it seems like you are trying to use the mysql_* library in a half-assed OOP fashion, no offense. It was never intended for that, and it doesn't support that very well. Why not instead use the mysqli_* library or PDO library, which support OOP fully?
while(!$succeed = try());

#3 codenoob123

codenoob123

    Newbie

  • New Members
  • Pip
  • 2 posts

Posted 08 January 2013 - 07:31 PM

It's because the query failed. Use mysql_error to find out why.

Your class is a bit over zealous though. A class called "DB_Functions", to me, sounds like a class for utility database functions. But instead, you have a bunch of user related methods. Would this not be better suited in its own User class?

function __construct() {
require_once 'DB_Connect.php';
// connecting to database
$this->db = new DB_Connect();
$this->db->connect();
}

I have a few issues with this.

require_once 'DB_Connect.php'; This makes your class very inflexible, because now it relies on specific file structure and files being available. What would be better, is if you passed in your class to the constructer.

function __construct(DB_Connect $database) {
	$this->db = $database;
	$this->db->connect();
}

My other issue is that, it seems like you are trying to use the mysql_* library in a half-assed OOP fashion, no offense. It was never intended for that, and it doesn't support that very well. Why not instead use the mysqli_* library or PDO library, which support OOP fully?


It is a little difficult to use mysql error since I am running the query via an Android application (android to sqlite to php to mysql) After reading your suggestions, I think I will investigate using mysqli library or PDO library and start all over rather than using a tutorial. Thanks.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com