PHP/MySQL Database/mysql fetch array

Материал из Web эксперт
Перейти к: навигация, поиск

An Example of Using the Old MySQL Extension

   <source lang="html4strict">

<?php

   mysql_connect("localhost", "username", "password");
   mysql_select_db("mydatabase");
   $result = mysql_query("SELECT * FROM mytable");
   while($row = mysql_fetch_array($result)) {
       foreach($row as $key=>$value) {
           echo "$key = $value
\n"; } } mysql_free_result($result); mysql_close();

?>

 </source>
   
  


Get data from database

   <source lang="html4strict">

<html>

<head>
 <title>Get data</title>
</head>
<body>

<?php $conn=@mysql_connect( "localhost", "userName", "password" ) or die( "Err:Conn" ); $rs = @mysql_select_db( "my_database", $conn ) or die( "Err:Db" ); $sql = "select id, first_name from my_table"; $rs = mysql_query( $sql, $conn ); while( $row = mysql_fetch_array( $rs ) ) {

  echo( "ID: " . $row["id"] );
  echo( " - FIRST NAME: " . $row["first_name"] . "
" );

} ?> </body> </html>

 </source>
   
  


mysql_fetch_array-2.php

   <source lang="html4strict">

<? mysql_connect("localhost","root",""); mysql_select_db("mydatabase"); $query = "SELECT id, title FROM mytable ORDER BY title"; $result = mysql_query($query); while ($row = mysql_fetch_array($result, MYSQL_NUM)) {

   $title = $row[1];
   $id = $row[0];
   echo "Product:  $title ($id) 
";

} ?>

 </source>
   
  


mysql_fetch_array.php

   <source lang="html4strict">

<? mysql_connect("localhost","root",""); mysql_select_db("mydatabase");

$query = "SELECT id, title FROM mytable ORDER BY title"; $result = mysql_query($query); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

   $title = $row["title"];
   $id = $row["id"];
   echo "Product:  $title ($id) 
";

} ?>

 </source>
   
  


Retrieving and Displaying Results

   <source lang="html4strict">

<?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"] . "
"; echo "Title: " . stripslashes ($adata["title"]) . "
"; echo "Artist: " . stripslashes ($adata["myvalue"]) . "
"; } } else { echo mysql_error(); } closedatabase ($db);

?>

 </source>
   
  


Using timestamp with mysql

   <source lang="html4strict">

<html> <head> <title>Using timestamp</title> </head> <body> <?php

 $conn = mysql_connect( "localhost", "userName", "password" ); 
 $rs = mysql_select_db( "my_database", $conn );
 $sql = "select * from guestbook where id=1";
 $rs = mysql_query( $sql, $conn );

 #split the timestamp into organized date format 
 while ( $row = mysql_fetch_array( $rs ) ) 
 {
   $datetime = $row["time"];
   $year = substr( $datetime, 0, 4 );
   $mon  = substr( $datetime, 4, 2 );
   $day  = substr( $datetime, 6, 2 );
   $hour = substr( $datetime, 8, 2 );
   $min  = substr( $datetime, 10, 2 );
   $sec  = substr( $datetime, 12, 2 );
   $orgdate = date("l F dS, Y h:i A", mktime( $hour, $min, $sec, $mon, $day, $year ) );
   echo( "Time of entry: " . $orgdate );
 }

?> </body> </html>

 </source>
   
  


while loop and mysql_fetch_array() function

   <source lang="html4strict">

<html>

<head>
 <title>Get data</title>
</head>
<body>
<?php
$conn = @mysql_connect( "localhost", "userName", "password" ) or die( "Err:Conn" );
$rs = @mysql_select_db( "my_database", $conn )  or die( "Err:Db" );
$sql = "select id,first_name from my_table order by first_name";
$rs = mysql_query( $sql, $conn );
while( $row = mysql_fetch_array( $rs ) )
{
  echo( "ID: " . $row["id"] );
  echo( " - FIRST NAME: " . $row["first_name"] . "
" ); } ?> </body>

</html>

 </source>