2nd Answer
2nd Answer
The Structured Query Language (SQL) is the standard programming language for accessing and manipulating information from a relational database. SQL is an ANSI and ISO standard, and is supported by almost all the relational databases. In the next section, we will present a small PHP application, which will allow you to execute SQL statements against a MySQL database.
The PHP script in this page occurs within the HTML <SELECT> element. It connects to the local MySQL database server with the username php and password php. We then call the mysql_list_dbs() function, which returns a reference to a resultset containing the names of the available databases. We iterate through this resultset, and for each entry print to the browser the string "<OPTION>" followed by the database name. This creates an <OPTION> element within the <SELECT> element for each database. When the Submit button is pressed, the chosen database will be passed to the next page, mysql_test.php, and be available through a variable $database. The text of the SQL query entered by the user will also be available, through the $query variable. The next page displays an HTML table containing the results of the query; if no rows are returned by the query, either a success message or an error message will be displayed.
<HTML> <HEAD> <TITLE> PHP SQL Code Tester </TITLE> <BODY> <!-- mysql_test.php --> <?php $user="php"; $host="localhost"; $password="php"; mysql_connect($host,$user,$password); mysql_select_db($database);
$result = stripSlashes($query) ; $result = mysql_query($query); ?> Results of query <B><?php echo($query); ?></B><HR> <?php if ($result == 0): echo("<B>Error " . mysql_errno() . ": " . mysql_error() . "</B>"); elseif (mysql_num_rows($result) == 0): echo("<B>Query executed successfully!</B>"); else:
We connect to the database server in exactly the same way as on the previous page, again using the username "php". We then specify the active database as that referenced by the $database variable passed over from query.php. Our next step, before we execute the query, is to remove any escape characters from the text of the query. We aren't going to type escape any characters when we type in the query, so why do we need to do this? Consider the SQL query:
SELECT * FROM books WHERE title="Professional PHP"
When this query is typed into the textarea, the quote marks will automatically be escaped if magic_quotes_gpc configuration directive is set true in the configuration file (php3.ini in case of PHP 3.0, php.ini in case of PHP 4.0), so the variable $query will actually contain the string:
SELECT * FROM books WHERE title=\"Professional PHP\"
To avoid this problem, we use the PHP function StripSlashes(), which removes the offending
field in the row represented by an element in the array. So our inner loop iterates through each element in this array, and prints it to the browser within <TD>...</TD> tags to create each cell of the row in our HTML table. Finally, the page also contains a form with a submit button to return the user to the previous page, so a new query can be made.