LauraL19 Posted October 30, 2013 Share Posted October 30, 2013 Hi, I am a complete newbie and would like to self teach to build my own database driven website. I am working from "PHP&MySQL Web Development for Dummies (Janet Valade) as a starting point. After experiencing a number of issues with the code I had created for my own site, I decided to just write the code as provided in the book, this is for a simple catalogue. But I am getting problems with that. The error message I get is - Query died: category Thank you so much in anticipation of any help I am using PHP 5.5.3, and MySQL 5.6.11 and Apache 2.4.4, running on Windows 7 pro. This is the code for the main PHP file (catalogue.php), it calls 3 includes (dbstuff.inc, page_furniture_index.inc, page_furniture_products.inc), I will add the code for these if anyone wishes. But for now here is the main PHP file - <?php /* Program: Catalogue.php * Desc: Dislays a catalogue of products */ include ("dbstuff.inc"); $n_per_page = 2; if(isset($_POST['products'])) { if(!isset($_POST['interest'])) { header("location: Catalog_furniture.php"); exit(); } else { if(isset($_POST['n_end'])) { if($_POST['Products'] == "Previous") { $n_start = $_POST['n_end']-($n_per_page)-1; } else { $n_start = $_POST['n_end'] +1; } } else { $n_start = 1; } $n_end = $n_start + $n_per_page - 1; $cxn = mysqli_connect($host,$user, $passwd, $database); $query = "select * FROM Furniture WHERE type = '$_POST[interest]' ORDER BY name"; $result = mysqli_query($cxn,$query) or die ("query died: furniture"); $n=1; while($row = mysqli_fetch_assoc($result)) { foreach($row as $field => $value) { $products[$n][$field]=$value; } $n++; } $n_products = sizeof ($products); if($n_end > $n_products) { $n_end = $n_products; } include ("page_furniture_products.inc"); } // end else isset interest } // end if isset products else { $cxn = mysqli_connect ($host, $user, $passwd, $database); $query = "SELECT DISTINCT category, type FROM Furniture ORDER BY category, type"; $result = mysqli_query($cxn, $query) or die ("Query died: category"); while($row = mysqli_fetch_array($result)) { $furn_Categories[$row['category']][]=$row['type']; } include("page_furniture_index.inc"); } ?> Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted October 30, 2013 Share Posted October 30, 2013 Can you do us a favor? Can you repost your code within [/nobbc] tags? You can create those tags either by clicking on the <> at the top of the text editor, or by manually writing them. So, when you have something written as: [nobbc] // stuff It'll actually look like: // stuff It'll likely help with indentation, as your code currently has none, making it difficult to see where conditionals and loops begin and end. Thanks. Quote Link to comment Share on other sites More sharing options...
LauraL19 Posted October 30, 2013 Author Share Posted October 30, 2013 How about this - Hi, I am a complete newbie and would like to self teach to build my own database driven website. I am working from "PHP&MySQL Web Development for Dummies (Janet Valade) as a starting point. After experiencing a number of issues with the code I had created for my own site, I decided to just write the code as provided in the book, this is for a simple catalogue. But I am getting problems with that. The error message I get is - Query died: category Thank you so much in anticipation of any help I am using PHP 5.5.3, and MySQL 5.6.11 and Apache 2.4.4, running on Windows 7 pro. This is the code for the main PHP file (catalogue.php), it calls 3 includes (dbstuff.inc, page_furniture_index.inc, page_furniture_products.inc), I will add the code for these if anyone wishes. But for now here is the main PHP file - <?php /* Program: Catalogue.php * Desc: Dislays a catalogue of products */ include ("dbstuff.inc"); $n_per_page = 2; if(isset($_POST['products'])) { if(!isset($_POST['interest'])) { header("location: Catalog_furniture.php"); exit(); } else { if(isset($_POST['n_end'])) { if($_POST['Products'] == "Previous") { $n_start = $_POST['n_end']-($n_per_page)-1; } else { $n_start = $_POST['n_end'] +1; } } else { $n_start = 1; } $n_end = $n_start + $n_per_page - 1; $cxn = mysqli_connect($host,$user, $passwd, $database); $query = "select * FROM Furniture WHERE type = '$_POST[interest]' ORDER BY name"; $result = mysqli_query($cxn,$query) or die ("query died: furniture"); $n=1; while($row = mysqli_fetch_assoc($result)) { foreach($row as $field => $value) { $products[$n][$field]=$value; } $n++; } $n_products = sizeof ($products); if($n_end > $n_products) { $n_end = $n_products; } include ("page_furniture_products.inc"); } // end else isset interest } // end if isset products else { $cxn = mysqli_connect ($host, $user, $passwd, $database); $query = "SELECT DISTINCT category, type FROM Furniture ORDER BY category, type"; $result = mysqli_query($cxn, $query) or die ("Query died: category"); while($row = mysqli_fetch_array($result)) { $furn_Categories[$row['category']][]=$row['type']; } include("page_furniture_index.inc"); } ?> Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted October 30, 2013 Share Posted October 30, 2013 Oh, you actually wrote your code without indents? Yeah, that's a problem. Not with the code itself, but with our ability to read it and deduce the problem you're having. Generally speaking, { } denotes a block of code. Whatever is between them should be indented (usually one tab/4 spaces). So, it should be: if (/* something */) { // code // code //code } else { // more code // more code // more code } while (/* something else */) { if (/* another thing */) { // code // code } else { // more code } } if (/* blah */) { if (/* foo */ ) { // code } } else { // more code } See how it makes it easier to read? We'll need you to do that before going any further. It may seem trivial (and like a PITA), but since code is a visual medium, it makes a lot of difference. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 30, 2013 Share Posted October 30, 2013 (edited) The following query is failing most probably due to an error $query = "SELECT DISTINCT category, type FROM Furniture ORDER BY category, type"; $result = mysqli_query($cxn, $query) or die ("Query died: category"); To see what the error is call mysqli_error(). Edited October 30, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
LauraL19 Posted October 30, 2013 Author Share Posted October 30, 2013 It was actually indented, but pasting it here it lost its indents. I should say this is not my code, it is code from Janet Valade. I had been writing my own code based on the principles gleaned from her book, but ving issues, so decided to start with her code, then modify it to my needs. So having downloaded it from her site, I found I had problems with this as well, so assume I have something fundamentally wrong. Anyway here is the code again, hopefully with indents this time. Thank you. <?php /* Program: Catalogue.php * Desc: Dislays a catalogue of products */ include ("dbstuff.inc"); $n_per_page = 2; if(isset($_POST['products'])) { if(!isset($_POST['interest'])) { header("location: Catalog_furniture.php"); exit(); } else { if(isset($_POST['n_end'])) { if($_POST['Products'] == "Previous") { $n_start = $_POST['n_end']-($n_per_page)-1; } else { $n_start = $_POST['n_end'] +1; } } else { $n_start = 1; } $n_end = $n_start + $n_per_page - 1; $cxn = mysqli_connect($host,$user, $passwd, $database); $query = "select * FROM Furniture WHERE type = '$_POST[interest]' ORDER BY name"; $result = mysqli_query($cxn,$query) or die ("query died: furniture"); $n=1; while($row = mysqli_fetch_assoc($result)) { foreach($row as $field => $value) { $products[$n][$field]=$value; } $n++; } $n_products = sizeof ($products); if($n_end > $n_products) { $n_end = $n_products; } include ("page_furniture_products.inc"); } // end else isset interest } // end if isset products else { $cxn = mysqli_connect ($host, $user, $passwd, $database); $query = "SELECT DISTINCT category, type FROM Furniture ORDER BY category, type"; $result = mysqli_query($cxn, $query) or die ("Query died: category"); while($row = mysqli_fetch_array($result)) { $furn_Categories[$row['category']][]=$row['type']; } include("page_furniture_index.inc"); } ?> Quote Link to comment Share on other sites More sharing options...
LauraL19 Posted October 30, 2013 Author Share Posted October 30, 2013 Arghhhhh, no it isn't, formatting is lost during the pasting. Anything I need to do to prevent this on this web site? Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 30, 2013 Share Posted October 30, 2013 (edited) Did you read reply #5 above? Arghhhhh, no it isn't, formatting is lost during the pasting. Anything I need to do to prevent this on this web site? Before pasting your code using Ctl+V type [/nobbc] then clicking the Paste as Plain Text icon and then type [nobbc] . Does that make a difference? Edited October 30, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
LauraL19 Posted October 30, 2013 Author Share Posted October 30, 2013 Hi Ch0cu3r, That is a great help thank you. So my Database login seems to be failing I get an - Warning: mysql_errno() expects parameter 1 to be resource, string given in C:\xampp\htdocs\dbstuff.inc on line 3 For every line of the following, this is the first include that my PHP file loads - <?php $host = "localhost"; $user = "admin"; $passwd = "xy.34W"; $dbname = "FurnitureCatalog"; ?> Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 30, 2013 Share Posted October 30, 2013 Sorry, I mistyped the link in my previous post it should be http://php.net/mysqli_error Change $result = mysqli_query($cxn, $query) or die ("Query died: category"); to $result = mysqli_query($cxn, $query) or die ("Query died: category - " . mysqli_error()); What is the error? Quote Link to comment Share on other sites More sharing options...
LauraL19 Posted October 30, 2013 Author Share Posted October 30, 2013 It returns - Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\dbstuff.inc on line 6 Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 31, 2013 Share Posted October 31, 2013 What! You dont put the code I suggested into dbstuff.inc. You replace this code (lines 60 - 61) in Category.php $result = mysqli_query($cxn, $query) or die ("Query died: category"); with this $result = mysqli_query($cxn, $query) or die ("Query died: category - " . mysqli_error()); Post the error message here. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted October 31, 2013 Share Posted October 31, 2013 @Ch0cu3r, mysqli_error() requires the connection link as a parameter. otherwise it produces an error itself rather than displaying the actual error. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 31, 2013 Share Posted October 31, 2013 @LauraL19 use the following instead $result = mysqli_query($cxn, $query) or die ("Query died: category - " . mysqli_error($cxn)); Quote Link to comment Share on other sites More sharing options...
LauraL19 Posted October 31, 2013 Author Share Posted October 31, 2013 Hi chaps, thanks for your help. from replacing $result..... with your suggested line above, I get the following message - Warning: mysqli_connect(): (HY000/1045): Access denied for user 'admin'@'localhost' (using password: YES) in C:\xampp\htdocs\Catalogue.php on line 61 Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\Catalogue.php on line 65 Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\Catalogue.php on line 65 Query died: category Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 31, 2013 Share Posted October 31, 2013 (edited) Your code is not connecting to the MySQL database properly. The mysql server is not allowing the admin user to connect the database on localhost. How have you setup the admin user? Edited October 31, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
LauraL19 Posted October 31, 2013 Author Share Posted October 31, 2013 I followed the instructions as set out in the book. I have since given full privileges to "admin". I have set the password as instructed in the book and been very careful in following every step. Quote Link to comment Share on other sites More sharing options...
LauraL19 Posted October 31, 2013 Author Share Posted October 31, 2013 OK I have changed $passwd to $password in both catalogue.php and dbstuff.inc which seems to have taken me a step forward. The only error I now get is - Query died: category - No database selected Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 31, 2013 Share Posted October 31, 2013 Change $dbname to $database in dbstuff.inc Quote Link to comment Share on other sites More sharing options...
LauraL19 Posted November 2, 2013 Author Share Posted November 2, 2013 Thank you for your help chaps, much 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.