PHP/MySQL Database/Query Result — различия между версиями

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

Текущая версия на 07:06, 26 мая 2010

From result row get the table name

<?php
   mysql_connect("mysql153.secureserver.net","wbex","password");
   mysql_select_db("wbex");
   $query = "SELECT id as Employee_ID FROM Employee";
   $result = mysql_query($query);
   $row = mysql_fetch_row($result);
   echo mysql_field_table($result, 0);
?>



Get column alias

<?php
   mysql_connect("mysql153.secureserver.net","wbex","password");
   mysql_select_db("wbex");
   $query = "SELECT FirstName as First_Name FROM Employee";
   $result = mysql_query($query);
   $row = mysql_fetch_row($result);
   echo mysql_field_name($result, 0);
?>



Get returned row count

<?php
   mysql_connect("mysql153.secureserver.net","wbex","password");
   mysql_select_db("wbex");
   $query = "SELECT * FROM Employee";
   $result = mysql_query($query);
   echo "There are ".mysql_num_rows($result)." Employee(s).";
?>



Store query result in an array

<?php
   mysql_connect("mysql153.secureserver.net","wbex","password");
   mysql_select_db("wbex");
   $query = "SELECT * FROM Employee";
   $result = mysql_query($query);
   while ($row = mysql_fetch_array($result,MYSQL_NUM)) {
      $name = $row[1];
      $developerid = $row[0];
      echo "Product: $name ($developerid) <br />";
   }
?>



Store query result in an associate array

<?php
   mysql_connect("mysql153.secureserver.net","wbex","password");
   mysql_select_db("wbex");

   $query = "SELECT id, firstname FROM Employee";
   $result = mysql_query($query);
   while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
      $name = $row["name"];
      $developerid = $row["id"];
      echo "Product: $name ($id) <br />";
   }
?>



Store query result in an Object

<?php
   mysql_connect("mysql153.secureserver.net","wbex","password");
   mysql_select_db("wbex");
   $query = "SELECT id, firstName FROM Employee";
   $result = mysql_query($query);
   while ($row = mysql_fetch_object($result)) {
      $name = $row->firstName;
      $id = $row->id;
      echo "Product: $name ($id) <br />";
   }
?>



use Column name in select statement as the variable name

<?php
   mysql_connect("mysql153.secureserver.net","wbex","password");
   mysql_select_db("wbex");
   $query = "SELECT id, firstname FROM Employee";
   $result = mysql_query($query);
   while (list($id, $name) = mysql_fetch_row($result)) {
      echo "$name ($id) <br />";
   }
?>



Use for loop to read all query result

<?php
   mysql_connect("mysql153.secureserver.net","wbex","password");
   mysql_select_db("wbex");
   $query = "SELECT id, firstname FROM Employee";
   $result = mysql_query($query);
   for ($count=0; $count <= mysql_numrows($result); $count++) {
      $id = mysql_result($result, $count, "id");
      $name = mysql_result($result, $count, "firstname");
      echo "$firstname ($id) <br />";
   }
?>



Use mysql_result function to get query result

<?php
   mysql_connect("mysql153.secureserver.net","wbex","password");
   mysql_select_db("wbex");

   $query = "SELECT id, firstname FROM Employee";
   $result = mysql_query($query);
   $id = mysql_result($result, 0, "id");
   $name = mysql_result($result, 0, "firstname");
   echo "The first returned id is $id and the name is $name";
?>