What's wrong with this query?

I can’t see anything that would cause this error. Any of this code look like it’s calling a boolean? Thanks.

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean

Won’t work. Same issue…

Admin logs in. Admin has adminID and customerID.
The admin’s adminID AND customerID are pulled from the ADMINS TABLE and stored, ‘temporarily’.

When admin successfully logs in, he is referred to a main admin page, where:

On that page, I’d like to display all the PRODUCTS from the PRODUCTS TABLE that have the same CUSTOMER ID as the admin’s CUSTOMER ID, not the admin’s ADMIN ID.

So when I write the code, I need the adminLOGIN page to call the validate php page and:

  1. Query the admin’s adminID and password against the ADMINS table and make sure it’s OK to login.
  2. Upon success of number 1, ‘save’ the admin’s adminID AND customerID from the CUSTOMERS TABLE.
  3. Query the PRODUCTS TABLE for all products that share a CUSTOMER ID, NOT ADMIN ID, with the admin
  4. Send the admin to the main admin page where said products are displayed.

This code is throwing an error calling for a boolean response, which should be asking for the second query?

Any help is appreciated. I have added various sessions and session start formats to the code, same error.

I am butting into the middle of a long conversation and probably gonna miss something but here is a confusing statement to me:
[php] $query2=“SELECT count(prodid), productname FROM products WHERE custid = $custid”;
$result2 = mysql_query($query2);

//—the line below this is line 32-------------

         $row2=mysql_fetch_array($result2);[/php]

Reason why:

doing a mysql_fetch_array inside of a while loop? there is no array inside the while loop, it will loop through each item one at a time, so use mysql_fetch_assoc.
I might be way off I did not take the time to read all the comments but hopefully my two cents helps a little :wink:

Okay, so this is the current code that you last posted…
[php]

<?php echo "

Browse Products:


\n"; $query="SELECT custid from customers where adminid=adminid"; $result=mysql_query($query); while($row=mysql_fetch_array($result,MYSQL_ASSOC)) { $custid = $row['custid']; $query2="SELECT count(prodid), productname FROM products WHERE custid = $custid"; $result2 = mysql_query($query2); //---the line below this is line 32------------- *** error line *** $row2=mysql_fetch_array($result2); $total = $row2[0]; $entreename = $row2['entreename']; echo "" . $entreename . " (" . $total . ")
\n"; } ?>

[/php]
After glancing at this code, it appears that the first query may be incorrect. You are pulling data based on “where adminid=adminid”… so it will only pull data where the adminid field is actually “admindid” !!! Nothing will be pulled. And, then, you pass the value of the pulled data without checking for a row count of zero to the next query. I think you need to change this to something like this:
[php]

<?php echo "

Browse Products:


\n"; $query="SELECT custid from customers where adminid='" . $_SESSION['store_smalladmin'] . "'"; $result=mysql_query($query); while($row=mysql_fetch_array($result,MYSQL_ASSOC)) { $custid = $row['custid']; $query2="SELECT count(prodid), productname FROM products WHERE custid='" . $custid . "'"; $result2 = mysql_query($query2); //---the line below this is line 32------------- *** error line *** $row2=mysql_fetch_array($result2,); $total = $row2[0]; $entreename = $row2[1]; echo "" . $entreename . " (" . $total . ")
\n"; } ?>

[/php]
So, what this code seems to do is loop thru the entire customers table finding any customers that have a adminid that matches the logged in smalladminid from the login form. Then, it pulls ONE (the first) row from the products table where the custid matches the one pulled from the first query. Is that what you really wanted to do? Wouldn’t you want all of the products pulled in the section query? Let me know that answer so we can fix this up…

And, Plintu, you can query by array or association either will work. The difference is the way you use the data once the query is done. Either by $row[1],$row[2],etc or by $row[‘fieldname1’]. $row[‘fieldname2’]…

LMAO Ye you are right it was 2am and I think I might have been too tired :smiley: there is an array of info, each field in the row!! :smiley:
I am willing to bet adminid=adminid should have been adminid=$adminid, but he missed the dollar sign!! good find on that one ErnieAlex

Thanks! I hope that is the issue. I didn’t notice it early and should have caught it. It is always hard to debug someone elses code. Something like a missing ? ; $ & . ’ " is always the worst for me!
Anyway, let us know if that worked for you, TrayDavid…

I want to pull ALL of them, not just the first one. So any help is appreciated.

I want to pull all of them, not just the first one. Thanks!

Okay. I had to remove the comma (,) after the $result2 because I was getting error that there was unexpected ‘)’ in my code. Removed the comma and I don’t have errors but the code does not work yet. Shows empty screen where php should be pulling products from products table that share custid with admin. Thanks.

[php]

<?php echo "

Browse Products:


\n"; $query="SELECT custid from customers where adminid='" . $_SESSION['store_smalladmin'] . "'"; $result=mysql_query($query); while($row=mysql_fetch_array($result,MYSQL_ASSOC)) { $custid = $row['custid']; $query2="SELECT count(prodid), productname FROM products WHERE custid='" . $custid . "'"; $result2 = mysql_query($query2); //---the line below this is line 32------------- *** error line *** $row2=mysql_fetch_array($result2); $total = $row2[0]; $entreename = $row2[1]; echo "" . $entreename . " (" . $total . ")
\n"; } ?>[/php]

LOL, such a simple bit of code and a dozen people can’t find the error… Always a puzzle! LOL…

Okay, I would suggest debugging it the old fashioned way. Use some echos to see where you are…

First, let’s debug the first query… Where you have (near start):
echo “

Browse Products:


\n”;
$query=“SELECT custid from customers where adminid=’” . $_SESSION[‘store_smalladmin’] . “’”;
$result=mysql_query($query);
Add a line to see if it is getting this list. I would just echo the record count and that will tell you if it is really working. Something like: echo (mysql_num_rows($result)); This will print the number of rows pulled from the custid table. And, tell us if you have something to work with going into the next query.
If it displays 0 then, that query is bad or the data going into it is bad. You can also display the session variable too if you want… echo $_SESSION[‘store_smalladmin’]; To see if the data coming from the form is correct…
Now if the record number is NOT zero, we are getting results from the first query. Then, on to check the second query. To debug this one just print the same thing, number of rows found. Do this in the same way… WHere you have this code:
$custid = $row[‘custid’];
$query2=“SELECT count(prodid), productname FROM products WHERE custid=’” . $custid . “’”;
$result2 = mysql_query($query2);
Add echo (mysql_num_rows($result2)); Note that this may display a bunch of lines if the query is good and it found a lot of prodid’s… But, it will tell us some info…

Okay, debug the two queries and let us know what you find…

Thanks.

I might be wrong again LOL but I have never seen echo used with brackets:
[php]
echo (mysql_num_rows($result2));
[/php]

couldnt it just be :
[php]echo mysql_num_rows($result2); [/php]

?
if it can be used with brackets, please let me know so I can not doubt this :d

Plintu, echo can be with or without ()… Either should work. I was typing too fast and left them in… LOL

Dave, so, we know it is accepting your posted values and pulling the admin from the first query.
All good there. And, it is getting the custid. So, I think the problem is the way you are getting the assoc from the row of data.

Try it this way…
[php]
echo “

Browse Products:


\n”;
$query=“SELECT custid from customers where adminid=’” . $_SESSION[‘store_smalladmin’] . “’”;
$result=mysql_query($query);
while($row=mysql_fetch_array($result)) {
$custid = $row[‘custid’];
$query2=“SELECT count(prodid), productname FROM products WHERE custid=’” . $custid . “’”;
$result2 = mysql_query($query2);
$row2=mysql_fetch_array($result2);
$total = $row2[0];
$entreename = $row2[1];
echo “<a href=“smalladmin.php?content=tester&custid=” . $custid . “”>” . $entreename . " (" . $total . “)
\n”;
}
[/php]
After reading a LOT at php.net, it seems that opening two separate queries have some issues if they are nested. Not sure if this pertains to your code. But, I made one minor change. I left the fetch_array back to the default settings which will pull both numeric and assoc values. So, can you try this code and let me know. Also, the custid we are using is the one we got, “smalladmin”… Not to be a silly question, but, have you looked at the live data in the current database to make sure there is data that matches that custid??? Just thought of that… LOL… Let me know if this code works!

I’ll check these things out. Thanks!

Tried it, won’t work. Still pulling no data for products to display. Made sure I had a custid number in smalladmin and had some matching custid in customers. Then made sure those customers had data in products with their custid numbers. Thanks.

Any help is appreciated. Months working on this now…months. It’s a tough one.

Well, i’m late to this convo. but it’s relatively easy fix
it’s called debugging

Step 1
Get a mysql client (phpmyadmin works but i prefer a non broser based client such as HeidiSQL
Step 2
Use the client to connect to your database
use the query editor to enter what the end SQL statement should be, as well as what the result should be
This is to confirm your script query
Step 3
Add a debug print out of the query, if this may affect html content use html comments

 echo "<!--- Query: '$query' --->".PHP_EOL;

You can use view source if using this method

Didn’t work. Thanks.

This query seems to only display one item from the products table and I’d like to display them all (with matching custid numbers). Any help is appreciated. Thanks.

[php]<?php

   $query="SELECT custid, adminid from customers where adminid='" . $_SESSION['store_smalladmin'] . "'";
   $result=mysql_query($query);
   while($row=mysql_fetch_array($result,MYSQL_ASSOC)) {
         $custid = $row['custid'];

         $query2="SELECT count(prodid), productname FROM products WHERE custid='" . $custid . "'";
         $result2 = mysql_query($query2);


         $row2=mysql_fetch_array($result2);
         $total = $row2[0];
         $productname = $row2[1];
         echo "<a href=\"smalladmin.php?content=showproducts3&prodid=" . $prodid . "\">" . $productname . "</a> (" . $total . ")<br>\n";
         }

?>
[/php]

did u use a client to test your sql statements?

TrayDavid, Sorry, I lost this thread somehow… Did you ever sort out this query problem?
I am around for a couple days if you still need help with it… Ernie

Sponsor our Newsletter | Privacy Policy | Terms of Service