PHP access to the database cluster method summary, access to the data cluster _php tutorial

Source: Internet
Author: User
Tags getting started with php

PHP access to the database cluster method summary, access to the data cluster


This paper summarizes and analyzes the method of PHP accessing database cluster. Share to everyone for your reference, as follows:

There are three common ways to do this:

1, automatically determine whether SQL is read, to select the database connection:

When instantiating a PHP db class, you need to connect two servers at a time, and then choose different connections based on SLQ, for example:

$link _w = mysql_connect ($w _host, $user, $pwd), $link _r = mysql_connect ($r _host, $user, $pwd);//Execute Sqlif (Preg_match ("/^ Select/i ", Trim ($sql))) {  mysql_query ($sql, $link _r);} else {  mysql_query ($sql, $link _w);}

The advantage of this method is that the developer executes SQL without distinguishing between reading and writing, and at the bottom of the DB class, the disadvantage is that it is often necessary to open two connections only when reading or writing.

2, when the call to choose their own:

It is generally possible to determine whether to write or read before executing SQL, so developers need to call different connections manually, for example:

$w _db = new db (' W '); $w _db, query (' INSERT INTO ... ');

When SQL is read:

$r _db = new db (' R '), $r _db, query (' Select ... ');

Mainly through, pass parameters to distinguish whether SQL is read or write, each call to SQL needs to call the developer's own judgment.

3, using the MySQL proxy to do the middle-tier agent, will automatically judge the SQL is read or write, the request forwarded to the server. The advantage is that the program does not have to change any code, as long as the MySQL proxy is started by specifying a read or write server:

--proxy-backend-addresses--proxy-read-only-backend-addresses

More readers interested in PHP related content can view this site topic: "PHP Operations Office Document Tips summary (including word,excel,access,ppt)", "PHP date and Time usage summary", "PHP Object-oriented Programming tutorial", "PHP string (string) Usage summary, "Getting Started with Php+mysql database operation" and "PHP common database Operation Skills Summary"

I hope this article is helpful to you in PHP programming.

Articles you may be interested in:

    • Simple analysis of PHP program to prevent DDOS,DNS, Cluster Server attack solution
    • Nginx+apache+mysql+php+memcached+squid Building a clustered Web environment
    • Shell, Perl, Python, PHP access MySQL database Code instance
    • PHP Access MySQL Database encapsulation Class (with function description)
    • thinkphp implementation of multi-database connection
    • Multi-database operation of ThinkPHP3.1 new features more perfect
    • PHP connects multiple MySQL database sample code simultaneously
    • Code for multiple databases in CI operations in PHP
    • Application Design for PHP multi-database support
    • PHP connection MySQL DB instance code
    • Database connection in PHP comparison of PDO and mysqli
    • How PHP connects to Oracle database and query data
    • How PHP uses PDO to connect and query SQL database

http://www.bkjia.com/PHPjc/1110094.html www.bkjia.com true http://www.bkjia.com/PHPjc/1110094.html techarticle PHP access to the database cluster method summary, access to the data cluster This paper summarizes and analyzes the method of PHP accessing the database cluster. Share to everyone for your reference, as follows: General Common ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.