| || |
Join Date: Jun 2009
How to connect PHP to mySQL Database
PHP syntax is very similar to the syntax of C or Perl. People familiar with programming will be able to very quickly begin to write programs in PHP. In this language there is no strict data typing and there is no need for action on allocation / freeing memory. MySQL - compact multi-threaded database server. MySQL is characterized by a high speed, stability and ease of use. MySQL is the ideal solution for small and medium-sized applications. MySQL supports a query language in the ANSI SQL 92, and in addition has a set of extensions to this standard are not found in any other database. MySQL is really a very fast server.
Retrieving Records from a MySQL database using PHP is one of the most common tasks is presented every web developer.
Connection to Server
- First Create a Database. To save, read, modify, or delete data, you must first establish a connection to MySQL as described above via a file-like connexion.php.
- To get the data from MySQL need a connection (a form of link or communication), which will allow us to extract data from it. This PHP has a number of unique features to interact with mysql . Then to connect use the function mysql_connect mysql_select_db which then allows us to define which database server you want to connect.
- Now the other part is obtaining records from the database. Once you had made a connection to extract the data, you will need to send SQL queries using mysql_query function. By this you can also get the total number of records returned by your query.
- Behind these two terms are hidden concepts. The dataset is a direct response to a query in MySQL. The dataset contains 0, 1, 2 ... No records. Each record is a DataRow as the data line. In fact, when asked MySQL, it returns a block of responses (dataset) that we will peel with php (DataRow DataRow by).
- To use PHP via this line, we use the command mysql_fetch_array ($ dataset): This command creates an associative array where each key corresponds to each column of the table, and each value for each value read from the dataset.
- print_r will display the source code.
- Remember that array keys are case-sensitive, if the column is called MySQL Manufacturer or producer, this is not the same. It only remains to show the information required in PHP (sample code in a html page named pieces.php).
- To send queries to MySQL, you must first have access, that is to say, have been registered by the administrator of the mysql database.
- It should then connect to the server using the mysql_connect (all functions php / mysql start with mysql_).
- This function calls as arguments the name of the host machine where the MySQL server, the name of the authorized user and the password for that user.
- This will attempt to connect to the MySQL database server which runs on the host INFRES under the username.
- It is obvious that in practice, the password should never be placed in a plain php script: the arguments of this function will be entered in the first place by a form whose implementation will start by trying the log with the information provided by the user. Once the connection is established, you can use the manipulation of databases.