your-db
database and the books
table and list the book titles, each of which contains a substring $_POST['title']
entered from the Web.
If the substring $_POST['title']
is empty, then list all titles.
Everything printed from PHP is displayed on the browser.
DigiCertGlobalRootCA.crt.pem
can be found from here.
~/public_html/course/515/3/ListTitles.php |
<html><body> <?php if ( $_POST['act'] == "List the titles" ) { /************************************************************ This program shows how to list the book titles from the books table. To use this program, you need to create a table books by using the following command: mysql> create table books ( -> title char(128), -> ISBN char(20) primary key, -> price real ); Query OK, 0 rows affected (0.07 sec) *************************************************************/ $host = "undcemmysql.mysql.database.azure.com"; $username = "your-id"; $password = "your-password"; $database = "your-db"; // Initializing MySQLi $conn = mysqli_init( ); // Creating an SSL connection mysqli_ssl_set( $conn, NULL, NULL, "DigiCertGlobalRootCA.crt.pem", NULL, NULL ); // Opening a new connection to the MySQL server mysqli_real_connect( $conn, $host, $username, $password, $database, 3306 ); // Connect to the database. if ( mysqli_connect_errno( ) ) die( 'Failed to connect to MySQL: ' . mysqli_connect_error( ) ); // Compose the query. $query = "select title from books where "; $query = $query . "title like '%" . $_POST['title'] . "%'"; // Execute the query. $result = $conn->query( $query ); $row = $result->fetch_assoc( ); if ( mysqli_num_rows( $result ) > 0 ) // Print the results row by row. do echo $row['title']; while( $row = $result->fetch_assoc( ) ); // Close the connection. $conn->close( ); } elseif ( $_POST["act"] == "Help" ) { echo "No help at this moment."; } else { echo "No such option: " . $_POST["act"]; } ?> </body></html> |
βIt is wise to direct your anger towards problems — not people; to focus your energies on answers — not excuses.β β William Arthur Ward |