Jump to content

Recommended Posts

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");
}
?>
Link to comment
https://forums.phpfreaks.com/topic/283445-help-with-some-very-basic-coding/
Share on other sites

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.

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");
}
?>

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.

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 by Ch0cu3r

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");
}
?>

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 by Ch0cu3r

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 

 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";
?>

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?

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.

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 
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.