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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:57:13+00:00 2026-06-08T22:57:13+00:00

PHP function mysql_connect returns an existing connection, if not setting the forth parameter ‘$new_link’

  • 0

PHP function mysql_connect returns an existing connection, if not setting the forth parameter ‘$new_link’ TRUE explicitly.
In the apache environment, does calling mysql_connect without setting $new_link TRUE might cause racing condition for the mysql connection resource?
In CLI environment, I have proved that the race condition does appear. But not in apache.
So, why? Does apache only use one process model?
CLI code example as following:

// create share memory
$nShmKey = ftok(__FILE__, 'i');
$nShmID = shm_attach($nShmKey, strlen($sArr) * 2);

// write the array to the shared memory
$nArrKey = 1;
shm_put_var($nShmID, $nArrKey, $arr);

// create semphore
$nSemID = sem_get(1, 1);

// child process consume the data in the shm
for($i = 0; $i < PROC_NUM; ++$i) {
    $nPID = pcntl_fork();

    if ($nPID == 0) {
        // child
        // create db link
        $oLink = mysql_connect(
                'my_server', 
                'my_user', 
                'my_password', 
                TRUE /*if set this false, it will cause race condition in each child*/
        );
        while (true) {
            sem_acquire($nSemID);

            // get the value
            $arrCur = shm_get_var($nShmID, $nArrKey);

            if (0 == count($arrCur) || $arrCur == FALSE) {
                // value out
                sem_release($nSemID);
                break;
            }
            $nVal = array_pop($arrCur);
            if (FALSE == shm_put_var($nShmID, $nArrKey, $arrCur)) {
                die('Failed to write array to shm');
            }
            sem_release($nSemID);

            // just insert the result to db
            mysql_query("INSERT INTO some_table(val) VALUES({$nVal})", $oLink);
        }
        exit(0);
    }
}

// wait for children
$n = 0;
while ($n < PROC_NUM) {
    $nStatus = -1;
    $nPID = pcntl_wait($nStatus, WNOHANG);
    if ($nPID > 0) {
        echo "{$nPID} exit\n";
        ++$n;
    }
}

// clear shm
sem_remove($nSemID);
shm_remove($nShmID);
echo "finished\n";
?>

I know that mysql link will not works well between multi-process, my question is:
Why, in apache, does the race condition for mysql link not happen?

  • 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-08T22:57:14+00:00Added an answer on June 8, 2026 at 10:57 pm

    No, it will only reuse the same connection within the running process (the same script). And since PHP scripts are generally not multi-threaded, there’s not problem. If you fork processes, you should be aware that you are then sharing the same connection, but that’s a special case.

    There is an option to use persistent connections, which is a kind of connection pool, where connections are shared across processes. But even then, the same connection won’t be dished out to two processes at the same time. In general, using persisten connections is not worth it though, since MySql is very fast at connecting.

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

Sidebar

Related Questions

I am trying to connect to a MySQL server using PHP's 'mysql_connect()' function, but
I'm looking for the equivalent to the PHP function mysql_real_escape_string() to use in C#.NET.
I have a PHP function which inserts multiple records into MySQL: function commit_purchase($asset_type_ID, $org_ID,
I've a php function to get some data from mysql database: function get_persons() {
I am trying to write a php function to stop MySQL injection attempts. What
I have a MySQL database and with a php function i made a table
Recently the PHP manual started showing the following warning on every mysql function page:
Is it possible to assign php array in MySQL IN() function? for example, $numbers
<?php class Page { function getPage($urlOfPage){ $result = mysql_query('SELECT category, title FROM rs-planet WHERE
I want to integrate the MySQL fulltext search function in my PHP site. 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.