Specify the particular database in Java
6 Specify the particular database Making PDF 417 In Java Using Barcode generation for Java Control to generate, create PDF-417 2d barcode image in Java applications. Once you re connected to the server, you need to specify which database on the server will be used for your transaction I m using a database called xfd for all the examples in this book The mysql_select_db() function is used to handle this task: Printing Barcode In Java Using Barcode encoder for Java Control to generate, create barcode image in Java applications. mysql_select_db( xfd ); Bar Code Reader In Java Using Barcode decoder for Java Control to read, scan read, scan image in Java applications. Retrieving Data from a Database
Paint PDF-417 2d Barcode In C# Using Barcode generation for .NET framework Control to generate, create PDF-417 2d barcode image in .NET framework applications. Passing a query to the database
Print PDF 417 In Visual Studio .NET Using Barcode creator for VS .NET Control to generate, create PDF 417 image in .NET framework applications. The reason for connecting to a database is to retrieve data from it (or to add or modify data, but the basic approach is always the same) In any case, you need to pass instructions to the database in SQL (If you re unfamiliar with SQL, it is described in Book VI, 1) Your PHP program usually constructs an SQL statement in a string variable and then passes this value to the database For this basic example, I specify the entire query See the section Processing the input, later in this chapter, for some warnings about how to incorporate user information in data queries The showContactphp program simply asks for a list of all the values in the contact table of the xfd database The SQL query for displaying all the data in a table looks like this: Printing PDF-417 2d Barcode In Visual Basic .NET Using Barcode printer for .NET framework Control to generate, create PDF 417 image in .NET applications. SELECT * FROM contact; Making GS1 - 13 In Java Using Barcode creation for Java Control to generate, create EAN-13 image in Java applications. To use an SQL statement, package it into a string variable, like this: Create Bar Code In Java Using Barcode maker for Java Control to generate, create bar code image in Java applications. $sql = SELECT * FROM contact ; UPC-A Encoder In Java Using Barcode generation for Java Control to generate, create GTIN - 12 image in Java applications. Note that you don t need to include the semicolon inside the string variable You can call the variable anything you wish, but it s commonly called $sql SQL queries can get complex, so if the SQL requires more than one line, you may want to encase it in a heredoc (See 2 of this minibook for information on using heredocs) Pass the request to the database using the msql_query() function: Barcode Generation In Java Using Barcode encoder for Java Control to generate, create barcode image in Java applications. $result = mysql_query($sql, $conn) or die(mysql_error()); Painting EAN / UCC - 14 In Java Using Barcode printer for Java Control to generate, create GS1-128 image in Java applications. The mysql_query() function has a lot going on Here s how you put it together: Generating UPC Case Code In Java Using Barcode drawer for Java Control to generate, create ITF14 image in Java applications. 1 Create a variable to house the results
Encode GS1 128 In VS .NET Using Barcode maker for .NET framework Control to generate, create EAN 128 image in Visual Studio .NET applications. When the query is finished, it will send results back to the program The $result variable will hold that data: Draw Code39 In Visual Basic .NET Using Barcode generator for Visual Studio .NET Control to generate, create Code 39 image in .NET applications. $result = mysql_query($sql, $conn) or die(mysql_error()); Printing Barcode In VB.NET Using Barcode printer for .NET Control to generate, create bar code image in .NET applications. 2 Invoke mysql_query() ANSI/AIM Code 39 Drawer In VS .NET Using Barcode drawer for .NET framework Control to generate, create ANSI/AIM Code 39 image in Visual Studio .NET applications. This function passes the query to the database: EAN13 Reader In VS .NET Using Barcode scanner for .NET framework Control to read, scan read, scan image in Visual Studio .NET applications. $result = mysql_query($sql, $conn) or die(mysql_error()); Code128 Encoder In C# Using Barcode creation for .NET Control to generate, create Code 128 Code Set B image in .NET framework applications. 3 Send the query to the database
Code 3 Of 9 Reader In Visual Studio .NET Using Barcode decoder for .NET Control to read, scan read, scan image in Visual Studio .NET applications. The first parameter is the query Normally, this is stored in a variable called $sql: Decoding DataMatrix In Visual Studio .NET Using Barcode reader for .NET Control to read, scan read, scan image in .NET applications. $result = mysql_query($sql, $conn) or die(mysql_error()); Retrieving Data from a Database
Book V 8
4 Specify the connection
The second parameter is the connection object created when you ran mysql_connect() If you leave out the connection object, PHP uses the last MySQL connection that was created: $result = mysql_query($sql, $conn) or die(mysql_error()); Connecting to a MySQL Database
5 Handle errors
If there s an error in your SQL request, MySQL will send back an error message Prepare for this with the or die() clause (just like you used for mysql_connect()): $result = mysql_query($sql, $conn) or die(mysql_error()); 6 Return the MySQL error if there was a problem
If something went wrong in the SQL code, have your program reply with the MySQL error so you ll at least know what went wrong: $result = mysql_query($sql, $conn) or die(mysql_error()); Processing the results
The results of an SQL query are usually data tables, which are a complex data structure The next step when you work with a database is to get all the appropriate information from the $request object and display it in a XHTML output for the user to understand This process is a little involved because SQL results are normally composed of two-dimensional data A query result typically consists of multiple records (information about a specific entity sometimes also called a row) Each record consists of a number of fields (specific data about the current record) I m tossing a bunch of database terms at you here Databases deserve (and have) a minibook of their own If nothing in this chapter makes sense to you, build your own copy of the contact database following the instructions in Book VI and then come back here to have your program present that data through your Web site The $request variable has a lot of data packed into it You get that data out by using a pair of nested loops:
|
|
|
|
| ALL RIGHTS RESERVED. Business Refinery (c) 2006 - 2010. | Terms of Use | Privacy Policy |