Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7776035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:46:30+00:00 2026-06-01T17:46:30+00:00

The below query is not telling me the username already exists in the database

  • 0

The below query is not telling me the username already exists in the database even though it does.

I am trying to learn how to bind parameters etc. but have confused myself somewhere I think.

<?php
    // Include config.php
    require_once("".$_SERVER['DOCUMENT_ROOT']."/admin/config.php");

    // top.inc.php
    require_once($top_inc);
?>

<!-- Meta start -->
<title></title>
<meta name="description" content="" />
<meta name="keywords" content="" />
<!-- Meta end -->

<!-- CONTENT START -->

<?php
    // sidebar.inc.php
    require_once($sidebar_inc);

    // main.inc.php
    require_once($main_inc);

    // check if form has been submitted
    if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['submit'])){

        // initialize form errors array
        $error    = array();

        // fetch form data
        $username = $_POST['username'];
        $email    = $_POST['email'];
        $password = $_POST['password'];

        // validate form data
        if(!preg_match(constant("USERNAME_REGEX"), $username)){
            $error[] = "Please enter a username. Use 3 to 15 digits and letters";
        }
        if(!preg_match(constant('PASSWORD_REGEX'), $password)){
            $error[] = "Please enter a password. Minimum of 6 characters required";
        }
        if(!empty($password) && $password == $username){
            $error[] = "Your pasword cannot be you username for security reasons";
        }
        if(empty($email)){
            $error[] = "Please enter your email address";
        }
        if(!empty($email) && !filter_var($email, FILTER_VALIDATE_EMAIL)){
            $error[] = "Your email address is not valid";
        }

        // connect to database
        sql_con();

        // Get instance of statement
        $stmt = mysqli_stmt_init($connect);

        // sql statement
        $UserExists = "
                    SELECT
                        `user_login`
                    FROM
                        `users`
                    WHERE
                        `user_login` = ? ";

        // prepare sql statement for execution
        if (mysqli_stmt_prepare($stmt, $UserExists)) {

            // bind parameters [s for string]
            mysqli_stmt_bind_param($stmt, "s", $username) or die(mysqli_stmt_error());
            // execute statement
            mysqli_stmt_execute($stmt) or die(mysqli_stmt_error());
            // check if username is found
            if(mysqli_stmt_num_rows($stmt) > 0 ){
                $error[] = 'The username you have choose has already been taken';
            }
        }

        // If errors found display errors
        if(!empty($error)){
            foreach($error as $msg){
                echo "$msg <br />";
            }
    } else {
            echo 'My Query Worked!';
        }
    }
    // signup.tpl template location
    $tpl = 'inc/tpl/signup.tpl';
    // load signup form template
    PageContentTemplate($tpl);
?>

<!-- CONTENT FINISH -->

<?php
    // footer.inc.php
    require_once($footer_inc);
?>

Basically it just echos out ‘My Query Worked’ even though it should say the username is already taken which it is, I enter details on the form and a username I know that has been taken and submit the form, I know I am doing something probably really silly but being new to mysqli and binding parameters etc. I don’t know where I am going wrong even though I have looked at some examples.

To be honest, I am not sure if this is the best way to do it in procedural style, I don’t know PDO/OOP, my main reason to change is to avoid SQL injection by using placeholders etc. in my queries.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:46:31+00:00Added an answer on June 1, 2026 at 5:46 pm

    I see it now. You have not called mysqli_stmt_store_result() before which mysqli_stmt_num_rows() won’t report the correct value.

        // prepare sql statement for execution
        if (mysqli_stmt_prepare($stmt, $UserExists)) {
    
            // bind parameters [s for string]
            mysqli_stmt_bind_param($stmt, "s", $username) or die(mysqli_stmt_error());
            // execute statement
            mysqli_stmt_execute($stmt) or die(mysqli_stmt_error());
    
            // check if username is found
            // FIRST : store the store the result set so num_rows gets the right value
            mysqli_stmt_store_result($stmt);
    
            // Now this should be 1 instead of 0
            if(mysqli_stmt_num_rows($stmt) > 0 ){
                $error[] = 'The username you have choose has already been taken';
            }
        }
    

    From the docs:

    Returns the number of rows in the result set. The use of mysqli_stmt_num_rows() depends on whether or not you used mysqli_stmt_store_result() to buffer the entire result set in the statement handle.

    If you use mysqli_stmt_store_result(), mysqli_stmt_num_rows() may be called immediately.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why does the MySQL query below give error 1066 (Not unique table/alias: 'customer') ?
I have a result from a query like the below, which does not have
I had the below query working in mysql 4.1, but does not in 5.0:
I have the below query which does not work. What am I doing wrong?
The query below does not work missing right parenthesis but you know what I
My LINQ query is not producing the expected output below. Basically, it's the sum
Why the two query below return duplicate member_id and not the third? I need
My query is like below (not working correctly) SELECT RentACar.dbo.Car.car_id as clm_CAR_ID, RentACar.dbo.Car_Image.car_image_url as
I have below query I am trying to show message 'No SubSource for this
What does the below query explain? SELECT * FROM `jos_menu` WHERE (id = 69

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.