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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:13:24+00:00 2026-06-18T06:13:24+00:00

I’m a newbie to programming and know only procedural PHP. I’ve been studying prepared

  • 0

I’m a newbie to programming and know only procedural PHP. I’ve been studying prepared statements and other safety issues. I have a few questions about my code below:

I have a table of Footwear sizes that have an identical size and sizeid (e.g. SIZE: 9.5 = sizeID: also 9.5).

My processing file checking then retrieving selected size from a previous html form page.

<?php
$link = mysqli_connect("servert", "user", "passW", "footwear");
        if(!$link){
            printf("Can't connect to the locatlhost. Errorcode: %d\n", mysqli_connect_errno());
        }
if (!mysqli_select_db($link, "footwear")) {
   printf("Can't connect to Database. Errorcode: %d\n", mysqli_connect_errno());
}      
$stmt = mysqli_stmt_init($link);
if($stmt=mysqli_prepare($link,"SELECT `sizeid`,`size` FROM `size` WHERE `sizeid`"))
{
mysqli_stmt_bind_param($stmt,"i", $sizeid);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt,$sizeid, $size);
mysqli_stmt_fetch($stmt);
        if(!floatval($size)) {
        echo "Invalid size. Pick another one.";
        }
        else {          
        $size = mysqli_real_escape_string($link, $size);
        $sizeid= mysqli_real_escape_string($link, $sizeid);

        $size= htmlspecialchars($size, ENT_QUOTES, 'UTF-8');
        $sizeid= htmlspecialchars($sizeid, ENT_QUOTES, 'UTF-8'); 

        $size = sprintf('%01.1f', $size);
        $sizeid = sprintf('%01.1f', $sizeid);

echo " Size: $size";
}
}
?>

The code works but I want to better understand how it does.

1. Is this line (I followed PHP.NET) necessary/critical?:

$stmt = mysqli_stmt_init($link);

Apparently the code works just fine when I omit it and go directly to the query:

$stmt=mysqli_prepare($link,"SELECT `sizeid`,`size` FROM `size` WHERE `sizeid`");

2. From what I’ve read, my understanding is that question marks (?) are used as place holders which are then bound in mysqli_stmt_bind_param ; but when I try

if($stmt=mysqli_prepare($link,"SELECT `sizeid`,`size` FROM `size` WHERE `?`"))…
mysqli_stmt_bind_param($stmt,"i", $sizeid);

it does not work.
Isn’t the idea to “hide” the variable?

3. Is it ok or does it hurt to have mysqli_real_escape_string and htmlspecialchars together. If not why and if it’s alright, should one be called out before the other, if that makes any difference?

4. I need the size to output as a float since real world shoe sizes are so( i.e. 7.5, 9.5, etc.)
Is there any danger in the method I’ve chosen to do so (floatval and sprint)?

  • 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-18T06:13:26+00:00Added an answer on June 18, 2026 at 6:13 am

    mysqli_stmt_init creates a prepared statement object. mysqli_prepare assigns a query to it. You’re supposed to pass $stmt to mysqli_prepare mysqli_stmt_bind_param is used to pass the values to replace the ? with

    $stmt = mysqli_stmt_init($link);
    // $prepared will be a boolean
    // the `?` are just for the values you want to pass
    $prepared = mysqli_prepare($stmt, "SELECT `sizeid`,`size` FROM `size` WHERE `sizeid` = ?");
    
    if($prepared){
        // Replace the `?` with a value in the WHERE clause
        mysqli_stmt_bind_param($stmt, "i", $sizeid);
    
        // run the query
        mysqli_stmt_execute($stmt);
    
        // assign result variables
        mysqli_stmt_bind_result($stmt, $result_sizeid, $result_size);
    
        // get results
        mysqli_stmt_fetch($stmt);
    
        echo htmlspecialchars("The sizeid is $result_sizeid and the size is $result_size");
    
        // close prepared statement
        mysqli_stmt_close($stmt);
    }
    else{
        die(mysqli_errno($link));
    }
    
    mysqli_close($link);
    

    PHP Docs: http://www.php.net/manual/en/mysqli-stmt.prepare.php

    mysqli_real_escape_string is only used to escape values before inserting them into the database. Your prepared statements do this for you, so mysqli_real_escape_string is not needed here at all.

    htmlspecialchars is used to display HTML characters on a webpage, it escapes them so they don’t get parsed by the browser.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I know there's a lot of other questions out there that deal with this
I have been unable to fix a problem with Java Unicode and encoding. The
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.