Retrieving and Displaying Results : mysql_fetch_array « MySQL Database « PHP






Retrieving and Displaying Results

 
<?php

  function opendatabase ($host,$user,$pass) {
    try {
      if ($db = mysql_connect ($host,$user,$pass)){
        return $db;
      } else {
        throw new exception ("Sorry, could not connect to mysql.");
      }
    } catch (exception $e) {
      echo $e->getmessage ();
    }
  }
  
  function selectdb ($whichdb, $db){
    try {
      if (!mysql_select_db ($whichdb,$db)){
        throw new exception ("Sorry, database could not be opened.");
      }
    } catch (exception $e) {
      echo $e->getmessage();
    }
  }
  
  function closedatabase ($db){
    mysql_close ($db);
  }
  
  $db = opendatabase ("localhost","root","");
  
  selectdb ("mydatabase",$db);
  
  if ($aquery = mysql_query ("SELECT * FROM mytable ORDER BY id ASC")){
    while ($adata = mysql_fetch_array ($aquery)){
      echo "ID: " . $adata['id'] . "<br />";
      echo "Title: " . stripslashes ($adata['title']) . "<br />";
      echo "Artist: " . stripslashes ($adata['myvalue']) . "<br />";
    }
  } else {
    echo mysql_error();
  }
  
  closedatabase ($db);
  
?>
  
  








Related examples in the same category

1.An Example of Using the Old MySQL Extension
2.while loop and mysql_fetch_array() function
3.Get data from database
4.mysql_fetch_array-2.php
5.mysql_fetch_array.php
6.Using timestamp with mysql