PHP/MySQL Database/Query Result

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

From result row get the table name

   <source lang="html4strict">

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

?>

      </source>
   
  


Get column alias

   <source lang="html4strict">

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

?>


      </source>
   
  


Get returned row count

   <source lang="html4strict">

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

?>

      </source>
   
  


Store query result in an array

   <source lang="html4strict">

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

?>


      </source>
   
  


Store query result in an associate array

   <source lang="html4strict">

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

?>


      </source>
   
  


Store query result in an Object

   <source lang="html4strict">

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

?>


      </source>
   
  


use Column name in select statement as the variable name

   <source lang="html4strict">

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

?>

      </source>
   
  


Use for loop to read all query result

   <source lang="html4strict">

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

?>


      </source>
   
  


Use mysql_result function to get query result

   <source lang="html4strict">

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

?>


      </source>