Archive:PHP and MySQL Programming/Database Connectivity: Difference between revisions

wikademia>Ralfe
wikademia>Wikademia
 
(15 intermediate revisions by 7 users not shown)
Line 1: Line 1:
[[Programming:PHP and MySQL|TOC]]
== Introduction ==
== Introduction ==


You can only do so much with storing information in files. When you need to store large amounts of data, and perform intensive number crunching on that data, there is nothing better than a good SQL database. In this section, we'll discuss connecting PHP to a MySQL database and perform queries and retrieve data back fom the database.
You can only do so much with storing information in files. When you need to store large amounts of data, and perform intensive number crunching on that data, there is nothing better than a good SQL database. In this section, we'll discuss connecting PHP to a MySQL database and perform queries and retrieve data back from the database.


== Opening a Connection to a MySQL Database ==
== Opening a Connection to a MySQL Database ==
Line 20: Line 17:
== Creating a Query ==
== Creating a Query ==


Once a connection to a database has been made, you will inevetably want to perform a database query. To create a query on the selected database, we use the mysql_query() function. If you use a SELECT query, then the data returned from that query will be passed to the mysql_query() function, which will in turn return it to a variable which you can specify. In the following example, two queries are made, the first does not return any data, and the second does.
Once a connection to a database has been made, you will inevitably want to perform a database query. To create a query on the selected database, we use the mysql_query() function. If you use a SELECT query, then the data returned from that query will be passed to the mysql_query() function, which will in turn return it to a variable which you can specify. In the following example, two queries are made, the first does not return any data, and the second does.


  <?
  <?
Line 50: Line 47:
  while ($row = mysql_fetch_row($result)){
  while ($row = mysql_fetch_row($result)){
     foreach ($row as $field) {
     foreach ($row as $field) {
       print "$filed . ";
       print "$field . ";
     }
     }
     print "<BR>";
     print "";
  }
  }
  ?>
  ?>
Line 90: Line 87:
  ?>
  ?>


[[Programming:PHP and MySQL:File Handling|<< 3. File Handling]] | [[Programming:PHP and MySQL|TOC]] | [[Programming:PHP and MySQL:Session Handling|>> 6. Session Handling]]
[[../File Handling/|Previous: File Handling]] | [[../|TOC]] | [[../Session Handling/|Next: Session Handling]]
 
[[Category:PHP and MySQL Programming|{{SUBPAGENAME}}]]