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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:20:06+00:00 2026-06-09T03:20:06+00:00

I was informed by someone senior in our company today that the PHP code

  • 0

I was informed by someone senior in our company today that the PHP code I have written for performing prepared statements on a MySQL database is “inefficient” and “too taxing on our server”. Since then I find myself in the difficult position of trying to understand what he meant and then to fix it. I have no contact to said person for four days now so I am asking other developers what they think of my code and if there are any areas that might be causing bottlenecks or issues with server performance.

My code works and returns the results of my query in the variable $data, so technically it works. There is another question though as to whether it is efficient and written well. Any advice as to what that senior employee meant or was referring to? Here is the method I use to connect and query our databases.

(Please note, when I use the word method I do not mean a method inside a class. What I mean to say is this how I write/structure my code when I connect and query our databases.)

<?php

// Create database object and connect to database
$mysqli=new mysqli();
$mysqli->real_connect($hostname, $username, $password, $database);

// Create statement object
$stmt=$mysqli->stmt_init();

// Prepare the query and bind params
$stmt->prepare('SELECT `col` FROM `table` WHERE `col` > ?');
$stmt->bind_param('i', $var1);

// Execute the query
$stmt->execute();

// Store result
$stmt->store_result();

// Prepare for fetching result
$rslt=array();
$stmt->bind_result($rslt['col']);

// Fetch result and save to array
$data=array();
while($stmt->fetch()){
    foreach($rslt as $key=>$value){
        $row[$key]=$value;
    } 
    $data[]=$row;
}

// Free result
$stmt->free_result();

// Close connections
$stmt->close();
$mysqli->close();

?>

Any advice or suggestions are useful, please do contribute and help out even if you are only guessing. Thanks in advance 🙂

  • 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-09T03:20:08+00:00Added an answer on June 9, 2026 at 3:20 am

    There are two types of code that may be inefficient, the PHP code and the SQL code, or both.

    For example, the SQL is a problem if the `col` column isn’t indexed in the database. This puts lots of load on the database because the database has to scan very many rows to answer queries. If `col` isn’t indexed in the given query, then all of the rows in the table would be scanned. Also, if the value passed in isn’t very selective, then many rows will have to be examined, perhaps all of the rows, as MySQL will choose a table scan over an index scan when many rows will be examined. You will need to become familiar with the MySQL EXPLAIN plan feature to fix your queries, or add indexes to the database to support your queries.

    The PHP would be a problem if you followed something like the pattern:

    select invoice_id from invoices where customer_id = ?
    for each invoice_id
       select * from line_items where invoice_id = ?   
    

    That kind of pattern will lead to “over querying” the database, which puts extra load on it.
    Instead use a join:

    select li.* from invoices i join line_items li using (invoice_id)
    

    Ask your database administrator to turn on the slow query log and then process it with pt-query-digest

    You can use pt-query-digest to report on queries that are expensive (take a long time to execute) and also to use it to report by frequency to detect over querying.

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

Sidebar

Related Questions

Our manager today has informed us that our group will now handle a new
My hosting service just informed me that they don't have Git installed on any
Recently I have been informed that it is possible to concatenate integers (and other
My DBAs have informed me that one of my applications is really bogging down
I have just been informed that a Rails 3 project I've been working on
I was informed in a later answer that I have to add the GestureOverlayView
So I am still somewhat new to PHP, MySQL, and Javascript but I have
so i took the codility interview test yesterday and was informed today that i
I have just been informed that the Silverlight 4 Toolkit (latest download) requires the
Are they equal in safeness? I was informed that using <?=$function_here?> was less safe,

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.