• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 228
  • Last Modified:

mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /home/salenbuy/public_html/manoj/login/authenticate.php on line

Dear sir
i am getting Error when i upload  my php code on live website,but in localhost in myssytem,it's working fine,but in live i am getting error::This is my code for login

my dbconfig.php
<?php
         $dbhost = 'localhost';
         $dbuser = '*****';
         $dbpass = '*****';
         $dbname = '*****';
         $conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);
   
         if(! $conn ) {
            die('Could not connect: ' . mysqli_error());
         }
       
        ?>

Open in new window

And  Authenticate.php
<?php ini_set('display_errors', '1'); ?>  
<?php
require_once("dbconfig.php");


 $username=$_POST['username'];
 $password=$_POST['password'];

 $username=stripcslashes($username);
 $username=mysqli_real_escape_string($conn,$username);
 $username=htmlspecialchars($username);
  
 $password=stripcslashes($password);
 $password=mysqli_real_escape_string($conn,$password);
 $password=htmlspecialchars($password);
$sql="SELECT * FROM userregisters  WHERE username='$username' and password='$password'";
$result=mysqli_query($sql);

$row=mysqli_num_rows($result);
$userinfo=mysqli_fetch_assoc($result);
 $role=$userinfo['role'];
 
 if($row==1){

           $_SESSION['login_user']=$username;
	       $id =$userinfo['id'];
		   $_SESSION["id"]=$id;
          $_SESSION["userid"] = $id;
		  
  $queryString = "INSERT INTO  admins (userid, intime) VALUES ('{$userinfo['id']}', NOW())";
  $result=mysqli_query($conn,$queryString);
 $row1=mysqli_num_rows($conn,$result1);
  if($role=='admin'){
    
    
  header('location:admin.php');
  }
  if($role=='user'){
    
  header('location:user.php');
  }   
   
 }else{
  echo "No User Found by Given Information";
 }
  
 
?>

Open in new window

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /home/salenbuy/public_html/manoj/login/authenticate.php on line 32

Warning: Cannot modify header information - headers already sent by (output started at /home/salenbuy/public_html/manoj/login/authenticate.php:2) in /home/salenbuy/public_html/manoj/login/authenticate.php on line 40
0
manoj kumar
Asked:
manoj kumar
1 Solution
 
Brian TaoSenior Business Solutions ConsultantCommented:
Change line#17 and #32:
// line#17
// from this
// $result=mysqli_query($sql);
// to this
$result=mysqli_query($conn, $sql);

// and line#32
// from this
// $row1=mysqli_num_rows($conn,$result1);
// to this
$row1=mysqli_num_rows($result1);

Open in new window


Also, a suggestion, do not include any DB credentials when asking a question, or at least replace the strings with something that's not your real username and password.
0
 
manoj kumarAuthor Commented:
sir again same issue is there Error:
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /home/salenbuy/public_html/manoj/login/authenticate.php on line 32
Warning: Cannot modify header information - headers already sent by (output started at /home/salenbuy/public_html/manoj/login/authenticate.php:2) in /home/salenbuy/public_html/manoj/login/authenticate.php on line 40

but in my localhost it's working fine,but when i upload in live website i am getting Error:what is issue
0
 
Chris StanyonCommented:
Look at these 2 lines:

$result=mysqli_query($conn,$queryString);
 $row1=mysqli_num_rows($conn,$result1);

In the first, your variable is called $result, in the second it's called $result1

Having said all that, I'm hoping this code is just for test purpose as there's a lot wrong with it from a security point of view. You should not be storing your passwords in plain text, and you should definitely be using a prepared query for this.
1
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
manoj kumarAuthor Commented:
Dear sir
why i am getting Error:again connected      
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /home/salenbuy/public_html/manoj/login/authenticate.php on line 32

Warning: Cannot modify header information - headers already sent by (output started at /home/salenbuy/public_html/manoj/login/authenticate.php:2) in /home/salenbuy/public_html/manoj/login/authenticate.php on line 40

IO modify all your suggestion but same issue is there :i cannot understand
Because in imy localhost server it's working fine:i know that password is protected i am storing password in plain text:but i will change:but this issue i cannot understand
0
 
manoj kumarAuthor Commented:
<?php ini_set('display_errors', '1'); ?>  
<?php
require_once("dbconfig.php");
 global $conn;

 $username=$_POST['username'];
 $password=$_POST['password'];

 $username=stripcslashes($username);
 $username=mysqli_real_escape_string($conn,$username);
 $username=htmlspecialchars($username);
  
 $password=stripcslashes($password);
 $password=mysqli_real_escape_string($conn,$password);
 $password=htmlspecialchars($password);
$sql="SELECT * FROM userregisters  WHERE username='$username' and password='$password'";
$result=mysqli_query($conn,$sql);

$row=mysqli_num_rows($result);
$userinfo=mysqli_fetch_assoc($result);
 $role=$userinfo['role'];
 
 if($row==1){

           $_SESSION['login_user']=$username;
	       $id =$userinfo['id'];
		   $_SESSION["id"]=$id;
          $_SESSION["userid"] = $id;
		  
  $queryString = "INSERT INTO  admins (userid, intime) VALUES ('{$userinfo['id']}', NOW())";
  $result=mysqli_query($conn,$queryString);
  $row1=mysqli_num_rows($result1);
  if($role=='admin'){
    
    
  header('location:admin.php');
  }
  if($role=='user'){
    
  header('location:user.php');
  }   
   
 }else{
  echo "No User Found by Given Information";
 }
  
 
?>

Open in new window

0
 
Chris StanyonCommented:
Look at your error:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result

Now look at your code:

$result=mysqli_query($conn,$queryString);
$row1=mysqli_num_rows($result1);

The first line the variable is called $result. The second line, it's called $result1

What you should have is this:

$result=mysqli_query($conn,$queryString);
$row1=mysqli_num_rows($result);

Open in new window

0
 
Chris StanyonCommented:
Actually, looking at your code, you don't need that at all. You're running an INSERT query, so trying to get the number of rows won't work. That only works for SELECT statements, so you should just remove line 32 altogther
0
 
manoj kumarAuthor Commented:
in line no. 40 i changes echo("<script>location.href = 'user.php?msg=$msg';</script>") and working fine but no output is there:when user login it show blank page:why it happens
0
 
manoj kumarAuthor Commented:
when i add ob_start() no error is there but it say "Failed To Connect";but when  run only dbconfig.php it's connected but when i login it say Failed To Connect
0
 
Chris StanyonCommented:
Nowhere in your code are you setting $msg so there's nothing to pass into the user.php file!
0
 
hieloCommented:
Try:
Authenticate.php
<?php
ini_set('display_errors', '1');

$username = array_key_exists('username',$_POST) ? stripcslashes(trim($_POST['username'])) : null;
$password = array_key_exists('password',$_POST) ? stripcslashes(trim($_POST['password'])) : null;

if( empty($username) || empty($password) )
{
	die( __LINE__ . ': Missing username and/or password.');
}

session_start();

require_once("dbconfig.php");

$sql = sprintf("SELECT `id`,`role` FROM `userregisters`  WHERE `username`='%s' AND `password`='%s'"
				,mysqli_real_escape_string($conn,$username)
				,mysqli_real_escape_string($conn,$password)
			);

$result = mysqli_query($conn,$sql);
$total = mysqli_num_rows($result);
if($total===0)
{
	echo __LINE__.": No User Found by Given Information";
}
elseif($total!==1)
{
	echo __LINE__.": Unexpected # of users found.";
}
else
{
	$userinfo = mysqli_fetch_assoc($result);
	$role=strtolower($userinfo['role']);

	$_SESSION['login_user'] = $username;
	$_SESSION["id"]=$userinfo['id'];
	$_SESSION["userid"] = $userinfo['id'];

	$queryString = sprintf("INSERT INTO `admins`(`userid`, `intime`) VALUES ('%s', NOW())"
							,mysqli_real_escape_string($conn,$userinfo['id'])
						);
	if( !mysqli_query($conn,$queryString) )
	{
		die(__LINE__ .': Unable to execute query.');
	}
	else
	{
		$rows_affected = mysqli_affected_rows($conn);
		if(1!==$rows_affected )
		{
			die(__LINE__ .': '.$rows_affected .' rows affected.');
		}
		else
		{
			if($role==='admin')
			{
				header('Location: admin.php');
				exit;
			}

			if($role==='user')
			{
				header('Location: user.php');
				exit;
			}
			
			header('Location: /?line='.__LINE__);
			exit;
		}
	}
}

Open in new window

0
 
manoj kumarAuthor Commented:
Can u Explain Me sir why it give waring That
Warning: Cannot modify header information - headers already sent by (output started at /home/salenbuy/public_html/manoj/login/dbconfig.php:13) in /home/salenbuy/public_html/manoj/login/authenticate.php on line 64
0
 
manoj kumarAuthor Commented:
Dear sir
i read php docomention and found that it can be issue of new line so i add ob_start(); and ob_end_flush(); at begning and Ending of file;the waring is not Display but say "failed to connect";but when i run dbconfig.php  and in else block i place echo "connected"; then it display connected :But When in Index.php i logged in then instead of going to user.php  it give  Failed to connct ;what is the cause
can it happens from cpanel:or my php code :can i made some seeting in cpanel

Thank you
singh.manoj@matebiz.com
0
 
Chris StanyonCommented:
If you're getting the 'Cannot modify header information' warning then it's becaue you've already output some information. Check through scripts very carefully to make sure you're not outputting anything before the header call., and that includes warnings and errors.
0
 
manoj kumarAuthor Commented:
Dear sir.
i have a requirment that i have a site and also have another site ,it is two diffrent site,but i want when googlebot see my both site,so i want in googlebot that he find only one site .so i serched in googlebot and find this code,can you Explain or can u give little knowledge how can i do that ;for this i placed my code<?php $ua = $_SERVER['HTTP_USER_AGENT']; $ip = $_SERVER['REMOTE_ADDR']; echo "hi"; if(preg_match('#googlebot#i',$ua)){ $dns = gethostbyaddr($ip); if (preg_match('#\.googlebot\.com$#i',$dns)) { $host = gethostbyname($dns); if ($host == $ip){ // This helps understand if googlebot here is googlebot for mobile, in some case you need to do other stuff if(preg_match('#AppleWebKit#i',$ua)) { include ('googlemobilebot.php'); exit; } else { include ('googlebot.php'); return $user; } } else { // someone is trying to make you think is Googlebot include ('fakegooglebot.php'); exit; } }
0
 
manoj kumarAuthor Commented:
<?php

       $ua = $_SERVER['HTTP_USER_AGENT'];
       $ip = $_SERVER['REMOTE_ADDR'];
echo "hi";

       if(preg_match('#googlebot#i',$ua)){
          
           $dns = gethostbyaddr($ip);
           if (preg_match('#\.googlebot\.com$#i',$dns)) {
               $host = gethostbyname($dns);
               if ($host == $ip){
                  
                   // This helps understand if googlebot here is googlebot for mobile, in some case you need to do other stuff
                   if(preg_match('#AppleWebKit#i',$ua)) {
                       include ('googlemobilebot.php');
                       exit;
                   }
                   else {
                       include ('googlebot.php');
                       return $user;
                   }
               }
               else {

                   // someone is trying to make you think is Googlebot
                   include ('fakegooglebot.php');
                   exit;
               }
           }

Open in new window

0
 
manoj kumarAuthor Commented:
can u explain this code what happens in this code
0
 
Chris StanyonCommented:
You need to ask another new question elsewhere in the forums as this has nothing to do with the original question. It's a completely separate issue. Close this question off and then open a new question.
0
 
manoj kumarAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now