, SAMS Teach Yourself PHP4 in 24 Hours 

[ Pobierz całość w formacie PDF ]
.PHP maintains an internal pointer thatkeeps a record of your position within a found set.This moves on to the next row aseach one is accessed.You can easily get an array of the fields in each found row with mysql_fetch_row().This function requires a result identifier, returning an array containing each field inthe row.When the end of the found set is reached, mysql_fetch_row() returns false.Listing 12.5 outputs the entire domains table to the browser. 223Listing 12.5: Listing All Rows and Fields in a Table1:2:3: Listing 12.5 Listing all rows and fields in a table4:5:6:29: 22430:After we have connected to the server and selected the database, we usemysql_query() to send a SELECT statement to the database server.We store thereturned result pointer in a variable called $result.We use this to acquire thenumber of found rows as before.In the test expression of our while statement, we assign the result ofmysql_fetch_row() to the variable $a_row.Remember that an assignment operatorreturns the value of its right-hand operand, so the assignment resolves to true aslong as mysql_fetch_row() returns a positive value.Within the body of the whilestatement, we loop through the row array contained in $a_row, outputting eachelement to the browser embedded in a table cell.You can also access fields by name in one of two ways.mysql_fetch_array() returnsa numeric array, as does mysql_fetch_row() It also returns an associative array,with the names of the fields as the keys.The following fragment rewrites the whilestatement from Listing 12.5, incorporating mysql_fetch_array():print "\n";while ( $a_row = mysql_fetch_array( $result ) ){print "\n";print "$a_row[mail]$a_row[domain]\n";print "\n";}print "\n";You can also extract the fields from a row as properties of an object withmysql_fetch_object().The field names become the names of the properties.Thefollowing fragment once again rewrites the while statement from Listing 12.5, thistime incorporating mysql_fetch_object():print "\n";while ( $a_row = mysql_fetch_object( $result ) ){print "\n";print "$a_row->mail$a_row->domain\n"; 225print "\n";}print "\n";Both mysql_fetch_array() and mysql_fetch_object() make it easier for you toselectively extract information from a row.Neither of these functions takes muchlonger than mysql_fetch_row() to execute.Changing DataYou can change data using the mysql_query() function in conjunction with anUPDATE statement.Once again, a successful UPDATE statement does notnecessarily change any rows.You need to use a function to callmysql_affected_rows() to discover whether you have changed data in your table.mysql_affected_rows() optionally accepts a link identifier.If this is missing, themost recent connection is assumed.This function can be used with any SQL querythat can alter data in a table row.Listing 12.6 builds a script that allows an administrator to change any of the valuesin the domain column of our example table.Listing 12.6: Using mysql_query() to Alter Rows in a Database1:2:3: Listing 12.6 Using mysql_query()4: to alter rows in a database5:6:7:26:27:28: id\"";33: if ( isset($id) && $id == $a_row->id )34: print " SELECTED";35: print "> $a_row->domain\n";36: }37: mysql_close( $link );38: ?>39:40:41:42:43:We open a connection to the database server and select a database as normal.Wethen test for the presence of the variables $domain and $id.If these are present, webuild a SQL UPDATE query that changes the value of the domain field where the idfield contains the same value as our $id variable.We do not get an error if anonexistent id is used or if the $domain variable is the same as the current value fordomain in the relevant row.Instead, the mysql_affected_rows() simply returns 0.We print this return value (usually 1 in this example) to the browser.We print an HTML form to allow the administrator to make her changes.Note thatwe use mysql_query() once again to extract the values of the id and domain columnand incorporate them in an HTML SELECT element.The administrator will use thispop-up menu to choose which domain to change.If the administrator has alreadysubmitted the form and the id value she chose matches the value of the id field weare currently outputting, we add the string SELECTED to the OPTION element.This 227ensures that her changed value will be instantly visible to her in the menu.Getting Information About DatabasesUntil now, we have primarily looked at adding information to and extractinginformation from a database [ Pobierz całość w formacie PDF ]
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • anikol.xlx.pl