MYSQL_AFFECTED_ROWS
//connect to server as root user, no password
$conn = mysql_pconnect("localhost", "root", "");
//select the 'test' database
mysql_select_db("test", $conn);
//update some invoices
$Query = "UPDATE inventory " .
"SET Active = 'Y' " .
"WHERE ID < 100 ";
$result = mysql_query($Query, $conn);
//let user know how many rows were updated
$AffectedRows = mysql_affected_rows($conn);
print("$AffectedRows rows updated.<BR>\n");
MYSQL_CHANGE_USER
//connect to server as root user, no password
$conn = mysql_pconnect("localhost", "root", "");
//select the 'mydb' database
mysql_select_db("mydb", $conn);
//switch to admin user
mysql_change_user("admin", "secret", "mydb", $conn);
MYSQL_CLOSE
//connection
$conn = mysql_pconnect("localhost", "root", "");
//close connection
mysql_close($conn);
MYSQL_CONNECT
//establish connection
if(!($conn = mysql_connect("localhost:3606", "root", "")))
{
print("mysql_connect failed!<BR>\n");
}
//select database
if(!(mysql_select_db("test", $conn)))
{
print("mysql_select_db failed!<BR>\n");
print(mysql_errno() . ": ");
print(mysql_error() . "<BR>\n");
}
MYSQL_CREATE_DB
//connection
$conn = mysql_connect("localhost", "root", "password");
//create database
mysql_create_db("mydb", $conn);
MYSQL_DATA_SEEK
//connect to server as root user, no password
$conn = mysql_pconnect("localhost", "root", "");
//select the 'mydb' database
mysql_select_db("mydb", $conn);
//get states from tax table
$Query = "SELECT State " .
"FROM tax ";
$result = mysql_query($Query, $conn);
//jump to fifth row
mysql_data_seek($result, 4);
//get row
$row = mysql_fetch_row($result);
//print state name
print($row[0]);
MYSQL_DB_QUERY
//connect to server as root user, no password
$conn = mysql_pconnect("localhost", "root", "");
//truncate session table
$Query = "DELETE FROM session ";
$result = mysql_db_query("mydb", $Query, $conn);
MYSQL_DROP_DB
//connection
$conn = mysql_connect("localhost", "admin", "secret");
//drop garbage database
if(mysql_drop_db("garbage", $conn))
{
print("Database dropped.<BR>");
}
else
{
print("Database drop failed!<BR>");
}
MYSQL_ERRNO
//connect to server as root user, no password
$conn = mysql_pconnect("localhost", "root", "");
//select the 'mydb' database
mysql_select_db("mydb", $conn);
//try to execute a bad query (missing fields)
$Query = "SELECT FROM tax ";
if(!($result = mysql_query($Query, $conn)))
{
// get error and error number
$errno = mysql_errno($conn);
$error = mysql_error($conn);
print("ERROR $errno: $error<BR>\n");
}
MYSQL_FETCH_ARRAY
//connect to server as root user, no password
$conn = mysql_pconnect("localhost", "root", "");
//select the 'mydb' database
mysql_select_db("mydb", $conn);
//get rates from tax table
$Query = "SELECT State, Rate " .
"FROM tax ";
$result = mysql_query($Query, $conn);
// get each row
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
// print state and rate
print("{$row["State"]} = {$row["Rate"]}<BR>\n");
}
MYSQL_FETCH_FIELD
//connect to server as root user, no password
$conn = mysql_pconnect("localhost", "root", "");
//select the 'mydb' database
mysql_select_db("mydb", $conn);
//get everything from address table
$Query = "SELECT * " .
"FROM address a, user u " .
"WHERE u.Address = a.ID ";
$result = mysql_query($Query, $conn);
// get description of each field
while($Field = mysql_fetch_field($result))
{
print("$Field->table, $Field->name, $Field->type<BR>\n");
}
MYSQL_FETCH_LENTHS
//connect to server as root user, no password
$conn = mysql_pconnect("localhost", "root", "");
//select the 'mydb' database
mysql_select_db("mydb", $conn);
//get everything from address table
$Query = "SELECT * " .
"FROM address ";
$result = mysql_query($Query, $conn);
//get field lengths
$lengths = mysql_fetch_lengths($result);
//print length of the third column
print($lengths[2]);
MYSQL_FETCH_OBJECT
//connect to server as root user, no password
$conn = mysql_pconnect("localhost", "root", "");
//select the 'mydb' database
mysql_select_db("mydb", $conn);
//get unique cities from address table
$Query = "SELECT City, StateProv " .
"FROM address ";
$result = mysql_query($Query, $conn);
// get each row
while($row = mysql_fetch_object($result))
{
// print name
print("$row->City, $row->StateProv<BR>\n");
}
MYSQL_FETCH_ROW
//connect to server as root user, no password
$conn = mysql_pconnect("localhost", "root", "");
//select the 'mydb' database
mysql_select_db("mydb", $conn);
//get unique cities from address table
$Query = "SELECT DISTINCT City, StateProv " .
"FROM address ";
$result = mysql_query($Query, $conn);
//get each row
while($row = mysql_fetch_row($result))
{
// print city, state
print("$row[0], $row[1]<BR>\n");
}
MYSQL_FIELD_SEEK
//connect to server as root user, no password
$conn = mysql_pconnect("localhost", "root", "");
//select the 'mydb' database
mysql_select_db("mydb", $conn);
// get everything from address table
$Query = "SELECT * " .
"FROM address ";
$result = mysql_query($Query, $conn);
//skip to second field
mysql_field_seek($result, 1);
//get description of each field
while($Field = mysql_fetch_field($result))
{
print("$Field->table, $Field->name, $Field->type<BR>\n");
}
MYSQL_FIELD_TABLE
//connect to server as root user, no password
$conn = mysql_pconnect("localhost", "root", "");
//select the 'mydb' database
mysql_select_db("mydb", $conn);
//get everything from user table
//get everything from address table
$Query = "SELECT * " .
"FROM address a, user u " .
"WHERE u.Address = a.ID ";
$result = mysql_query($Query, $conn);
$Fields = mysql_num_fields($result);
for($i = 0; $i < $Fields; $i++)
{
print(mysql_field_table($result, $i) . "<BR>\n");
}
MYSQL_FREE_RESULT
//connect to server as root user, no password
$conn = mysql_pconnect("localhost", "root", "");
//select the 'mydb' database
mysql_select_db("mydb", $conn);
//get everything from user table
$Query = "SELECT * " .
"FROM user ";
$result = mysql_query($Query, $conn);
//free result set
mysql_free_result($result);
MYSQL_INSERT_ID
//connect to server as root user, no password
$conn = mysql_pconnect("localhost", "root", "");
//select the 'mydb' database
mysql_select_db("mydb", $conn);
//insert a row
$Query = "INSERT INTO user (Login, Password) " .
"VALUES('leon', 'secret') ";
$result = mysql_query($Query, $conn);
//get id
print("ID is " . mysql_insert_id($conn));
MYSQL_LIST_DBS
//connect to server as root user, no password
$conn = mysql_pconnect("localhost", "root", "");
//get list of databases
$result = mysql_list_dbs($conn);
//get each row
while($row = mysql_fetch_row($result))
{
// print name
print($row[0] . "<BR>\n");
}
MYSQL_LIST_FIELDS
//connect to server
$conn = mysql_pconnect("localhost", "root", "");
//get list of fields
$result = mysql_list_fields("mydb", "invoice", $conn);
//start HTML table
print("<TABLE>\n");
print("<TR>\n");
print("<TH>Name</TH>\n");
print("<TH>Type</TH>\n");
print("<TH>Length</TH>\n");
print("<TH>Flags</TH>\n");
print("</TR>\n");
//loop over each field
for($i = 0; $i < mysql_num_fields($result); $i++)
{
print("<TR>\n");
print("<TD>" . mysql_field_name($result, $i) . "</TD>\n");
print("<TD>" . mysql_field_type($result, $i) . "</TD>\n");
print("<TD>" . mysql_field_len($result, $i) . "</TD>\n");
print("<TD>" . mysql_field_flags($result, $i) . "</TD>\n");
print("</TR>\n");
}
//close HTML table
print("</TABLE>\n");
MYSQL_LIST_TABLES
//connect to server as root user, no password
$conn = mysql_pconnect("localhost", "root", "");
//get list of tables
$result = mysql_list_tables("mydb", $conn);
//get each row
while($row = mysql_fetch_row($result))
{
//print name
print($row[0] . "<BR>\n");
}
MYSQL_PCONNECT
//persistent connection
$conn = mysql_pconnect("localhost", "root", "");
MYSQL_RESULT
//connect to server as root user, no password
$conn = mysql_connect("localhost", "root", "");
//select the 'mydb' database
mysql_select_db("mydb", $conn);
// get everything from customer table
$Query = "SELECT * FROM " .
"user " .
"WHERE user.login like 'A%' ";
$result = mysql_query($Query, $conn);
// get number of rows
$rows = mysql_num_rows($result);
for($i = 0; $i < $rows; $i++)
{
$name = mysql_result($result, $i, "user.login");
print("$name<BR>\n");
}
>> Comments/FeedBack
|