Snoozee Posted September 21, 2015 Share Posted September 21, 2015 HI I'm creating my first e-commerce website and have come up against a problem that I can't work out on my own :-( I have my MySQL database created with the following headers product_name product_code price details In my HTML coding I want to be able to echo a particular row so that whatever "value" is in that row displays on the webpage e.g. so rather than having the code like this - <span class="product-price">$49.95</span> it would be something like this - <span class="product-price">echo #row 20 ('price')</span>. I'm aware that I would need to have the query run first, yet am having problems with this also :-( Any help would be greatly appreciated. Quote Link to comment Share on other sites More sharing options...
requinix Posted September 21, 2015 Share Posted September 21, 2015 It'll be easier to help you with the code you have than to come up with something from scratch. What's your code so far, and exactly what kind of problem(s) are you having with it? Quote Link to comment Share on other sites More sharing options...
hansford Posted September 21, 2015 Share Posted September 21, 2015 (edited) This will get you started. You'll need to make adjustments depending if you just want to return a specific row or all rows. And format the database output. <?php error_reporting(E_ALL); ini_set('display_errors',1); define('br','<br />'); $db = new mysqli('localhost',$username,$password,$dbname); if( !$db) { echo $db->errno . ': ' . $db->error . br; } $sql = "SELECT product_name,product_code,price,details FROM table_name"; if($result = $db->query($sql)) { while($row = $result->fetch_assoc()) { echo "<span class='class_name'>{$row['product_code']}</span>"; echo "<span class='class_name'>{$row['product_name']}</span>"; echo "<span class='class_name'>{$row['price']}</span>"; echo "<span class='class_name'>{$row['details']}</span>"; } } Edited September 21, 2015 by hansford Quote Link to comment Share on other sites More sharing options...
hansford Posted September 21, 2015 Share Posted September 21, 2015 I messed up the previous error handling...should be: $db = new mysqli('localhost',$username,$password,$dbname); if($db->connect_errno) { echo $db->connect_errno . ': ' . $db->connect_error; exit(); } $sql = "SELECT product_name,product_code,price,details FROM table_name"; if($result = $db->query($sql)) { while($row = $result->fetch_assoc()) { echo "<span class='class_name'>{$row['product_code']}</span>"; echo "<span class='class_name'>{$row['product_name']}</span>"; echo "<span class='class_name'>{$row['price']}</span>"; echo "<span class='class_name'>{$row['details']}</span>"; } } else { echo 'No results returned from database'; } Quote Link to comment Share on other sites More sharing options...
Snoozee Posted September 21, 2015 Author Share Posted September 21, 2015 Ok just to clarify :-) I have the PHP to connect to my database yet as in the above example I can only work out how to echo the entire database not echo a specific row in my html code. I somehow need to get the query to look at everything within the database & then from this have it return the value of a certain row and column ID within my line of html code - refer example in first post. Quote Link to comment Share on other sites More sharing options...
Landslyde Posted September 21, 2015 Share Posted September 21, 2015 Maybe this will be helpful to you. $product_name = "Poulan" /* Set this variable to the product name you're looking for. Of course, for this field you'll be using the entry from an input box or a select box. */ $stmt = $db->prepare('SELECT product_name, product_code, price, details FROM products WHERE product_name = :product_name $stmt->bindValue(':product_name', $product_name, PDO::PARAM_INT); $stmt->execute(); $result = $stmt->fetchAll(); ?> <div class="row"> <div class="table-repsonsive"> <table class="table table-bordered table-hover table-striped" style="margin-top:30px"> <thead> <tr> <th style="text-transform: uppercase">Product Name</th> <th style="text-transform: uppercase">Product Code</th> <th style="text-transform: uppercase">Product Price</th> <th style="text-transform: uppercase">Product Details</th> </tr> </thead> <?php foreach($result as $row ) { $i++; ?> <tbody> <tr> <td style="width:auto;"><?php echo $row[0] ?></td> /* Outout for Product Name */ <td style="width:auto;"><?php echo $row[1] ?></td> /* Outout for Product Code */ <td style="width:auto;"><?php echo $row[2] ?></td> /* Outout for Product Price */ <td style="width:auto;"><?php echo $row[3] ?></td> /* Outout for Product Details */</tr> I hope this helps you. Quote Link to comment Share on other sites More sharing options...
Snoozee Posted September 21, 2015 Author Share Posted September 21, 2015 Thanks this looks perfect so I'll give it a try. Thank you so much as it's greatly appreciated Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.