The basic syntax for making connection to MYSQL database is as follows

 

$cnn=mysql_connect("name of database server","database name","password");

 

We have to select the database before doing transaction in that database. It can be done as follows

 

mysql_select_db("database name",$cnn) or die("Error while selecting database");

 

To execute a select statement in database, mysql_query can be used as follows.

 

$sql= "select title, description from article where type_of = '$type_of' order by rand()";

$result=mysql_query($sql) or die('Error executing Query');

$tot_rows = mysql_num_rows($result);

 

The result rows can be displayed as follows

 

$i = 1

While ( $i<$tot_rows)

{

?>

  <tr align="left" valign="middle">

              <td><strong><? mysql_result($result,$i,"title") ?></strong></td>

  </tr>

  <tr align="left" valign="middle">

              <td><? echo mysql_result($result,$i,"description")?> </td>

  </tr>

  <?

$i++;

}

 

To Close the MYSQL Connection, use following statement.

 

mysql_close();

 

 

0 comments: