Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Database structure
- Table products:
- Columns:
- id tinyint unsigned auto_increment
- name varchar(255)
- img varchar(255)
- desc text
- Attributes:
- Primary Key: id
- */
- // Configuration
- $db_engine = 'mysql'; // Database Engine
- $db_name = 'mydb'; // Database name
- $db_host = 'localhost'; // Database host
- $db_user = 'root'; // Database username
- $db_pass = ''; // Database password
- // Setup the Database object
- // $dbo = new PDO($db_engine.':dbname='.$db_name.';host='.$db_host', $db_user, $db_pass);
- mysql_connect($db_host, $db_user, $db_pass);
- mysql_select_db($db_name);
- // For security reasons, release the password now.
- unset($db_pass);
- // Let's grab and check the request data for what we do.
- if (!empty($_GET['pid'])) {
- // Show Products.
- // Prepare the statement
- //$sh = $dbo->prepare('SELECT name, img, desc FROM products WHERE id = ?');
- $result = mysql_query('SELECT name, img, desc FROM products WHERE id = '.mysql_real_escape_string($_GET['pid']));
- // Execute the statement with the required parameters
- //$sh->execute(array($_GET['pid']));
- // Fetch the results, if there are any.
- //if ($sh->rowCount() == 0) {
- // We have results!
- // Get the results in a nice, easy to use array
- //$results = $sh->fetch(PDO::FETCH_ASSOC);
- //} else {
- if (mysql_num_rows($result) != 1) {
- // Either we have no results, or somehow we got too many... Either way, a problem.
- die("Error: Product not found");
- }
- $results = mysql_fetch_assoc($results);
- // Time to output!
- echo '<h1>'.$results[0]['name'].'</h1>\n<img src="'.$results[0]['img'].'" alt="Product Picture" />\n<p>'.$results[0]['desc'].'</p>';
- } else {
- // Show list of items
- // Prepare the statement
- //$sh = $dbo->prepare('SELECT id, name FROM products');
- $results = mysql_query('SELECT id, name FROM products');
- // Execute the statement
- //$sh->execute();
- // A neat PDO trick, it'll automagically apply this function to the result set.
- //function listify($id, $name) {
- //return '<li><a href="index.php?pid='.$id.'">'.$name.'</a></li>';
- //}
- // Get the result set
- //$result = $sh->fetchAll(PDO::FETCH_FUNC, "listify");
- // Start the list
- echo '<ol>';
- // Iterate through the result set
- //foreach($result as $value) {
- while($value = mysql_fetch_assoc($results)) {
- // Echo the value
- echo '<li><a href="index.php?pid='.$value['id'].'">'.$value['name'].'</a></li>';
- }
- // Finish the list
- echo '</ol>';
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement