Jump to content

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


codenoob123

Recommended Posts

<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
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

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.