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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:26:29+00:00 2026-06-12T20:26:29+00:00

I have a table with fields such as id , type , data ,

  • 0

I have a table with fields such as id, type, data, etc… I need to write a web service script in PHP which should insert data to table, then select last 10 record with same type and return in HTTP body.

1000 instances need to access this script per second.

My question is what is the best way to do it?

Here is my code. I am not sure this is good practice and could do with an example of how to make it better…

// Connect to mysql server
$link = mysql_connect(HOST, USER, PASSWORD);
if(!$link) {
    die('Could not connect to server: ' . mysql_error());
}

// Select database
$db = mysql_select_db(DATABASE);
if(!$db) {
    die('Cannot use the database');
}
mysql_set_charset('charset=utf8', $link); 

// Insert data
$query = "INSERT INTO `mytable` (`id`, `type`, `data`) VALUES ('$id', '$type', '$data')";   
mysql_query($query);

// Select data
$query = "SELECT * FROM `mytable` WHERE `type`='$type' ORDER BY DESC LIMIT 10";
$result = mysql_query($query);

while($row = mysql_fetch_assoc($result)) {

    // Create xml to return

}
  • 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-12T20:26:31+00:00Added an answer on June 12, 2026 at 8:26 pm

    Your PHP script itself (ignoring any security issues) will unlikely be a problem for scaling this up to the load you expect. Your infrastructure will matter and will make the difference between this script working for the loads you expect or not.

    Can your MySQL instance support 1000 new connections a second? Can your web server run this script 1000 times a second?

    The only way to work that out is to benchmark it figure out where you are, what loads can you support? If you can’t support it then you will need to find the bottleneck but I doubt it will ever be this script.


    Update in response to your comment, the best approach is to simulate the load you are expecting, there is no point worrying if your setup can handle this. If it can’t handle it then you need to narrow down the problem.

    First, download a tool like Apache JMeter. There are tutorials that you can follow to set up a simulation to try out your setup.

    From here you can work out the scope of the problem, if you can support more than the traffic you expect you probably won’t need to worry. If you can just about support it or you are a long way off you will need to find the bottlenecks which are preventing you from reaching this target.

    Narrow down the problem by testing the parts of the system separately, this is where you answer questions like how many connections can your web server or database support. Once you have identified the bottlenecks; the causes of what stops you from handling more traffic, you can then have a better idea of what you will need to do.

    Using Apache JMeter:
    http://www.davegardner.me.uk/blog/2010/09/23/effective-load-testing-with-apache-jmeter/
    Using mysqlslap to load test MySQL:
    http://dev.mysql.com/doc/refman/5.1/en/mysqlslap.html

    Bootnote: There will likely be a lot to learn… Hopefully this will get you started and you can support the loads you are looking for relatively painlessly. If not it will require a lot of reading up on scalable architecture for web services, advanced configuration of the systems you are using, and buckets of patience.


    Using PDO

    //Connect to mysql server
    $host = ""; // Your database host name
    $driver = "mysql";
    $database = ""; // Your database;
    $user = ""; // The user for the database;
    $password = ""; // The password for the database;
    
    // Create a DSN string from the above parameters
    $dsn = "$driver:host=$host;dbname=$database";
    
    // Create a connection you must have the pdo_mysql
    // extension added to your php.ini
    try {
        $db = new PDO($dsn, $user, $password);
    
    // The connection could not be made
    } catch(PDOException $ex)
        die("Could not connect to server: {$ex->getMessage()}");
    }
    
    // Prepare an insert statement
    $stmt = $db->prepare("
        INSERT INTO `mytable` (`id`, `type`, `data`) 
        VALUES      (:id, :type, :data)
    ");    
    
    // I'm guessing at the types here, use the reference
    // http://php.net/manual/en/pdo.constants.php to
    // select the right datatypes. Using parameter binding
    // you ensure that the value is converted and escaped
    // correctly for the database
    $stmt->bindParam(":id", $id, PDO::PARAM_INT);
    $stmt->bindParam(":type", $type, PDO::PARAM_STR);
    $stmt->bindParam(":data", $data, PDO::PARAM_LOB);
    
    // Execute the insert statement
    $stmt->execute();
    
    // Prepare a select data
    $stmt = $db->prepare("
        SELECT   * 
        FROM     `mytable` 
        WHERE    `type` = :type 
        ORDER BY `id` DESC 
        LIMIT 10
    ");
    
    // Again bind the parameters
    $stmt->bindParam(":type", $type, PDO::PARAM_STR);
    
    // Execute the select statement
    $stmt->execute();
    
    // There are different ways that fetch can return
    // a row, the web page above lists all of the
    // different types of fetch as well. In this case
    // we are fetching the rows as associative arrays
    while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    
        // Write XML for rows
    }
    
    // Finalise and output XML response
    

    Using PDO:
    http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/

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

Sidebar

Related Questions

I have Access table with fields, in which there is some data and path
I have a products table, with the fields productname, category and cost, of type
I'm working rearchitecting a reporting/data warehouse type database. We currently have a table that
I have table /entity called SaleRecord with fields such as @Entity public class SaleRecord
I have a table with (among others) x and y fields of SMALLINT type
I have two fields in a table. One contains values such as BTA, BEA,
i have payment table fields update reason and amount & total field are change
I have my Table structure like this :: ATT_Table : Fields - Act_ID, Assigned_To_ID,
I have table A with fields user1 and user2 and table B with user3
I have a table with fields user_id and votes . The votes field is

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.