PHP Access MySQL Database

Source: Internet
Author: User
Tags install php phpinfo

PHP access MySQL database php can access MySQL database via MySQL interface and mysqli interface. Need to join MySQL and mysqli interface to access MySQL database. Under Windows configuration amp:a, install Apache: Locate the \apache\conf\httpd.conf file, open it with Notepad, find: Define srvroot This, change the value to the right to the current directory address of your Apache installation, such as: Define srvroot "D:/apache" next need to configure the installation of Apache Master Service, with it, Apache can start: Open the cmd window, enter: "D:\application_software\Apache\bin\ Httpd.exe "-K install-n Apache remember to include quotation marks. In the installation directory, locate the D:\application_software\Apache\bin\ApacheMonitor.exe executable, double-click to run, the icon appears in the bottom right corner of the desktop, double-click the Open Window interface. Click Start on the left to start the Apache service. Note: To uninstall Apache, enter SC delete Apache under commands. Configure Apache:1, Apache server configuration file httpd.conf to modify the information. The default path is C:\ProgramFiles\Apache software foundation\apache2.2. The httpd.conf file is in the Conf folder in the Apache server directory. Add information in httpd.conf: LoadModule php5_module "C:/php/php5apache2_2.dll"--note: Apache Loading PHP module AddType application/ x-httpd-php. php. html. htm phpinidir "D:/web/php5.2.17/php.ini"-Note: Specify the directory of the php.ini configuration file, add this sentence and then do not put php.ini to c:/ The Windows directory is down. Note:--addtype is the PHP code that lets Apache parse. php. html. htm three files. 2, load MySQL library (php5.3 above version no Libmysql.dll), you can load php5_module in Httpd.ini before adding this sentence: LoadFile "E:\server\php528\libmysql.dlL "or copy the Libmysql.dll file under the PHP directory to the C:\windows folder. Or you can add the PHP directory into the environment variable path. Configure Php.ini:1, rename the php.ini-recommended file under PHP directory to php.ini. Add the following information in the php.ini file: extension_dir= "C:/php/ext"--note: Specify the location of the PHP extension directory Extension=php_mysql.dll-- Turn on MySQL and mysqli dynamic link library Extension=php_mysqli.dll If there is "extension=./" in php.ini, change the value after the equal sign directly to the path above. If there are Extension=php_mysql.dll and Extension=php_mysqli.dll, and there is a semicolon in front, then the semicolon is removed. The php.ini semicolon is used to represent the comment that follows the message. 2. Click All Programs |apache HTTP server 2.2|control Apache server|restart option to restart the Apache server. After 3, you can create a new test.php file under the Htdocs directory under the Apache Server installation directory. The file entered: <?php phpinfo ();? >php:http://www.php.net/downloads.php VC11 x86 Non thread Safe version for IIS,VC11 x86 thread Sa Fe for Apache Note: phpinfo () Displays the value of the configuration file (php.ini) path if it is C:\windows, which means that the default storage location for the profile php.ini is C:\windows. If the value of loaded configuration file is C:\windows\php.ini, this means that the PHP configuration files are loaded from the file. Configure PHP1 under Linux, copy the php-5.2.11.tar.gz to the/USR/LOCAL/SRC directory, and then unzip and install PHP in that directory. Command: cp/home/hih/download/php-5.2.11.tar.gz/usr/local/src/--Copy file to/usr/local/src/cd/usr/local/src/--Switch directory to/usr/ local/src/under Tar -XZVF php-5.2.11--Decompression tar.gz Compression Pack CD php-5.2.11--Switch directory to php-5.2.11--set compilation options via--prefix 、--with-mysql 、-- With-mysqli and other options to set./configure--prefix=/usr/local/php--with-mysql=/usr/local--with-mysqli=/usr/bin/mysql_ Configmake--Start compiling make install--do install makes clean--clear compilation Results Note:--prefix: Sets the installation path. Add a traditional MySQL interface to the--with-mysql:php. The value behind it is the installation path for MySQL, and the Mysqli interface is added to the--with-mysqli:php. More parameters can be viewed by "./confiure--help". 2. Start configuring the Apache server's httpd.conf file if the Apache server is installed in the/usr/local/apache directory, then httpd.conf should be in the/usr/local/apache/conf/directory. Open httpd.conf with VI, add the following information: LoadModule php5_module modules/libphp5.soaddtype application/x-httpd-php. php restart Apache server:/ ETC/INIT.D/HTTPD Restart test if PHP is installed successfully can create test.php file in/var/www/html/directory, such as: <?php phpinfo ();?> Connect MySQL database php can connect to MySQL database via MySQL or mysqli interface. 1, MySQL interface provides mysql_connect () method to connect MySQL database, such as: $connection =mysql_connect ("HOST/IP", "username", "password"); You can also specify which database to log on to, such as: $connection =mysql_connect ("HOST/IP", "username", "password", "Database"), 2, mysqli interfaces have two more commonly used classes, Mysqli and Mysqli_result, respectively. Mysqli mainly used with MySQL database to establishThe connection, where the query () method is used to execute the SQL statement. Mysqli_result is primarily used to process query results for SELECT statements. such as: $connection =new mysqli ("HOST/IP", "username", "password", "database"); Note: The Mysqli interface provides more functions than the MySQL interface. The Mysqli_connect_errno () function can determine if PHP has an error connecting to the MySQL database. If an error occurs, the function returns True. The Mysqli_connect_error () function returns an error message. If you connect to the database, you can select the database through the select_db () function. MySQL information can also be obtained through functions such as get_client_info (), Get_server_info (). Example: Connection database <?php$connection=new mysqli ("localhost", "root", "Huang", "test"), if (Mysqli_connect_error ()) {echo "<p > Connection failed: ", Mysqli_connect_error ()," </p>\n "; exit ();} Else{echo "<p> connection Success </p>\n";}? >php operation MySQL Database php can query, insert, UPDATE, and delete data by querying () function. However, the query () function can execute only one SQL statement at a time. You can execute multiple SQL statements at once by using the Multi_query () function. The statement executes successfully with query () returns TRUE, otherwise false is returned. The number of records that have changed can be obtained through the affected_rows () function. Example: PHP executes the statement via the query () function $result= $connection->query ("INSERT into score values (11,908, ' French ',") "); if ($result) {echo] <p>insert Statement execution Success </p> "; echo "<p> number of records inserted:", $connection->affected_rows, "</p>";//returns the number of records inserted}else{echo "<p>insert statement execution failed </p> ";}

PHP Access MySQL Database

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.