RSS


[ Pobierz całość w formacie PDF ]
.Listing 12.2: Adding a Row to a Table1:2:3: Listing 12.2 Adding a row to a table4:5:6:22:23:Notice that we did not insert a value for the id column.This field will auto-increment.Of course, every time we reload the script in Listing 12.2, the same data is added toa new row.Listing 12.3 creates a script that will enter user input into our database.Listing 12.3: Adding User Input to a Database1:2:3: Listing 12.3 Adding user input to a database4:5:6: 22062:63:To keep the example brief, we have left out one important process in Listing 12.3.We are trusting our users.We should in fact check any kind of user input.We dealwith the string functions that help you test user input in Hour 17, "Working withStrings."We check for the variables $domain, $sex, and $mail.If they exist, we can be fairlycertain that the user has submitted data, and we call the add_to_database()function.The add_to_database() function requires four arguments: the $domain, $sex, and$mail variables submitted by the user, and a string variable called $dberror.Wepopulate this last argument with any error strings we encounter.For this reason, weaccept $dberror as a reference to a variable.Any changes made to this string withinthe function will change the original argument rather than a copy.We attempt to open a connection to the MySQL server.If this fails, we assign anerror string to $dberror and end the execution of the function by returning false.Weselect the database that contains the domains table and build a SQL query to insertthe user-submitted values.We pass this to mysql_query(), which makes the queryfor us.If either mysql_select_db() or mysql_query() fail, we assign the valuereturned by mysql_error() to $dberror and return false.Assuming that all went well,the function returns true.Back in the calling code, we can test the return value from add_to_database().Ifthe function returns true, we can be sure that we have added to the database andthank the user.Otherwise, we write an error message to the browser.We know thatthe $dberror variable that we passed to add_to_database() will now contain usefulinformation, so we include it in our error message.If our initial if statement fails to find $domain, $sex, or $mail variables, we canassume that no data has been submitted and call another user-defined function,write_form(), which outputs an HTML form to the browser.Acquiring the Value of an Automatically IncrementedFieldIn our previous examples, we have added data to our database without worryingabout the id column, which automatically increments as data is inserted.If we needthe value of this field for a record at a later date, we can always extract it with a SQLquery.What if we need the value straight away, though? It would be wasteful to 221look it up.Luckily, PHP provides mysql_insert_id(), a function that returns the valueof an auto-incremented key field after a SQL INSERT statement has been performed.mysql_insert_id() optionally accepts a link identifier as an argument.With noarguments, it works with the most recent link established.So, if we want to tell a user the number we have allocated to her order, we could callmysql_insert_id() directly after adding the user's data to our database.$query = "INSERT INTO domains ( domain, sex, mail ) values( '$domain', '$sex','$mail' )";mysql_query( $query, $link );$id = mysql_insert_id();print "Thank you.Your transaction number is $id.Please quote it in anyqueries.";Accessing InformationNow that we can add information to a database, we need to look at strategies forretrieving the information that it contains.As you might guess, you can usemysql_query() to make a SELECT query.How do you use this to look at the returnedrows, though? When you perform a successful SELECT query, mysql_query()returns a result identifier.You can pass this identifier to other functions to accessand gain information about a resultset.Finding the Number of Rows Found by a QueryYou can find the number of rows returned as a result of a SELECT query using themysql_num_rows() function.mysql_num_rows() requires a result identifier andreturns a count of the rows in the set.Listing 12.4 uses a SQL SELECT statement torequest all rows in the domains table and then uses mysql_num_rows() todetermine the table's size.Listing 12.4: Finding the Number of Rows Returned by a SELECT Statementwith mysql_num_rows()1:2:3: Listing 12.4 Using mysql_num_rows() 2224:5:6:20:21:The mysql_query() function returns a result identifier.We then pass this tomysql_num_rows(), which returns the total number of rows found [ Pobierz całość w formacie PDF ]
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • wblaskucienia.xlx.pl