1 / 12

PHP AND MYSQL

PHP AND MYSQL. PHP and MySQL. PHP has functions that allow access to MySQL Databases Access is very easy 1. Connect 2. Select Database 3. Query 4. Close connection. 1. Connect. $link = mysql_connect('mysql_server', 'mysql_user',

hmarie
Download Presentation

PHP AND MYSQL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP AND MYSQL

  2. PHP and MySQL • PHP has functions that allow access to MySQL Databases • Access is very easy • 1. Connect • 2. Select Database • 3. Query • 4. Close connection

  3. 1. Connect $link = mysql_connect('mysql_server', 'mysql_user', 'mysql_password');

  4. 2. Select Database $link = mysql_connect('mysql_server', 'mysql_user', 'mysql_password'); $db_selected = mysql_select_db('foo', $link);

  5. 3. Query $link = mysql_connect('mysql_server', 'mysql_user', 'mysql_password'); $db_selected = mysql_select_db('foo', $link); $result = mysql_query("DELETE FROM table;", $link);

  6. 4. Close $link = mysql_connect('mysql_server', 'mysql_user', 'mysql_password'); $db_selected = mysql_select_db('foo', $link); $result = mysql_query("DELETE FROM table;", $link); mysql_close($link);

  7. mysql_query • INSERT, UPDATE, DELETE, DROP • Returns true or false • SELECT • Returns resource on success, false on error • The returned resource should be passed to mysql_fetch_array()

  8. Retrieving Table $link = mysql_connect('mysql_server', 'mysql_user', 'mysql_password'); $db_selected = mysql_select_db('foo', $link); $result = mysql_query("SELECT * FROM table;", $link); while ($row = mysql_fetch_array($result)) { print($row[0]); print($row[1]); } mysql_close($link);

  9. Retrieving Table $link = mysql_connect('mysql_server', 'mysql_user', 'mysql_password'); $db_selected = mysql_select_db('foo', $link); $result = mysql_query("SELECT * FROM table;", $link); while ($row = mysql_fetch_array($result)) { print($row["id"]); print($row["name"]); } mysql_close($link);

  10. Error Handling • Every function in the previous code examples could fail • Connection can fail, sql query can fail etc. • Usually you exit the script when DB fails. • With exit($status) – function, you can stop the execution of the script.

  11. Example of Error Handling 1 $link = mysql_connect('mysql_server', 'mysql_user', 'mysql_password'); if( ! $link ) { exit("Error connecting to database"); }

  12. Example of Error Handling 2 $link = mysql_connect('mysql_server', 'mysql_user', 'mysql_password') or exit("Error connecting to database");

More Related