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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:10:33+00:00 2026-06-05T06:10:33+00:00

I’ve got a website where I connect to a mySQL database to make a

  • 0

I’ve got a website where I connect to a mySQL database to make a number of queries, in the usual fashion. I’m not doing anything more complicated than:

$result = mysql_query('SELECT * FROM table WHERE condition = "'.mysql_real_escape_string($_POST['condition']).'"');
$row = mysql_fetch_assoc($result);
echo $row['var1'].' '.$row['var2'];

And it works. But I’ve been reading up about prepared statements and they seem to offer more security and I’d like to use them and replace my database calls with some prepared statements, so I’ve been looking at the mysqli class.

But it seem so much more code to achieve the same thing. I understand I’d have to do this to get the above:

$stmt = $db->stmt_init();
if($stmt->prepare('SELECT * FROM table WHERE condition = ?')) {
$condition = $_POST['condition'];
$stmt->bind_param('s', $condition);
$stmt->execute();

$stmt->bind_result($var1, $var2, ...);
if ($stmt->fetch()) {
    echo $var1 . ' - ' . $var2;
}
}

So it seems like a hell of a lot more code, and a bit harder to manage. Am I misunderstanding how to use these or is there a shorter way of doing the “normal” PHP things:

  • Populating $row, being an array representing one single line from the database.
  • Looping over rows, and refilling $row with the “next row” along.
  • Normal UPDATE enquiries.

The above are all nice and quick to do “normally” but seem like they would take many more lines using prepared statements.

  • 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-05T06:10:34+00:00Added an answer on June 5, 2026 at 6:10 am

    A common way is to wrap database functionality into a class. Here’s a simple one implementing caching of the prepared statements:

    class DB {
      protected $db;
      protected $cache;
    
      public function __construct($host, $database, $user, $pass, $charset = 'utf8') {
        $this->db = new PDO(sprintf('mysql:dbname=%s;host=%s', $database, $host, $charset),
                            $user, $pass);
        $this->cache = array();
        $this->db->query(sprintf('SET NAMES %s', $charset));
      }
    
      public function query($query, $vars = array()) {
        //You may input a simple value, no need for arrays with a single argument                              
        if (!is_array($vars))
          $vars = array($vars);
    
        //Short names inside the function                                                                      
        $db = &$this->db;
        $cache = &$this->cache;
    
        //Ensure the prepared statement is in cache                                                            
        if (!isset($cache[$query]))
          $cache[$query] = $db->prepare($query);
    
        //Execute the statement and return all rows                                                            
        $stmt = $cache[$query];
        if ($stmt->execute($vars))
          return $stmt->fetchAll();
        else
          return false;
      }
    }
    

    Usage of this is very close to the older database interfaces. Example:

    $db = new DB(host, database, user, pass);
    $result = $db->query('SELECT id, name FROM table WHERE id = ? AND address = ?',
                         array(42, 'home'));
    foreach ($result as $row) {
      ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
Seemingly simple, but I cannot find anything relevant on the web. What is the
I need a function that will clean a strings' special characters. I do NOT

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.