Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.45 KB | None | 0 0
  1. <?php
  2. /*
  3. Database structure
  4.  
  5. Table products:
  6.     Columns:
  7.         id tinyint unsigned auto_increment
  8.         name varchar(255)
  9.         img varchar(255)
  10.         desc text
  11.     Attributes:
  12.         Primary Key: id
  13. */
  14. // Configuration
  15. $db_engine = 'mysql'; // Database Engine
  16. $db_name = 'mydb'; // Database name
  17. $db_host = 'localhost'; // Database host
  18. $db_user = 'root'; // Database username
  19. $db_pass = ''; // Database password
  20.  
  21. // Setup the Database object
  22. // $dbo = new PDO($db_engine.':dbname='.$db_name.';host='.$db_host', $db_user, $db_pass);
  23. mysql_connect($db_host, $db_user, $db_pass);
  24. mysql_select_db($db_name);
  25.  
  26. // For security reasons, release the password now.
  27. unset($db_pass);
  28.  
  29. // Let's grab and check the request data for what we do.
  30. if (!empty($_GET['pid'])) {
  31.     // Show Products.
  32.    
  33.     // Prepare the statement
  34.     //$sh = $dbo->prepare('SELECT name, img, desc FROM products WHERE id = ?');
  35.     $result = mysql_query('SELECT name, img, desc FROM products WHERE id = '.mysql_real_escape_string($_GET['pid']));
  36.    
  37.     // Execute the statement with the required parameters
  38.     //$sh->execute(array($_GET['pid']));
  39.    
  40.     // Fetch the results, if there are any.
  41.     //if ($sh->rowCount() == 0) {
  42.    
  43.         // We have results!
  44.        
  45.         // Get the results in a nice, easy to use array
  46.         //$results = $sh->fetch(PDO::FETCH_ASSOC);
  47.     //} else {
  48.     if (mysql_num_rows($result) != 1) {
  49.         // Either we have no results, or somehow we got too many... Either way, a problem.
  50.         die("Error: Product not found");
  51.     }
  52.     $results = mysql_fetch_assoc($results);
  53.    
  54.     // Time to output!
  55.     echo '<h1>'.$results[0]['name'].'</h1>\n<img src="'.$results[0]['img'].'" alt="Product Picture" />\n<p>'.$results[0]['desc'].'</p>';
  56. } else {
  57.     // Show list of items
  58.    
  59.     // Prepare the statement
  60.     //$sh = $dbo->prepare('SELECT id, name FROM products');
  61.     $results = mysql_query('SELECT id, name FROM products');
  62.    
  63.     // Execute the statement
  64.     //$sh->execute();
  65.    
  66.     // A neat PDO trick, it'll automagically apply this function to the result set.
  67.     //function listify($id, $name) {
  68.         //return '<li><a href="index.php?pid='.$id.'">'.$name.'</a></li>';
  69.     //}
  70.    
  71.     // Get the result set
  72.     //$result = $sh->fetchAll(PDO::FETCH_FUNC, "listify");
  73.    
  74.     // Start the list
  75.     echo '<ol>';
  76.    
  77.     // Iterate through the result set
  78.     //foreach($result as $value) {
  79.     while($value = mysql_fetch_assoc($results)) {
  80.         // Echo the value
  81.         echo '<li><a href="index.php?pid='.$value['id'].'">'.$value['name'].'</a></li>';
  82.     }
  83.    
  84.     // Finish the list
  85.     echo '</ol>';
  86. }
  87. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement