spires Posted June 29, 2006 Share Posted June 29, 2006 Hi, I am building a shopping cart, which works great on my localhost. but when i upload it to my server i was getting errors, i have fixed most of them and now just need to get rid of any object code in my scitpt. Could someone please help?Heres the error Fatal error: Call to a member function on a non-object in D:\Domains\spirestest.com\wwwroot\login2\cart\inc\functions.inc.php on line 39Heres the codefunction showCart() { $cart = $_SESSION['cart']; if ($cart) { $items = explode(',',$cart); $contents = array(); foreach ($items as $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } echo '<form action="cart.php?action=update" method="post" id="cart">'; echo "<center>"; echo "<TABLE width=\"600\" cellpadding=\"0\" cellspacing=\"0\">"; echo "<TR bgcolor=\"#CCCCCC\"> <TD class=\"Title\">Track</TD> <TD class=\"Title\">Title</TD> <TD class=\"Title\">Price</TD> <TD class=\"Title\">Amount</TD> <TD class=\"Title\">Total</TD> <TD class=\"Title\">Remove</TD> </TR>\n" ; foreach ($contents as $id=>$qty) { $sql = 'SELECT * FROM books WHERE id = '.$id; $result = $db->query($sql); $row = $result->fetch(); extract($row); echo '<tr> <td><br>'.$title.' </td><td><br> '.$author.'</td> <td><br>£'.$price.'</td> <td><br><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td> <td><br>£'.($price * $qty).'</td> <td><br><a href="cart.php?action=delete&id='.$id.'" class="loginBox_text">Remove</a></td> </tr>'; $total += $price * $qty; } echo '</table>'; echo "</center>"; echo "<br><hr>"; echo '<table> <tr> <td align=\"right\"><span>Grand total: <strong>£'.$total.'</strong></span></td> </tr> <tr> <td align=\"right\"><div><button type="submit">Update cart</button></div></td> </tr> </table>'; echo '</form>'; } else { echo '<p class="loginBox_text">You shopping cart is empty.</p>'; } echo "</TABLE>\n"; return join('',$output);}Thanks for all of your help. [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /] Quote Link to comment https://forums.phpfreaks.com/topic/13209-editing-a-function/ Share on other sites More sharing options...
Buyocat Posted June 29, 2006 Share Posted June 29, 2006 It sounds like the error is coming from when you try to query the database. It is saying that $db is not an object. I don't see it being defined anywhere in your script so I think that is the problem. If $db is supposed to be a database abstraction object you need to include the class file and set it with $db = new DB(); or whatever. Quote Link to comment https://forums.phpfreaks.com/topic/13209-editing-a-function/#findComment-50838 Share on other sites More sharing options...
spires Posted June 29, 2006 Author Share Posted June 29, 2006 Hay, I have got abit further, Is there another way of doing this$result = $db->query($sql); $row = $result->fetch(); extract($row);Not in objects??????Thanks [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]Hi buyocatI really dont know much about objects. The script was connecting the new db() bla bla bla.But i'm trying to convert this so i doesn't need to .Is this a bad idea? Quote Link to comment https://forums.phpfreaks.com/topic/13209-editing-a-function/#findComment-50839 Share on other sites More sharing options...
Buyocat Posted June 29, 2006 Share Posted June 29, 2006 There are a number of ways to skin this cat. I suggest using a PEAR package, such as MDB2, which will handle most everything for you and give you more flexibility. Just in case you are worried about a learning curve, getting it running is as simple as including the file and following the (admittedly sparse) instructions/examples on PEAR. If you don't want to take the plunge however you can accomplish it with something along these lines...$dbc = mysql_connect(various information here);// check that the connection was established$result = @mysql_query(a query here);// check that the result is what you wanted// do stuffI'll let you look up those functions on php.net. You can probably just enter in...php.net/mysql_connectphp.net/mysql_query Quote Link to comment https://forums.phpfreaks.com/topic/13209-editing-a-function/#findComment-50844 Share on other sites More sharing options...
spires Posted June 29, 2006 Author Share Posted June 29, 2006 HI thanks for your help. I have already converted the inc.db page and that connects fine. <?php$host = 'europa';$user = '*******';$pass = '*******;$db = 'jeff_cart';$connect = mysql_connect($host, $user, $pass) or die ("could not connect"); mysql_select_db($db) or die ("could not select db");?>However, i just need to now convert this simple bit of code, so it none object related. $sql = 'SELECT * FROM books WHERE id = '.$id; $result = $db->query($sql); $row = $result->fetch(); extract($row); I been playing around with a few things here. Which removes the error, but no resutls are displaying?$sql = 'SELECT * FROM books WHERE id = '.$id; $result = mysql_query($sql); $row = mysql_fetch_row($result); extract($row);goto [a href=\"http://www.spirestest.com/login2/cart/cart.php\" target=\"_blank\"]My Webpage[/a]Thanks once again for all your help. Its nice to be in the hands of masters. Quote Link to comment https://forums.phpfreaks.com/topic/13209-editing-a-function/#findComment-50849 Share on other sites More sharing options...
Buyocat Posted June 29, 2006 Share Posted June 29, 2006 For the result try this:while ($data = mysql_fetch_array($result, MYSQL_NUM)) {echo $data[0];}that will echo out the first column of the first row fetched. Quote Link to comment https://forums.phpfreaks.com/topic/13209-editing-a-function/#findComment-50852 Share on other sites More sharing options...
spires Posted June 29, 2006 Author Share Posted June 29, 2006 No Luck, However, i'm not to sure if i put it in the right place. Could you please take a look?function showCart() { $cart = $_SESSION['cart']; if ($cart) { $items = explode(',',$cart); $contents = array(); foreach ($items as $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } echo '<form action="cart.php?action=update" method="post" id="cart">'; echo "<center>"; echo "<TABLE width=\"600\" cellpadding=\"0\" cellspacing=\"0\">"; echo "<TR bgcolor=\"#CCCCCC\"> <TD class=\"Title\">Track</TD> <TD class=\"Title\">Title</TD> <TD class=\"Title\">Price</TD> <TD class=\"Title\">Amount</TD> <TD class=\"Title\">Total</TD> <TD class=\"Title\">Remove</TD> </TR>\n" ; foreach ($contents as $id=>$qty) { $sql = 'SELECT * FROM books WHERE id = '.$id; $result = mysql_query($sql); $row = mysql_fetch_row($result); while ($data = mysql_fetch_array($result, mysql_num)) { echo $data[0]; echo '<tr> <td><br>'.$row['title'].' </td><td><br> '.$row['author'].'</td> <td><br>£'.$row['price'].'</td> <td><br><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td> <td><br>£'.($price * $qty).'</td> <td><br><a href="cart.php?action=delete&id='.$id.'" class="loginBox_text">Remove</a></td> </tr>'; $total += $price * $qty; } } echo '</table>'; echo "</center>"; echo "<br><hr>"; echo '<table> <tr> <td align=\"right\"><span>Grand total: <strong>£'.$total.'</strong></span></td> </tr> <tr> <td align=\"right\"><div><button type="submit">Update cart</button></div></td> </tr> </table>'; echo '</form>'; } else { echo '<p class="loginBox_text">You shopping cart is empty.</p>'; } echo "</TABLE>\n"; return join('',$output);}Cheers Buyocat Quote Link to comment https://forums.phpfreaks.com/topic/13209-editing-a-function/#findComment-50854 Share on other sites More sharing options...
Buyocat Posted June 29, 2006 Share Posted June 29, 2006 What error are you getting when you run this? If it isn't printing an error try echoing out something from the top of your script working down until you hit where the error is. Finally, if the error is with the mysql try this:$result = mysql($query) or die (mysql_error()); Or something like thatLooking at your code, I'm a little confused about what you're doing after the query. It looks like it would try to echo out a result column but then move past that and use mysql_fetch_row. I don't know how mysql_fetch_row works, but it would be worth print_r - ing the $row variable to see what it is being set to. Quote Link to comment https://forums.phpfreaks.com/topic/13209-editing-a-function/#findComment-50858 Share on other sites More sharing options...
spires Posted June 29, 2006 Author Share Posted June 29, 2006 Sorry to be such a pain. I'm not getting an error now. Its just not displaying any $*****Everything else in the function is being displatedThe page before is working, and the edit page is working, Its just this function. Goto and you will see, [a href=\"http://www.spirestest.com/login2\" target=\"_blank\"]My Webpage[/a]Thanks Quote Link to comment https://forums.phpfreaks.com/topic/13209-editing-a-function/#findComment-50866 Share on other sites More sharing options...
spires Posted June 29, 2006 Author Share Posted June 29, 2006 Hay, Thanks for your help.I HAVE DONE IT! WWOOOOHe is the solution,foreach ($contents as $id=>$qty) { $sql = 'SELECT * FROM books WHERE id = '.$id; $result = mysql_query($sql); $row = mysql_fetch_array($result); echo '<tr> <td><br>'.$row['title'].' </td><td><br> '.$row['author'].'</td> <td><br>£'.$row['price'].'</td> <td><br><input type="text" name="qty'.$row['id'].'" value="'.$qty.'" size="3" maxlength="3" /></td> <td><br>£'.($row['price'] * $qty).'</td> <td><br><a href="cart.php?action=delete&id='.$id.'" class="loginBox_text">Remove</a></td> </tr>'; $total += $row['price'] * $qty; }Thanks once again. [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /] Quote Link to comment https://forums.phpfreaks.com/topic/13209-editing-a-function/#findComment-50877 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.