PHP

Querys

Two types:

Querys are issued by calling the mysql_query() function which takes a query string and a optional connection identifier as arguments.

mysql_query() returns TRUE, FALSE, or a result identifier which must be interpreted according to the type of query issued.

Querys that return a result set

SELECT statements.

mysql_query() returns FALSE or a result identifier. Use the result identifier to obtain further information using:

Examples:

$result_id = mysql_query ("SELECT COUNT(*) FROM member");
if (!result_id) || !($row = mysql_fetch_row ($result_id)))
  print ("query failed\n");
else
  print ("The member table has $row[0] records\n");

When returning more than one row, use a loop:

$result_id = mysql_query ("SELECT lname,fname FROM member");
if (!result_id))
  print ("query failed\n");
else {
  while (list($fname,$lname) = mysql_fetch_row ($result_id))
    printf ("%s %s\n", $fn, $ln);
 mysql_free_result ($result_id);

Each call to mysql_fetch_row returns the next row as an array. To find out how many elements in the array use mysql_num_fields().

Querys that do not return a result set

Non-SELECT statements such as DELETE, INSERT, REPLACE, and UPDATE.

mysql_query() returns TRUE or FALSE to indicate success or failure of the query.

If the query is successful, call mysql_affected_rows() to find out how many rows were changed (deleted, inserted, replaced, or updated).

If the query fails, call mysql_error() or mysql_errno() to determine the error.


Send mail to the Webmaster

logo This site best viewed with a browser
Warning: This is a Debian centric site and MAY contain peanuts.
Many thanks to Debra Lynn and Ian Murdock for making Debian possible
First created Apr 22, 2008 ~ Last revised October 24, 2010

Valid XHTML 1.0 Strict Valid CSS!