cty Posted December 18, 2006 Share Posted December 18, 2006 Anyone know where is my mistake?The error show when i click on "buy" in product.php.Please guide me.---------------------------------------------------------Error shown:Fatal error: Call to a member function fetch_row() on a non-object in C:\test\kelly.php on line 14----------------------------------------------------------//product.php<?phpinclude("db.php");$db=new mysqli('localhost','root','','test');$db->select_db('test');$query="select * from book order by title asc";$result = $db->query($query);?><?phpwhile($row =$result->fetch_assoc()){?> <table border=1 width=80% bgcolor="pink"><tr><td width=20%><font face="verdana" size="2" color="black" >ISBN :</font></td><td ><font face="verdana" size="2" color="black"><?php echo $row["isbn"]; ?></font></td></tr><tr><td width=20%><font face="verdana" size="2" color="black">TITLE:</font></td><td ><font face="verdana" size="2" color="black"><?php echo $row["title"]; ?></font></td></tr><tr><td width=20%><font face="verdana" size="2" color="black">Author :</font></td><td > <font face="verdana" size="2" color="black"><?php echo $row["author"]; ?></font></td></tr><tr><td width=20%><font face="verdana" size="2" color="black">Description :</font></td><td ><font face="verdana" size="2" color="black"><?php echo $row["description"]; ?></font></td></tr><tr><td width=20%><font face="verdana" size="2" color="black">Condition :</font></td><td ><font face="verdana" size="2" color="black"><?php echo $row["condition"]; ?></font></td></tr><tr><td width=20%><font face="verdana" size="2" color="black">Price(RM) : </font></td><td ><font face="verdana" size="2" color="black"><?php echo $row["price"]; ?></font></td></tr><td ><font face="verdana" size="2" color="black"><a href="cart.php?action=add_item&id=<?php echo $row["bookid"];?>">Buy</a></font></td></tr><br /></table><?php }?><tr><td ><hr size="2" color="red" NOSHADE></td></tr><tr><td ><font face="verdana" size="5" color="black"><a href="cart.php"> View Your Shopping Cart >></a></font></td></tr></table></body></html>------------------------------------------------//kelly.php<?phpfunction AddItem($bookid){$db=new mysqli('localhost','root','','test');$db->select_db('test');$query="select count(*) from cart where cookieId = '" . GetCartId() . "' and bookid = $bookid";$result=$db->query($query);$row =$result->fetch_row();$numRows = $row[0];if($numRows == 0){// This item doesn't exist in the users cart,// we will add it with an insert query$query="insert into cart(cookieId, bookid) values('" . GetCartId() . "', $bookid)";$result=$db->query($query);}else{ echo "The book already in your shopping cart."; // This item already exists in the users cart,}}function RemoveItem($bookid){$db=new mysqli('localhost','root','','test');$db->select_db('test');$query="delete from cart where cookieId = '" . GetCartId() . "' and bookid = $bookid";$result=$db->query($query);}function ShowCart(){$db=new mysqli('localhost','root','','test');$db->select_db('test');$query="select * from cart inner join items on cart.bookid = book.bookid where cart.cookieId = '" . GetCartId() . "' order by book.title asc";$result=$db->query($query);$totalCost=0;while($row = $result->fetch_assoc()){// Increment the total cost of all items$totalCost+=$row["price"];?><td width="55%" height="25"><font face="verdana" size="1" color="black"><?php echo $row["title"]; ?></font></td><td width="20%" height="25"><font face="verdana" size="1" color="black">$<?php echo number_format($row["price"], 2, ".", ","); ?></font></td><td width="10%" height="25"><font face="verdana" size="1" color="black"><a href="cart.php?action=remove_item&id=<?php echo $row["bookid"]; ?>">Remove</a></font></td></tr><br /><?php } ?><tr><td width="100%" colspan="4"><hr size="1" color="red" NOSHADE></td></tr><tr><td width="70%" colspan="2"><font face="verdana" size="1" color="black"><a href="products.php"><< Keep Shopping</a></font></td><td width="30%" colspan="2"><font face="verdana" size="2" color="black"><b>Total: $<?php echo number_format($totalCost, 2); ?></b></font></td></tr><?php } ?>---------------------------------------------------//cart.php<?phpinclude("kelly.php");include("db.php");?><?phpswitch($_GET["action"]){case "add_item":{AddItem($_GET["bookid"]);ShowCart();break;}case "remove_item":{RemoveItem($_GET["bookid"]);ShowCart();break;}default:{ShowCart();}}?>---------------------------------------------------//db.php<?phpfunction GetCartId(){// This function will generate an encrypted string and// will set it as a cookie using set_cookie. This will// also be used as the cookieId field in the cart tableif(isset($_COOKIE["cartId"])){return $_COOKIE["cartId"];}else{// There is no cookie set. We will set the cookie// and return the value of the users session IDsession_start();setcookie("cartId", session_id(), time() + ((3600 * 24) * 30));return session_id();}}?>-----------------------------------------------//end Quote Link to comment https://forums.phpfreaks.com/topic/31072-fatal-error/ Share on other sites More sharing options...
Cep Posted December 18, 2006 Share Posted December 18, 2006 Its my understanding that is should be mysql_fetch_row or odbc_fetch_row or something similar not just fetch_row Quote Link to comment https://forums.phpfreaks.com/topic/31072-fatal-error/#findComment-143476 Share on other sites More sharing options...
trq Posted December 18, 2006 Share Posted December 18, 2006 [quote]Its my understanding that is should be mysql_fetch_row or odbc_fetch_row or something similar not just fetch_row[/quote]The op is using the mysqli extension, so a call to the fetch_row() method is correct.However, that being said, I dont see where you use it in your code. What it does suggest though is that your call to the query() method of the mysqli object is failing and not returning the result object your expecting. Quote Link to comment https://forums.phpfreaks.com/topic/31072-fatal-error/#findComment-143484 Share on other sites More sharing options...
Cep Posted December 18, 2006 Share Posted December 18, 2006 [quote author=thorpe link=topic=119074.msg487201#msg487201 date=1166436118]The op is using the mysqli extension, so a call to the fetch_row() method is correct.[/quote]Ah! I see :) Quote Link to comment https://forums.phpfreaks.com/topic/31072-fatal-error/#findComment-143487 Share on other sites More sharing options...
JasonLewis Posted December 18, 2006 Share Posted December 18, 2006 in both those files fetch_row is only called once, at line 14 on kelly.php and i cant find anywhere where you are defining the function... hmmm... Quote Link to comment https://forums.phpfreaks.com/topic/31072-fatal-error/#findComment-143496 Share on other sites More sharing options...
cty Posted December 18, 2006 Author Share Posted December 18, 2006 Hi,since i m newbie in php.Can u make it more understand what should i do?And how i edit my code?TQ Quote Link to comment https://forums.phpfreaks.com/topic/31072-fatal-error/#findComment-143529 Share on other sites More sharing options...
HuggieBear Posted December 18, 2006 Share Posted December 18, 2006 [quote author=ProjectFear link=topic=119074.msg487214#msg487214 date=1166437979]in both those files fetch_row is only called once, at line 14 on kelly.php and i cant find anywhere where you are defining the function... hmmm...[/quote]There's no need to define the function. It's a function that's included with the [url=http://uk.php.net/manual/en/ref.mysqli.php]MySQLi[/url] extension.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/31072-fatal-error/#findComment-143544 Share on other sites More sharing options...
JasonLewis Posted December 19, 2006 Share Posted December 19, 2006 ahhh ok. i've never used MySQLi before you see. sorry bout that....is it possible to fetch something that is being counted? Quote Link to comment https://forums.phpfreaks.com/topic/31072-fatal-error/#findComment-144131 Share on other sites More sharing options...
trq Posted December 19, 2006 Share Posted December 19, 2006 Why did you post all that unrequired code? Justy complicates things.read my previous post, the error suggests your query is failing. Narrow the problem down a little, do some debugging, and if still need be, post the [b]relevent[/i] code. Quote Link to comment https://forums.phpfreaks.com/topic/31072-fatal-error/#findComment-144231 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.