[ Pobierz całość w formacie PDF ] .When there are nomore rows to be had, it returns FALSE.As you saw in Chapter 5, this is very help-ful for looping through all rows returned by a query.$result = mysql_query( select * from my_table ) ordie ( mysql_error() );3537-4 ch06.f.qc 12/15/00 15:22 Page 114114 Part II: Working with PHPwhile($row = mysql_fetch_array($result)){//process row}Finally, there are occasions where a function will return nothing.This will becommon in functions that perform a specific action, like closing a connection to adatabase or the file system.Function DocumentationAs we say repeatedly throughout this book, the PHP online manual is your friend.The documentation team is amazing, and we really believe the quality of the onlinemanual is one of the reasons for the success of the language.As there is no way wecan cover every PHP function in this book, you will need to consult the manual.For that reason, we want to take a minute to go over how the functions are pre-sented in the manual.A typical manual reference will look something like this:int mysql_affected_rows ([int link_identifier])This function returns the number of rows affected by an update, insert, or deletequery.Looking at this, you can see that the first portion (int) indicates the variabletype that will be returned.This could be any of the variable types or void (meaningthat the function will return nothing).Then within the parentheses there will be alist of arguments.The type of argument is listed as well as what it represents.Notethat optional arguments are placed in brackets.So above, the function requires noarguments but has one optional argument: the connection identifier grabbed frommysql_connect().In a case like phpinfo(), you will see that the argument listis void.In the preceding example, if you pass an argument, it better be an integer.If youwere to give it a string or an array, you will get an error.Important PHP 4 FunctionsIn this section, we will attempt to break down PHP 4 functions into logical group-ings.Along the way we will cover every function used in the applications presentedin this book.3537-4 ch06.f.qc 12/15/00 15:22 Page 115Chapter 6: PHP s Built-in Functions 115MySQL APIThere are a total of 33 MySQL functions available in PHP.Only 17 of these are usedin the applications in this book.You may find uses for some of the other MySQLfunctions in your applications, but you probably won t use all of them.For the sakeof this listing, I ll break the functions into the set you might use the most, and thenthe ones that you re less likely to use extensively.FREQUENTLY USED MYSQL FUNCTIONSYou will probably end up using the following functions frequently.You may wantto dog-ear this page.MYSQL_CONNECT() You can t do anything with MySQL until you make the con-nection using the following function.int mysql_connect(str host, str username, str password)Most often you will be connecting to MySQL on localhost using a username andpassword assigned to you, the Web developer.The integer that this function returnsto you is a connection identifier.You may need to track the connection identifierand use it with the mysql_db_select() function.It will typically look somethinglike this:$conn = mysql_connect( localhost , username , password ) ordie ( Could Not Connect to Database );If MySQL is not installed on a standard port or if the mysql socket is notlocated in /tmp/mysql.sock, you can specify the port of socket location inthe host string.For example:mysql_connect( localhost:/usr/local/mysql.sock , username , password );Or, if the MySQL database in sitting on another machine, you can access itwith the followingmysql_connect( mymachine.mydomain.com , username , password );You can also specify host, username, and password in the php.ini file.ThatTipway you could leave one or more of these arguments empty.3537-4 ch06.f.qc 12/15/00 15:22 Page 116116 Part II: Working with PHPMYSQL_PCONNECT() The mysql_pconnect() function works exactly like mysql_connect() but with one important difference: The link to MySQL will not closewhen the script finishes running.int mysql_pconnect(str host, str username, str password)When you use this function the connection remains open, and additional calls tomysql_pconnect() will attempt to use these open connections when they run.Thiscould make your scripts quite a bit faster.It is interesting to note what happens when mysql_pconnect() is run.The firsttime the script is run, PHP will ask for a connection, and MySQL will open a connec-tion to the database.When that script finishes, the connection remains available.Thenext time a PHP page is requested, PHP will ask for a connection that is already open.If MySQL has one available, it will grant PHP the open connection.If there are noopen connections available, a new connection will be opened.Establishing a connection with the MySQL database will be about the slowestfunction in your scripts.If PHP can use a connection that has already been opened,there will be far less overhead in the application.In order for mysql_pconnect() to work, set the following lines in your php
[ Pobierz całość w formacie PDF ] zanotowane.pldoc.pisz.plpdf.pisz.plmikr.xlx.pl
|