bidvertiser

Wednesday, October 22, 2008

Querying data from a table code

// Make a MySQL Connection
mysql_connect("localhost", "admin", "1admin") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());

// Retrieve all the data from the "example" table
$result = mysql_query("SELECT * FROM example")
or die(mysql_error());

// store the record of the "example" table into $row
$row = mysql_fetch_array( $result );
// Print out the contents of the entry

echo "Name: ".$row['name'];
echo " Age: ".$row['age'];

?>

Inserting data into a table code

mysql_connect("localhost", "admin", "1admin") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
// Insert a row of information into the table "example"
mysql_query("INSERT INTO example (name, age)
VALUES('Timmy Mellowman', '23' ) ") or die(mysql_error());
mysql_query("INSERT INTO example (name, age)
VALUES('Sandy Smith', '21' ) ") or die(mysql_error());
mysql_query("INSERT INTO example (name, age)
VALUES('Bobby Wallace', '15' ) ") or die(mysql_error());
echo "Data Inserted!";
?>

MySQL Function Reference example code

if (!$link)
{ die('Could not connect: ' . mysql_error());
} mysql_select_db('mydb'); /* Update records */
mysql_query("UPDATE mytable SET used=1 WHERE id < 10");
printf ("Updated records: %d\n", mysql_affected_rows());
mysql_query("COMMIT");
?>

MySQL Function Reference example code

if (!$link)
{
die('Could not connect: ' . mysql_error());
} mysql_select_db('mydb'); /* this should return the correct numbers of deleted records */ mysql_query('DELETE FROM mytable WHERE id < 10');
printf("Records deleted: %d\n", mysql_affected_rows());
/* with a where clause that is never true, it should return 0 */ mysql_query('DELETE FROM mytable WHERE 0');
printf("Records deleted: %d\n", mysql_affected_rows());
?>

MySQL Function Reference contd..

int mysql_affected_rows ([ resource $link_identifier ] )
Gets the number of affected rows by the last INSERT, UPDATE, REPLACE or DELETE query associated with link_identifier.
Return Values
Returns the number of affected rows on success, and -1 if the last query failed.
If the last query was a DELETE query with no WHERE clause, all of the records will have been deleted from the table but this function will return zero with MySQL versions prior to 4.1.2.
When using UPDATE, MySQL will not update columns where the new value is the same as the old value. This creates the possibility that mysql_affected_rows() may not actually equal the number of rows matched, only the number of rows that were literally affected by the query.
The REPLACE statement first deletes the record with the same primary key and then inserts the new record. This function returns the number of deleted records plus the number of inserted records.

MySQL Function Reference example code

mysql_select_db("database", $link);
$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);
echo "$num_rows Rows\n";
?>

$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
/* returns 2 because id,email === two fields */
echo mysql_num_fields($result);
?>

MySQL Function Reference contd..

int mysql_num_rows ( resource $result )
Retrieves the number of rows from a result set. This command is only valid for statements like SELECT or SHOW that return an actual result set. To retrieve the number of rows affected by a INSERT, UPDATE, REPLACE or DELETE query, use mysql_affected_rows().
$result.
The result resource that is being evaluated.
Return value.
The number of rows in a result set on success, or FALSE on failure.

int mysql_num_fields ( resource $result )
Retrieves the number of fields from a query.
Returns the number of fields in the result set resource on success, or FALSE on failure.

MySQL Function Reference contd..

resource mysql_db_query ( string $database , string $query [, resource $link_id] ) .
Selects a database, and executes a query on it.
$database
The name of the database that will be selected.
$query
The MySQL query.
Return value.
Returns a positive MySQL result resource to the query result, or FALSE on error.

MySQL Function Reference example code

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) 
{ die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';

MySQL Function Reference contd..

string mysql_error ([ resource $link_identifier ] )
Returns the error text from the last MySQL function. Errors coming back from the MySQL database backend no longer issue warnings. Instead, use mysql_error() to retrieve the error text. Note that this function only returns the error text from the most recently executed MySQL function (not including mysql_error() and mysql_errno()), so if you want to use it, make sure you check the value before calling another MySQL function.
$link_identifier.
The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If by chance no connection is found or established, an E_WARNING level error is generated.
Return value.
Returns the error text from the last MySQL function, or '' (empty string) if no error occurred.

int mysql_errno ([ resource $link_identifier ] )
Returns the error number from the last MySQL function or 0 (zero) if no error occurred.
bool mysql_close ([ resource $link_identifier ] )
closes the non-persistent connection to the MySQL server that's associated with the specified link identifier. If link_identifier isn't specified, the last opened link is used.
Return value.
Returns TRUE on success or FALSE on failure.

mysql_connect example code

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) 
{ die('Could not connect');
}
echo 'Connected successfully';

MySQL Function Reference

resource mysql_connect ([ string $server [, string $username [, string $password [,
bool $new_link [, int $client_flags ]]]]] )
$server.
The MySQL server. It can also include a port number. e.g. "hostname:port" or a path to a local socket e.g. ":/path/to/socket" for the localhost.
$username.
The username.
$password.
The password.
$new_link.
If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. The new_link parameter modifies this behavior and makes mysql_connect() always open a new link, even if mysql_connect() was called before with the same parameters.
$client_flags.
The client_flags parameter can be a combination of the following constants: 128 (enable LOAD DATA LOCAL handling), MYSQL_CLIENT_SSL, MYSQL_CLIENT_COMPRESS, MYSQL_CLIENT_IGNORE_SPACE or MYSQL_CLIENT_INTERACTIVE.
Return Value.
Returns a MySQL link identifier on success, or FALSE on failure.

MySQL Functions

mysql_affected_rows — Get number of affected rows in previous MySQL operation
mysql_change_user — Change logged in user of the active connection
mysql_client_encoding — Returns the name of the character set
mysql_close — Close MySQL connection
mysql_connect — Open a connection to a MySQL Server
mysql_create_db — Create a MySQL database
mysql_data_seek — Move internal result pointer
mysql_db_name — Get result data
mysql_db_query — Send a MySQL query
mysql_drop_db — Drop (delete) a MySQL database
mysql_errno — Returns the numerical value of the error message from previous MySQL operation
mysql_error — Returns the text of the error message from previous MySQL operation
mysql_escape_string — Escapes a string for use in a mysql_query
mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both
mysql_fetch_assoc — Fetch a result row as an associative array
mysql_fetch_field — Get column information from a result and return as an object
mysql_fetch_lengths — Get the length of each output in a result
mysql_fetch_object — Fetch a result row as an object
mysql_fetch_row — Get a result row as an enumerated array
mysql_field_flags — Get the flags associated with the specified field in a result
mysql_field_len — Returns the length of the specified field
mysql_field_name — Get the name of the specified field in a result
mysql_field_seek — Set result pointer to a specified field offset
mysql_field_table — Get name of the table the specified field is in
mysql_field_type — Get the type of the specified field in a result
mysql_free_result — Free result memory
mysql_get_client_info — Get MySQL client info
mysql_get_host_info — Get MySQL host info
mysql_get_proto_info — Get MySQL protocol info
mysql_get_server_info — Get MySQL server info
mysql_info — Get information about the most recent query
mysql_insert_id — Get the ID generated from the previous INSERT operation
mysql_list_dbs — List databases available on a MySQL server
mysql_list_fields — List MySQL table fields
mysql_list_processes — List MySQL processes
mysql_list_tables — List tables in a MySQL database
mysql_num_fields — Get number of fields in result
mysql_num_rows — Get number of rows in result
mysql_pconnect — Open a persistent connection to a MySQL server
mysql_ping — Ping a server connection or reconnect if there is no connection
mysql_query — Send a MySQL query
mysql_real_escape_string — Escapes special characters in a string for use in a SQL statement
mysql_result — Get result data
mysql_select_db — Select a MySQL database
mysql_set_charset — Sets the client character set
mysql_stat — Get current system status
mysql_tablename — Get table name of field
mysql_thread_id — Return the current thread ID
mysql_unbuffered_query — Send an SQL query to MySQL, without fetching and buffering the result rows

What is MySQL?

MySQL is currently the most popular open source database server in existence.
It is very commonly used in conjunction with PHP scripts to create powerful and dynamic server-side DB applications.