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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:26:16+00:00 2026-05-20T10:26:16+00:00

I want to use prepared statement to insert and update data in mysqli, Below

  • 0

I want to use prepared statement to insert and update data in mysqli,

Below is my database class,

class database extends mysqli
{
    ...

    # insert and update data
    public function query_stmt($sql,$types = null,$params = null)
    {
        # create a prepared statement
        $stmt = parent::prepare($sql);

        if($stmt)
        {
            # bind parameters dynamically for markers
            if($types&&$params)
            {
                $bind_names[] = $types;
                for ($i=0; $i<count($params);$i++) 
                {
                    $bind_name = 'bind' . $i;
                    $$bind_name = $params[$i];
                    $bind_names[] = &$$bind_name;
                }
                $return = call_user_func_array(array($stmt,'bind_param'),$bind_names);
            }

            # execute query 
            $result = $stmt->execute();

            if($result) return 'success!';
                else return 'failed!';

            # close statement
            $stmt->close();

        }

        else
        {
            return self::get_error();
        }
    }

    # display error
    public function get_error() 
    {
        if($this->errno || $this->error)
        {
            return sprintf("Error (%d): %s",$this->errno,$this->error);
        }
    }

    public function __destruct()
    {
        parent::close();
        //echo "Destructor Called";
    }
}

It works fine when I have a correct query like this,

$sql = "
    INSERT root_countries_cities_towns (
        tcc_names,
        cny_numberic,
        tcc_created
    )VALUES(
        ?,
        ?,
        NOW()
    )";

$mysqli->query_stmt($sql, 'ss',array('UK','004'));

But I want it to return something when I have an incorrect query, such as,

$sql = "
        INSERT root_countries_cities_townx (
            tcc_names,
            cny_numberic,
            tcc_created
        )VALUES(
            ?,
            ?,
            NOW()
        )";

the table of root_countries_cities_townx does not exist. So I want the the method of query_stmt to return this type of error message to my browser. But my method doesn’t! Why?

I have tried to test it to return some default words whether it is ok or not, but nothing at all comes out on my browser,

# execute query 
$result = $stmt->execute();

if($result) return 'success!';
else return 'failed!';

What should I add or change in the code so that it can return a positive message when it is ok and returns negative message when it is not ok?

Thanks.

  • 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-20T10:26:17+00:00Added an answer on May 20, 2026 at 10:26 am

    You’re returning the error messages, not echoing them. You’d either have to modify the DB class to use echo instead of return, or do

    $ret_text = $mysqli->query_stmt(...);
    echo $ret_text;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below is my stored procedure. I want use stored procedure select all row of
I want to use the Publish.GacRemove function to remove an assembly from GAC. However,
I want to use SQL Profiler to trace the queries executed agains my database,
I want to use the mouse scrollwheel in my OpenGL GLUT program to zoom
I want to use Powershell to write some utilities, leveraging our own .NET components
I want to use the functions exposed under the OpenGL extensions. I'm on Windows,
I want to use the MultipleLookupField control in a web page that will run
I want to use the Web Browser control within an mono application, but when
I want to use CodeDOM to both declare and initialize my static field in
I want to use CSS sprites on a web site instead of separate image

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.