query returning null
Posted by: rpjd (79.97.242.---)
Date: March 20, 2015 04:42PM

I have the following code for activating a user account:
include 'connect.php';
$Con = new mysqli($Host, $Username, $Password, $Db) or die(mysql_error()); // Connect to database server(localhost) with username and password.
if(isset($_GET['Email']) && !empty($_GET['Email']) AND isset($_GET['Hash']) && !empty($_GET['Hash']))
{
// Verify data
$Email = mysqli_real_escape_string($Con,$_GET['Email']); // Set email variable
$Hash = mysqli_real_escape_string($Con,$_GET['Hash']); // Set hash variable
$search = $Con->query("SELECT * FROM members_security WHERE Email='.$Email.' and Active='0'"winking smiley;
$result = mysqli_fetch_array($search);
$numRows = mysqli_num_rows($result);
echo $numRows;
}
I am getting this error message:
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given
$Email and $Hash are correct, I can't figure out why the query is not working.
Appreciate any guidance.

Options: ReplyQuote
Re: query returning null
Posted by: RiggsFolly (---.as43234.net)
Date: March 20, 2015 05:18PM

Well that error usually means that a previous command failed and you had not bothered to check before moving on.

In your case it could be the connection that failed somehow,
Or
That you are trying to mix and match Object Oriented calls and Proceedural call to the mysqli extension library.

So have a look at this and see if it helps :-

include 'connect.php';
// Connect to database server(localhost) with username and password.
$Con = new mysqli($Host, $Username, $Password, $Db) or die(mysql_error()); 

/* check connection */
if ($Con ->connect_errno) {
    printf("Connect failed: %s\n", $Con->connect_error);
    exit();
}

// see manual empty() does a isset 'http://php.net/manual/en/function.empty.php'
if( !empty($_GET['Email']) AND !empty($_GET['Hash']))
{
    // Verify data now using OO calls
    $Email = $Con->real_escape_string($Con,$_GET['Email']); // Set email variable
    $Hash = $Con->real_escape_string($Con,$_GET['Hash']); // Set hash variable
    
    $search = $Con->query("SELECT * 
                           FROM members_security 
                           WHERE Email='.$Email.' 
                            and Active='0' "winking smiley;

    // changed to OO call
    $result = $search->fetch_array(MYSQLI_ASSOC);

    // changed to OO call
    $numRows = $search->num_rows();
    echo $numRows;
}

---------------------------------------------------------------------------------------------
(Windows 10 Pro 64bit) (Wampserver 3.3.4 64bit) Aestan Tray Menu 3.2.5.4
<Apache versions MULTIPE> <PHP versions MULTIPLE> <MySQL Versions MULTIPLE>
<MariaDB versions MULTIPLE> <phpMyAdmin versions MULTIPLE> <MySQL Workbench 8.0.23>

Read The Manuals Apache -- MySQL -- PHP -- phpMyAdmin
Get your Apache/MySQL/mariaDB/PHP ADDONs here from the WAMPServer alternate Repo
-X-X-X- Backup your databases regularly Here is How dont regret it later! Yes even when developing -X-X-X-

Options: ReplyQuote
Re: query returning null
Posted by: rpjd (79.97.242.---)
Date: March 21, 2015 02:51AM

I tried your suggestion and it still gives me No records found. Puzzling.

Options: ReplyQuote
Re: query returning null
Posted by: Otomatic (Moderator)
Date: March 21, 2015 05:29PM

Hi,

In your first example, the result of the query is the resource $search, so you have to use this resource to find the num rows:
$numRows = mysqli_num_rows($search) ;
And I do not understand why you use a mysqli "class" only for the query and not for other codes, for example:
$numRows = $Con->num_rows($search);

---------------------------------------------------------------
Documentation Apache - Documentation PHP - Documentation MySQL - Wampserver install files & addons

Options: ReplyQuote
Re: query returning null
Posted by: rpjd (79.97.242.---)
Date: March 21, 2015 05:57PM

I tried that, it gave me:
Call to undefined method mysqli::num_rows()

Options: ReplyQuote
Re: query returning null
Posted by: Otomatic (Moderator)
Date: March 21, 2015 06:57PM

Hi,

The best thing to do is to always read the documentation

---------------------------------------------------------------
Documentation Apache - Documentation PHP - Documentation MySQL - Wampserver install files & addons

Options: ReplyQuote


Sorry, only registered users may post in this forum.