Accessing a MySQL Database


Now it's time to access our MySQL database from PHP. To do so, you need to run the MySQL server (unless you're working on an ISP or machine where it's always running). As we've already seen, that involves running mysqld in the mysql/bin directory:

 %mysqld --console 

To stop the server, you enter this command in the mysql/bin directory in another command line window:

 %mysqladmin -u root shutdown 

To actually connect to the MySQL database, you use the mysql_connect function, providing hostname, username, and password, if any:

 $connection = mysql_connect("localhost","root","")     or die ("Couldn't connect to server"); 

After you've connected, you use the $connection object with the mysql_select_db function to select a database to work with, which is "produce" for us:

 $connection = mysql_connect("localhost","root","")     or die ("Couldn't connect to server"); $db = mysql_select_db("produce", $connection)     or die ("Couldn't select database"); 

Now you're connected to the produce database. To get all the data from the fruit table, you can execute a SQL query that looks like this using the mysql_query functionnote also that the mysql_error function returns any MySQL error:

 $query = "SELECT * FROM fruit"; $result = mysql_query($query)     or die("Query failed: " . mysql_error()); 

Besides mysql_connect, mysql_select_db, and mysql_query, other functions are available for use with MySQL; here's a sampling (if you're using another database server, you can find the list of available functions for that server at www.php.net/dbname, where dbname is the name of the server):

  • mysql_affected_rows. Get the number of rows affected by the previous MySQL operation

  • mysql_change_user. Change the logged-in user

  • mysql_client_encoding. Return the name of the current character set

  • mysql_close. Close a MySQL connection

  • mysql_connect. Open a connection to a MySQL Server

  • mysql_create_db. Create a MySQL database

  • mysql_data_seek. Seek data in the database

  • mysql_db_name. Get the name of the database

  • mysql_db_query. Send a MySQL query

  • mysql_drop_db. Drop (that is, delete) a MySQL database

  • mysql_error. Return the text of the error message from the previous MySQL operation

  • 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_row. Get a result row as an enumerated array

  • mysql_field_len. Return the length of a given field

  • mysql_field_name. Get the name of the given field in a result

  • mysql_field_seek. Seek to a given field offset

  • mysql_field_table. Get name of the table the given field is in

  • mysql_field_type. Get the type of the given field in a result

  • mysql_get_server_info. Get MySQL server info

  • mysql_info. Get information about the most recent query

  • mysql_list_dbs. List databases available on a MySQL server

  • mysql_list_fields. List MySQL table fields

  • mysql_list_tables. List the tables in a MySQL database

  • mysql_num_fields. Get the number of fields in result

  • mysql_num_rows. Get the number of rows in result

  • mysql_pconnect. Open a persistent connection to a MySQL server

  • mysql_query. Send a MySQL query

  • mysql_result. Get result data

  • mysql_select_db. Select a MySQL database

  • mysql_tablename. Get the table name of a field



    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net