Mega Code Archive

 
Categories / Php / MySQL Database
 

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);    ?>