Click here to Skip to main content
13,588,544 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
i have write the php code for my collage project. i used mysqli_num_rows() function of php/mysql and i get this error:mysqli_num_rows() expects parameter 1 to be mysqli_result boolean given in

here is my whole code:
<pre><?php require_once('inc/top.php');?>
 </head>
 <body>
 <!--navbae-->
		<?php require_once('inc/nav.php');?>
<!--End navbar-->
<div class="container-fluid">
	<div class="row">
		<?php require_once('inc/sidebar.php');?><!--end sidebar-->
		<div class="col-md-9">
			<h1> Add Users <small>Add New Users Profile</small></h1><hr>
			<ol class="breadcrumb">
              <li><a href="index.php"> Dashbord</a></li>
			  <li class="active"> Add Users</li>
			</ol>
			<?php
                if(isset($_POST['submit'])){
                    $name = mysqli_real_escape_string($cn, $_POST['name']);
                    $username = mysqli_real_escape_string($cn, $_POST['user-name']);
                    $username_trim = preg_replace('/\s+/','',$username);
                    $email = mysqli_real_escape_string($cn,$_POST['email']);
                    $password = mysqli_real_escape_string($cn,$_POST['password']);
                    $role = $_POST['role'];
                    $image = $_FILES['image']['name'];
                    $image_tmp = $_FILES['image']['tmp_name'];
                  //  $check_query = "SELECT * FROM user WHERE user_name = '$username' or email = '$email'";
                    $check_run = mysqli_query($cn,"SELECT * FROM `user` WHERE `user_name` = '$username' or `email` = '$email'");
                    if(empty($name) or empty($username) or empty($email) or empty($password) or empty($image)){
                        $error = "ALL (*) FEILDS ARE REQUIRED";
                    }
                    else if($username != $username_trim)
                    {
                        $error = "Don't use spaces in Username";
                    }
                    else if(mysqli_num_rows($check_run)>0)
                    {
                        $error = "Username or email allready Exists";
                    }
                    else
                    {
                        $msg = "Success";
                    }
                }
            ?>
			<div class="row">
			    <div class="col-md-8">
                        <form action="" method="post" enctype="multipart/form-data">
                        <div class="form-group">
                            <label for="name">Name:*</label>
                            <?php
                                if(isset($error)){
                                    echo "<span class='pull-right' style='color:red;'>$error</span>";
                                }
                            ?>
                            <input type="text" name="name" id="name" class="form-control" placeholder="Name">
                        </div>
                        <div class="form-group">
                            <label for="user_name">User Name:*</label>
                            <input type="text" name="user-name" id="user-name" class="form-control" placeholder="User Name">
                        </div>
                        <div class="form-group">
                            <label for="email">E-mail:*</label>
                            <input type="text" name="email" id="email" class="form-control" placeholder="E-mail">
                        </div>
                        <div class="form-group">
                            <label for="password">Password:*</label>
                            <input type="password" name="password" id="password" class="form-control" placeholder="Password">
                        </div>
                        <div class="form-group">
                            <label for="role">Role:*</label>
                            <select name="role" id="role" class="form-control">
                                <option value="author">Author</option>
                                <option value="admin">Admin</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="profile">Profile Picture:*</label>
                            <input type="file" name="image" id="image">
                        </div>
                        <input type="submit" value="Add user" name="submit" class="btn btn-primary">
                        </form>
			    </div>
			    <div class="col-md-4"></div>
			</div>
		</div>
	</div>
</div>
<?php require_once('inc/footer.php');?>


i hope i'll get the best solution.
thanks in advance.

What I have tried:

i'd try some alternation but it dosen't work.
Posted 12-Aug-17 3:21am
Updated 12-Aug-17 15:26pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You need first to check the return value from the query: see PHP: mysqli::query - Manual[^] .
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

In here I used MySql. I just want to give you the idea. You should use MySqli with this answer and you can successfully run your code.

<?php

error_reporting(E_ALL ^ E_DEPRECATED);

include('DbConnection.php'); 
	if(isset($_POST['submit']))
	{

	    $query = mysql_query("SELECT * FROM user WHERE user_name='$username'");
		
		if(mysql_num_rows($query) > 0 ) //check if there is already an entry for that username
		{ 
		  $error = "Username is already taken , User another Username !!";
		}
			
		else
		{
		  echo " You Can Use This Username "; //you can echo or write a code as you want
		}
	}
	mysql_close();
?>
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04-2016 | 2.8.180615.1 | Last Updated 12 Aug 2017
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100