Home   Notes   Contact Me

php

Local

External


Arrays

// Create an array $list = array(); // set Values $list = array( "alpha", "beta", "gamma" ); echo("list[0] = $list[0]
\n"); echo("list[1] = $list[1]
\n"); echo("list[2] = $list[2]
\n"); $list = array( "first" => "one", "second" => "two", "third" => "three" ); echo("list[first] = $list[first]
\n"); echo("list[second] = $list[second]
\n"); echo("list[third] = $list[third]
\n"); $number_of_elements = count($list);

Include

<?php include( "filename.php"); ?>

Reference http://www.tizag.com/phpT/include.php


MySQL

Connect to the Database

{link} mysql_connect([$server [, $username [, $password [, $new_link [, $flags]]]]]) // Connect to the server at hostname using the default port $server = 'hostname' // Connect to the server at hostname using the specified port $server = 'hostname:port' // Connect to the server on the local machine using the provided local socket $server = ':/path/to/socket'

Choose the database to USE

{boolean} mysql_select_db($database [, $link]);

Read data

{result|false} mysql_query($query [, $link]); {row|false} mysql_fetch_row($result); mysql_num_rows($result); <?php /* Connection code omitted */ $result = mysql_query("SELECT * FROM books"); if(!$result) die("Query Failed."); while($row = mysql_fetch_row($result)) { /* $row[0] now contains the first column of the current row, index 1 is the second, etc. */ } ?> // Return the current row as an associative array, where the name of each column is a key in the array. $row = mysql_fetch_assoc($result) $row['column_name'] // Return the current row with both associative and numeric indexes where each column can either be accessed by 0, 1, 2, etc., or the column name. $row = mysql_fetch_array($result) $row[0] // or $row['column_name'] // Return the current row as an object with member variables for each column in the result set. $row = mysql_fetch_object($result) $row->column_name

Closing the connection

mysql_close($link)

Errors

// Getting error info mysql_error([$link]); mysql_errno([$link]); $result = mysql_query("SELECT * FROM foo", $link); if (!$result) { $errno = mysql_errno($link); $error = mysql_error($link); echo "Database Error ($errno): $error"; }

Counting

mysql_num_rows($result); mysql_affected_rows([$link]); <?php /* Assume $link is a valid database connection and a database has been selected */ $result = mysql_query("SELECT * FROM books", $link); if (!$result) die("Query Failed."); if (mysql_num_rows($result) > 0) { mysql_query("UPDATE books SET author_id=2 WHERE book_id=1", $link); $affected = mysql_affected_rows($link); echo "Number of records modified: $affected"; } ?>

Misc

// retrieves the last ID used when inserting a row into a table with an auto-increment column. mysql_insert_id([$link]); mysql_escape_string($string)

Commands

CommandDescription
echo'prints' to html file
Example 1
strlen( str)
strpos( str, sustr)returns index of beginning of sustr in str, or FALSE
strtoupper( str)returns uppercase version of the string
string filetype ( string filename )Returns a string saying what type of file 'filename' is.
filetype return values

Examples

filetype return values

ValueMeaning
"block"block special device
"char"character special device
"dir"directory
"fifo"FIFO (named pipe)
"file"regular file
"link"symbolic link
"unknown"unknown file type