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

  • Home
  • SEARCH
  • 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 7458475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:27:08+00:00 2026-05-29T13:27:08+00:00

I was wondering if someone could help me. Im trying to integrate some code

  • 0

I was wondering if someone could help me.

Im trying to integrate some code into my application, the code that i need to integrate is written with PDO statements and i have no idea how it goes.

I was wondering if someone could help me convert it.

The code is as follows

$sql = "insert into message2 (mid, seq, created_on_ip, created_by, body) values (?, ?, ?, ?, ?)";
$args = array($mid, $seq, '1.2.2.1', $currentUser, $body);
$stmt = $PDO->prepare($sql);
$stmt->execute($args);
if (empty($mid)) {
    $mid = $PDO->lastInsertId();
}
$insertSql = "insert into message2_recips values ";
$holders = array();
$params = array();
foreach ($rows as $row) {
    $holders[] = "(?, ?, ?, ?)";
    $params[] = $mid;
    $params[] = $seq;
    $params[] = $row['uid'];
    $params[] = $row['uid'] == $currentUser ? 'A' : 'N';
}
$insertSql .= implode(',', $holders);
$stmt = $PDO->prepare($insertSql);
$stmt->execute($params);
  • 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-29T13:27:09+00:00Added an answer on May 29, 2026 at 1:27 pm

    You shoudl use PDO unles for some technical reason you cant. If you dont know it, learn it. Maybe this will get you started:

    /* 
    This the actual SQL query the "?" will be replaced with the values, and escaped accordingly 
    - ie. you dont need to use the equiv of mysql_real_escape_string - its going to do it 
    autmatically 
    */
    $sql = "insert into message2 (mid, seq, created_on_ip, created_by, body) values (?, ?, ?, ?, ?)";
    
        // these are the values that will replace the ?
        $args = array($mid, $seq, '1.2.2.1', $currentUser, $body);
    
        // create a prepared statement object
        $stmt = $PDO->prepare($sql);
    
        // execute the statement with $args passed in to be used in place of the ?
        // so the final query looks something like:
        // insert into message2 (mid, seq, created_on_ip, created_by, body) values ($mid, $seq, 1.2.2.1, $currentUser, $body)
        $stmt->execute($args);
    
    
        if (empty($mid)) {
            // $mid id is the value of the primary key for the last insert
            $mid = $PDO->lastInsertId();
        }
    
        // create the first part of another query
        $insertSql = "insert into message2_recips values ";
    
        // an array for placeholders - ie. ? in the unprepared sql string
        $holders = array();
    
        // array for the params we will pass in as values to be substituted for the ?
        $params = array();
    
        // im not sure what the $rows are, but it looks like what we will do is loop
        // over a recordset of related rows and do additional inserts based upon them
        foreach ($rows as $row) {
            // add a place holder string for this row
            $holders[] = "(?, ?, ?, ?)";
    
            // assign params
            $params[] = $mid;
            $params[] = $seq;
            $params[] = $row['uid'];
            $params[] = $row['uid'] == $currentUser ? 'A' : 'N';
        }
        // modify the query string to have additional place holders
        // so if we have 3 rows the query will look like this:
        // insert into message2_recips values (?, ?, ?, ?),(?, ?, ?, ?),(?, ?, ?, ?)
        $insertSql .= implode(',', $holders);
    
        // create a prepared statment
        $stmt = $PDO->prepare($insertSql);
    
        // execute the statement with the params
        $stmt->execute($params);
    

    PDO really is better. It has the same functionality as MySQLi but with a consistent interface across DB drivers (ie. as long as your SQL is compliant with a different database you can theoretically use the exact same php code with mysql, sqlite, postresql, etc.) AND much better parameter binding for prepared statements. Since you shouldnt be using the mysql extension any way, and MySQLi is more cumbersome to work with than PDO its really a no-brainer unless you specifically have to support an older version of PHP.

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

Sidebar

Related Questions

Wondering if someone could help with with some Spring.net IOC integration into a .aspx
I'm wondering if someone could look over my code. I'm trying to pass a
I was wondering if someone could help me with some javascript as I'm quite
I'm trying to get started with HtmlAgilityPack and was wondering if someone could help
Hey guys I was wondering if someone could provide a little help. I've been
I was wondering if someone could tell me what I am doing wrong that
I was wondering if someone could help me understand this problem. I prepared a
I'm relatively new to hibernate and was wondering if someone could help me out.
I was wondering if someone could help I've successfully been able to import an
I was wondering if someone could help with an example criteria for nhibernate. I

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.