Some server-side reference notes
W3Schools SQL tutorial
SQL data types
Connecting to a database
Querying the database
Using PHP to connect to a database:
require 'DB.php'; //uses the PEAR DB package
$db = DB::connect('[programtype]://[username]:[password]@[hostname]/[databasename]');
if(DB::isError($db)) { die("Can't connect: ".$db->getMessage()); } //error message
The things in the second line in brackets need to be replaced with the actual values. Note that there are no square brackets in the actual call, they're just there to mark off which bits need replacing.
[programtype] varies depending on what type of program your database server is running. Here is a list of possible values (blatently stolen out of Learning PHP5 page 117-118, but shh, don't tell anyone):
[username] and [password] are pretty self explanitory.
The [hostname] depends on where your server is, so if it's on the same machine as the server you're connecting from you might use localhost, or if it's elsewhere you can use the IP address or domain name.
[databasename] is the name of the particular database you're trying to access. Since it is possible to have multiple databases on the same host, you have to specify which database exactly it is that you want to connect to. Every database must have a unique name.
The error message part is useful especially in testing, telling you if you've misstyped the password or are trying to connect to something that doesn't exist. I'm not sure I'd allow such an explicit error in a production-stage application, maybe just a general error that the database appears to be down, try again later rather than $db->getMessage(). When an error is encountered, no further php scripts are run; the program exits and prints an error message.
Sending SQL queries to the database:
function queryError ($error_object) {
}
$db->setErrorHandling(PEAR_ERROR_CALLBACK,'queryError');
$q = $db->query("[SQL query]");
[SQL query] must be replaced with any valid SQL statement.
setErrorHandling creates a default error behavior for every SQL query. isError can also be used like it was with the database connection, but it needs to be used for every query sent, so setErrorHandling is much more efficient if you want the same behavior for every failed SQL statement.
The second argument of setErrorHandling is a custom function that includes statements you want to be executed when an error occurs, e.g. printing an error message to the screen. The first causes that function to be called and then creates the standard php "die" behavior for ending all scripts when an error is encountered.
SQL data types
Connecting to a database
Querying the database
Using PHP to connect to a database:
require 'DB.php'; //uses the PEAR DB package
$db = DB::connect('[programtype]://[username]:[password]@[hostname]/[databasename]');
if(DB::isError($db)) { die("Can't connect: ".$db->getMessage()); } //error message
The things in the second line in brackets need to be replaced with the actual values. Note that there are no square brackets in the actual call, they're just there to mark off which bits need replacing.
[programtype] varies depending on what type of program your database server is running. Here is a list of possible values (blatently stolen out of Learning PHP5 page 117-118, but shh, don't tell anyone):
| [programtype] value | database program |
| dbase | dBase |
| fbsql | FrontBase |
| ibase | InterBase |
| ifx | Informix |
| msql | MiniSQL |
| mssql | Microsoft SQL Server |
| mysql | MySQL (versions <= 4.0) |
| mysqli | MySQL (versions >= 4.1.2) |
| oci8 | Oracle (versions 7,8,9) |
| odbc | ODBC |
| sqlite | SQLite |
| sybase | Sybase |
[username] and [password] are pretty self explanitory.
The [hostname] depends on where your server is, so if it's on the same machine as the server you're connecting from you might use localhost, or if it's elsewhere you can use the IP address or domain name.
[databasename] is the name of the particular database you're trying to access. Since it is possible to have multiple databases on the same host, you have to specify which database exactly it is that you want to connect to. Every database must have a unique name.
The error message part is useful especially in testing, telling you if you've misstyped the password or are trying to connect to something that doesn't exist. I'm not sure I'd allow such an explicit error in a production-stage application, maybe just a general error that the database appears to be down, try again later rather than $db->getMessage(). When an error is encountered, no further php scripts are run; the program exits and prints an error message.
Sending SQL queries to the database:
function queryError ($error_object) {
//stuff you want to happen when an error occurs
//eg printing the error to the screen for debugging:
print "Query error: " . $error_object->getDebugInfo();
//or sending it to an error log (where visitors can't see it):
error_log($error_object->getDebugInfo());
}
$db->setErrorHandling(PEAR_ERROR_CALLBACK,'queryError');
$q = $db->query("[SQL query]");
[SQL query] must be replaced with any valid SQL statement.
setErrorHandling creates a default error behavior for every SQL query. isError can also be used like it was with the database connection, but it needs to be used for every query sent, so setErrorHandling is much more efficient if you want the same behavior for every failed SQL statement.
The second argument of setErrorHandling is a custom function that includes statements you want to be executed when an error occurs, e.g. printing an error message to the screen. The first causes that function to be called and then creates the standard php "die" behavior for ending all scripts when an error is encountered.


0 Comments:
Post a Comment
Links to this post:
Create a Link
<< Home