Lab2: Part4c Writing php files


Location of PHP files


#1 Create a php file called db_connect.php
<?php
/**
 * A class file to connect to database
 */
class DB_CONNECT {

    var $myconn;

  
    /**
     * Function to connect with the database
     */
    function connect() {

    define('DB_USER', "xxxxxx_mydb"); // db user
    define('DB_PASSWORD', "xxxxxx"); // db password (mention your db password here)
    define('DB_DATABASE', "xxxxx_mydb"); // database name
    define('DB_SERVER', "xxxxx.atspace.me"); // db server
// import database connection variables //require_once __DIR__ . '/db_config.php'; // Connecting to mysql database $con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD,DB_DATABASE) or die(mysqli_error($con)); $this->myconn = $con; // returning connection cursor return $this->myconn; } /** * Function to close db connection */ function close($myconn) { // closing db connection mysqli_close($myconn); } } ?>

These 4 lines contains the username and password to the atspace server,
the database name and  server location.

For my case it is:
define('DB_USER', "3467266_mydatabase"); // db user
define('DB_PASSWORD', "ILXy_igl6te{QBRg"); // db password (mention your db password here)
define('DB_DATABASE', "3467266_mydatabase"); // database name
define('DB_SERVER', "fdb24.atspace.me"); // db server





#2 List All Products  Create a php file called get_all_products.php   
<html>
<body>
 
<h1> List all Products </h1>
 <?php
 /*
 * Following code will list all the products
 */
  
 echo("<table border=1>");
 echo ("<tr> <th>  ID </th><th>  Name </th> <th>  Price </th> <th>  Description </th> </tr>");
 
    // include db connect class
    require_once __DIR__ . '/db_connect.php';
    $db= new DB_CONNECT(); // connecting to db
    $db->connect();
    
            
    // get all products from products table
    $sqlCommand="SELECT *FROM products";
    $result =mysqli_query($db->myconn, "$sqlCommand");
  
    foreach($result as $row)
    {
   
   echo("<tr>");
   echo ("<td>".  $row["pid"]. "</td> <td>". $row["name"]. "</td><td>". $row["price"]. "</td><td>". $row["description"]. "</td>");
   echo("</tr>");            
    

    }
    echo("</table>");
  
?>

</body>
</html>

</table>




Exercise 1
Modify get_all_products.php   to list price and description shown below:




No comments:

Post a Comment

Note: only a member of this blog may post a comment.