Search the Community
Showing results for tags 'database'.
-
I always get confused on this part. I am playing with a mySql Maria DB. in my localhost PC.... ohhh am using PDO Am I just checking for "0" failure and "1" Success return? some extra code to see how many rows effected .. in DB2 a return code of 100, meant no records were found but it was a successful execution. What kinda things do i need to check for? do the all return a code? What is the DB goes down? will my query return a code? or will it fail in another place? Besides a down DB what else must i look for? Thanks
-
Hello, After following along and successfully completing Ben's tutorial of the Online Shopping Cart, I figured I would try and make my own. The first major problem I am having is being able to display my products from my database in my index.php page. The goal is to actually see the images of each product displayed in index.php (like in the tutorial). This would then enable me to add my products to the cart.php page. I've checked the database connection and it does exist. My guess would be that I am using the wrong syntax to display my products in the "fetch_all_products()" method. *** M_PRODUCTS.PHP *** <?php class Products { private $Conn; private $db_table = "toys"; function __construct() { // here we're making sure that the connection from $conn in "init.php" is transferred // into our own private $conn property for usage in this object global $Conn; $this->Conn = $Conn; } // fetches and displays all products from db public function fetch_all_products($id = NULL) { if ($id = NULL) { $data = "<li>"; if ($result = $this->conn->query("SELECT * FROM " . $this->db_table . " ORDER BY name")) { if ($result->num_rows > 0) { while ($row = $result->fetch_array()) { $data .= array( "id" => $row["product_id"], "name" => $row["name"], "price" => $row["price"], "image" => $row["image"] ); } $data .= "</li>"; return $data; } else { return "<h1>Oops... Something went wrong!</h1>"; } } } } } *** INIT.PHP *** <?php // db connection $host = "localhost"; $user = "root"; $pass = "root"; $db = "cart"; $Conn = new mysqli($host, $user, $pass, $db); // error reporting ini_set("display_errors", 1); mysqli_report(MYSQLI_REPORT_ERROR); // constants define("shop_name", "Tremendous Toys"); define("main_path", "localhost/web_apps/shopping_cart/"); define("image_path", "localhost/web_apps/shopping_cart/images/"); // includes include("models/m_shopping_cart.php"); include("models/m_products.php"); // object integrations $Shopping_Cart = new Shopping_Cart(); $Products = new Products(); // session initialisation session_start(); *** INDEX.PHP *** <?php include("init.php") ?> <link rel="stylesheet" href="visual/style.css" type="text/css"> <body> <div id="whitespace"> <h1><?php echo shop_name ?></h1> <?php $Products->fetch_all_products(); ?> </div> </body>
-
- shopping cart
- database
-
(and 3 more)
Tagged with:
-
Hi people, I have one question about mysqli_fetch_assoc in a while loop. $query = "SELECT * FROM category "; $result = mysqli_query($connection, $query); while($row = mysqli_fetch_assoc($result)) { $cat_id = $row['cat_id']; $cat_title = $row['cat_title']; echo $cat_id . " " . $cat_title ."<br>"; } So, how does $row = mysql_fetch_assoc($result) works? So it loops one row at a time from $results and stores that information in $row until it there is no row to return? And this mysqli_fetch_assoc($result) in while loop iterations is this " array(some rows that it got from $result) "? $row = mysqli_fetch_assoc($result) is same as $row = array(all rows from $result that are gathered by mysqli_fetch_assoc) ? And that means $row is actually an array, and every time it loops the information is not overwriten by new instead it is added? Sorry if i confused you with such questions Thanks in advance!