Php accesses the mysql database and phpmysql Database

Source: Internet
Author: User

Php accesses the mysql database and phpmysql Database

PHP can access the Mysql database through the mysql interface and mysqli interface. You must add the mysql and mysqli interfaces to access the mysql database. In windows, configure amp: a and install apache: Find \ Apache \ conf \ httpd. in the conf file, open it in notepad, find the Define SRVROOT option, and change the value on the right to the directory address where your Apache installation is stored, for example, Define SRVROOT "D: /apache "Next, you need to configure and install the main service of Apache. With this service, Apache can be started: Open the CMD window and enter:" D: \ application_software \ Apache \ bin \ httpd.exe "-k install-n apache remember, contains quotation marks. In the installation directory, find the executable file D: \ application_software \ Apache \ bin \ ApacheMonitor.exe and double-click it. The icon appears in the lower right corner of the desktop. Double-click the window to open the window. Click start on the left side to start the apache service. Note: To uninstall apache, run SC delete apache. Configure apache: 1. Modify the information in httpd. conf. The default path is C: \ ProgramFiles \ Apache Software Foundation \ Apache2.2. The httpd. conf file is in the conf folder under the apache server directory. In httpd. add the following information to the conf file: loadModule php5_module "C:/php/php5apache2_2.dll" -- Note: add the AddType application/x-httpd-php module to apache. php. html. htm PHPIniDir "D:/Web/PHP5.2.17/php. ini "-- Note: Specify php. ini configuration file directory. You do not need to add this sentence to php. ini is stored in the C:/windows directory. Note: -- AddType enables apache to parse the php code in three files:. php. html. htm. 2. Load the mysql database (libmysql is not available in php5.3 or later versions. dll), which can be found in httpd. before loading php5_module in ini, add this sentence: LoadFile "E: \ server \ php528 \ libmysql. dll "or use libmysql in the php Directory. copy the dll file to the C: \ windows folder. You can also add the php Directory to the environment variable path. Configure php. ini: 1. Rename the PHP. ini-recommended file under the php Directory to php. ini. In php. add the following information to the INI file: extension_dir = "C:/php/ext" -- Note: specify the php extension directory location extension = php_mysql.dll -- enable mysql and mysqli Dynamic Link Library extension = php_mysqli.dll If php. ini contains "extension =. /", directly change the value after the equal sign to the above path. If extension = php_mysql.dll and extension = php_mysqli.dll exist and there is a semicolon before them, remove the semicolon. In php. ini, semicolons are used to indicate comments of the following information. 2. Click All Programs | apache http server 2.2 | control apache server | restart to restart the apache server. 3. Create the test. php file in the htdocs directory under the apache Server installation directory. Enter <? Php phpinfo ();?> Php: VC11 x86 Non Thread Safe version for iis, VC11 x86 Thread Safe for apache Note: phpinfo () display configuration file (php. ini) if the path value is C: \ windows, this indicates the configuration file php. by default, ini is stored in C: \ windows. If the value of loaded configuration file is C: \ windows \ php. ini, this indicates that the php configuration file is loaded from the file. In linux, configure php1gz to copy php-5.2.11.tar.gz to the/usr/local/src directory, and decompress and install php in the directory. Command: cp/home/hih/download/php-5.2.11.tar.gz/usr/local/src/-- copy the file to/usr/local/src/cd/usr/local/src/-- switch the directory /usr/local/src/lower tar-xzvf php-5.2.11 --decompress tar.gz compressed package cd php-5.2.11 -- switch directory to php-5.2.11 -- set compilation options via -- prefix, -- with-mysql, -- with-mysqli, etc. option. /configure -- prefix =/usr/local/php -- with-mysql =/usr/local -- with-mysqli =/usr/bin/mysql_configmake -- start to compile make install -- to install make clean -- clear compilation result note: -- prefix: Set the installation path. -- With-mysql: adds a traditional mysql interface to php. The value is the installation path of mysql. -- with-mysqli: add the mysqli interface to php. For more parameters, see "./confiure -- help. 2. Configure httpd for the apache server. 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. Use vi to open httpd. conf, add the following information: loadModule php5_module modules/libphp5.soAddType application/x-httpd-php. php restarts the apache server:/etc/init. d/httpd restart to test whether php is successfully installed. You can create test in the/var/www/html/directory. PHP file, such as: <? Php phpinfo ();?> To connect to the mysql database php, you can connect to the mysql database through mysql or mysqli interfaces. 1. the mysql interface provides the mysql_connect () method to connect to the mysql database, for example, $ connection = mysql_connect ("host/IP", "username", "password "); you can also specify which database to log on to, for example, $ connection = mysql_connect ("host/IP", "username", "password", "database "); 2. The mysqli interface has two commonly used classes: mysqli and mysqli_result. Mysqli is mainly used to establish a connection with the mysql database, where the query () method is used to execute SQL statements. Mysqli_result is mainly used to process the query results of select statements. For example: $ 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 whether an error occurs when php connects to the mysql database. If an error occurs, the function returns true. The mysqli_connect_error () function returns an error message. After connecting to the database, you can use the select_db () function to select a database. You can also use functions such as get_client_info () and get_server_info () to obtain mysql information. For example, connect to a 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 successful </p> \ n ";}?> You can use the query () function to query, insert, update, and delete data in the mysql database. However, the query () function can only execute one SQL statement at a time. You can use the multi_query () function to execute multiple SQL statements at a time. If the statement is successfully executed, query () returns true; otherwise, false. You can use the affected_rows () function to obtain the number of records that have changed. For example, php uses the query () function to execute the statement $ result = $ connection-> query ("insert into score values (11,908, 'French ', 88 )"); if ($ result) {echo "<p> insert statement execution succeeded </p>"; echo "<p> Number of inserted records:", $ connection-> affected_rows, "</p>"; // return the number of inserted records} else {echo "<p> insert statement execution failed </p> ";}

Related Article

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: 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.