hosted by liquidweb


Go Back   Web Hosting Talk : Other Forums : Web Hosting Lounge : PHP/MYSQL problem
Reply

Web Hosting Lounge Forum for general conversation, share interests, have a laugh or discuss topics not related to the above or below forums.
Forum Jump

PHP/MYSQL problem

Reply Post New Thread In Web Hosting Lounge Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 01-21-2002, 05:07 PM
rrsnider rrsnider is offline
Junior Guru Wannabe
 
Join Date: Nov 2001
Posts: 91
Question

PHP/MYSQL problem


I am trying to execute what I considered to be a simple command using MYSQL. I am working on an e-commerce site and I want to count the number of products in a category. I am trying to do a "SELECT COUNT" statement, but I can not get it to execute. Here are my statements:

$sql = "SELECT COUNT (*) FROM products WHERE Category =\"$cat\"";
$sql_result = mysql_query($sql,$connection) or die ("Could not execute count");

I get "Could not execute..." returned from the Query.

I know my table and variables are OK, because I am able to execute this query a few lines earlier.

$sql = "SELECT * FROM products WHERE Category = \"$cat\" LIMIT $j,$num_display";
$sql_result = mysql_query($sql,$connection) or die ("Could not execute query");

What am I doing wrong with my "SELECT COUNT" statement? I am guessing that I have a syntax problem by trying to perform the query from PHP (it works fine in MYSQL front).

Reply With Quote
Sponsored Links
  #2  
Old 01-21-2002, 05:17 PM
ho247 ho247 is offline
Web Hosting Master
 
Join Date: Aug 2001
Location: United kingdom
Posts: 1,003
I don't think you can do a COUNT(*)... replace * with one field, usually the ID of the product, so it'll be SELECT COUNT(id), something like that.

Also, to get a more detail error, change:

Quote:
or die ("Could not execute count");
to:

Quote:
or print mysql_error();
that will give you details of which part of the SQL statement is incorrect.

Tell me if that helps.

Alan

__________________
Alan Ho
Former Systems Administrator

Reply With Quote
  #3  
Old 01-21-2002, 05:30 PM
rrsnider rrsnider is offline
Junior Guru Wannabe
 
Join Date: Nov 2001
Posts: 91
Thanks for the quick response. That corrected my syntax problem. My next question is how do I get the result of the "select count" into a variable that I can use?

Reply With Quote
Sponsored Links
  #4  
Old 01-21-2002, 05:51 PM
ho247 ho247 is offline
Web Hosting Master
 
Join Date: Aug 2001
Location: United kingdom
Posts: 1,003
There is a quicker way to get it into a variable than the way I'm going to tell you, but this is the way I use:

Change the SQL to:

SELECT COUNT (id) as productCount FROM...

then after the $sql_result line, put this:

while ( $row = mysql_fetch_array ($sql_result) ) {
$numberOfProducts = $row["productCount"];
}

Alan

__________________
Alan Ho
Former Systems Administrator

Reply With Quote
Reply

Related posts from TheWhir.com
Title Type Date Posted
Web Host Rackspace Launches Private Beta for MySQL Cloud Database Web Hosting News 2011-12-01 21:09:51
Parallels Now Offering Licenses for CloudLinux OS Web Hosting News 2011-10-26 17:29:25
Web Hosting Sales and Promos Roundup - October 7, 2011 Web Hosting News 2011-10-07 19:56:18
Web Hosting Software Firm CloudLinux Releases CloudLinux OS Version 6.1 Web Hosting News 2011-09-13 14:08:49
Web Host FireHost Partners with Cloud Security Firm Gazzang for Data Encryption Web Hosting News 2011-08-16 20:33:43


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?