PHP/MySQL Database/mysql fetch array — различия между версиями

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

Версия 10:37, 26 мая 2010

An Example of Using the Old MySQL Extension

 
<?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<BR/>\n";
        }
    }
    mysql_free_result($result);
    mysql_close();  
?>



Get data from database

 
<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"] . "<br>" );
}
?>
</body>
</html>



mysql_fetch_array-2.php

 
<?
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) <br />";
}
?>



mysql_fetch_array.php

 
<?
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) <br />";
}
?>



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



Using timestamp with mysql

 
<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>



while loop and mysql_fetch_array() function

 
<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"] . "<br>" );
 }
 ?>
 </body>
</html>