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 6972611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:59:31+00:00 2026-05-27T16:59:31+00:00

Prepared Statements Okay, I’ve just started taking a look at MySQLi prepared statements. This

  • 0

Prepared Statements

Okay, I’ve just started taking a look at MySQLi prepared statements. This was a big step for me as I’m very new to MySQL and PHP anyway, so I have an extremely tenuous grasp on the concept (perhaps about an hours worth), so your answers will have to be phrased similarly, sorry about this.

What I am wanting to know is if I am correctly writing a prepared statement. There’s nothing worse than learning a method which is incorrect and getting used to it, therefore coding entire projects inefficiently.

To the point: I have a function which registers a user, and then returns the inserted id, which is therefore the referencing id of the user.

Previously, I was simply querying the database, which I was told had security risks despite the use of mysql_real_escape_string() and similar security measures.

Now, it looks something like this: (assume for the sake of this question that all referenced variables are defined, the bound parameters are strings, and all called functions exist and are working).

function registerUser($username, $fname, $email, $password, $region, $activation) {
    $uniqueSalt = uniqueSalt();
    $password = sha1($uniqueSalt . $password);

    $mysqli = mysqli_connect('localhost', 'root', '', 'database');

    if ($stmt = $mysqli->prepare("INSERT INTO `users` VALUES('', ?, ?, ?, ?, '$password', '$uniqueSalt', '$activation')") ) {
        $stmt->bind_param("ssss", $username, $fname, $email, $region);
        $stmt->execute();
        $stmt->close();
    } else {
        echo 'error preparing statement';
    }

    return mysqli_insert_id($mysqli);
}

Questions

It seems to work, but:

1) Is this correct syntax for executing a prepared statement?

2) I had included the file this function was in (call it function.php) with another file called init.php which previously defined the variable $mysqli. I found if I didn’t include

$mysqli = mysqli_connect('localhost', 'root', '', 'database');

I would receive an error. Why did I have to redefine it inside the function?

3) When I previously ended the function before I used prepped statements, with return mysql_insert_id() which worked fine, now I’ve found I have to use mysqli_insert_id($mysqli).

If I don’t include $mysqli inside the parentheses I get the error mysqli_insert_id() expects exactly 1 parameter, 0 given. Why is this and why does it differ from what I had before?

Cheers,
Luke.

  • 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-05-27T16:59:31+00:00Added an answer on May 27, 2026 at 4:59 pm
    1. Your usage doesn’t make a lot of sense. From the PHP Manual Example:

      $stmt = $mysqli->prepare("INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)");
      $stmt->bind_param('sssd', $code, $language, $official, $percent);
      

      Compare this to your usage:

      $stmt = $mysqli->prepare("INSERT INTO `users` VALUES('', ?, ?, ?, ?, '$password', '$uniqueSalt', '$activation')");
      $stmt->bind_param("ssss", $username, $fname, $email, $region);
      

      Notice anything strange? The Manual example is using ? as a placeholder, where you then use ->bind_param() to create the substitution set. So your example, I believe, should be:

      $stmt = $mysqli->prepare("INSERT INTO `users` VALUES('', ?, ?, ?, ?, ?, ?, ?)");
      $stmt->bind_param("sssssss", $username, $fname, $email, $region, $password, $uniqueSalt, $activation);
      

      Not knowing if $activation is a number or string value. (Also, I would suggest using the column names and not omitting them in the INSERT query.)

      Now, what you did should work in many cases simply because you’re creating a single use statement and using variable expansion to insert the $pass, $uniqueSalt, $activation into the query string that is being prepared. Cases where it won’t is if you accidentally have a ' in one or more variables you’re putting in the query, which should either be parametized (using ->bind_param()), or using mysqli_real_escape_string(). However, mixing these approaches is poor practice and defeats the purpose of using prepared statements. There is no reason to do the first few, but not the last few.

    2. Your $mysqli variable is “out of scope” when you call it in the function if it’s defined globally, hence when not available, you can’t use it unless you import it or create another one locally (in the function). You can import it using the global $mysqli; syntax within the function (as long as it’s created globally and not locally in another function).

    3. This is the same issue as #2. See: http://php.net/manual/en/mysqli.insert-id.php

    My recommendation is to use PDO instead of the mysql_/mysqli_ functions.

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

Sidebar

Related Questions

I'm relatively new to MySQLi prepared statements, and running into an error. Take this
I'm having trouble with mysqli and prepared statements. I've just started learning mysqli an
Are PHP/mysql prepared statements possible when mysqli and PDO are not available? Are there
Mysql supports prepared statements in this way: http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-prepared-statements.html Is there a support for it
I'm using prepared statements and MySQLi, and am currently getting this error. PHP Catchable
I'd just like to verify if using prepared statements in MySQL prevents SQL injection.
I am new to using prepared statements in mysql with php. I need some
I'm doing SQL queries in prepared statements(MySQLi) This is the query $register = $friend_zone->prepare(INSERT
Does MySQLdb support server-side prepared statements ? I can't figure this out from its
I am trying to do prepared statements as follows: $stmt = $mysqli->prepare(SELECT COUNT(*) FROM

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.