Jump to content

Need help creating a query


wee493

Recommended Posts

I have a table similar the the one below.

Countryurl

United States125

Canada300

Canada125

United States125

 

Currently I use the query below to get all the distinct countries.

SELECT DISTINCT country FROM log WHERE url= '$url'

 

But, I try to count the number of rows WHERE the url = 125. It only does it for the first row.

 

This is the whole script

$sql = mysql_query("SELECT DISTINCT country FROM log WHERE url = '$url'");
while ($r = mysql_fetch_array($sql)){
	$country = $r['country'];
		//$sql = mysql_query("SELECT COUNT(country) FROM log WHERE url = '$url' AND country = '$country'");
		$sql = mysql_query("SELECT * FROM log WHERE url = '$url' AND country = '$country'");
	//$visits = mysql_result($sql, 0);
	echo $country;
	echo " - $visits Visits";
	echo "<br>";
}

 

Any help would be amazing!

Link to comment
Share on other sites

If you are talking about this commented line:

 

//$sql = mysql_query("SELECT COUNT(country) FROM log WHERE url = '$url' AND country = '$country'");

 

It looks like you just need to remove the $country variable from the WHERE clause. Right now, it is only counting the countries where the country name is $country and the URL is $url.

 

For instance, if $country="Canada" and $url="125", it would only give you a count of 1 because there is only one instance where the WHERE clause is true. (Based on the info in your post.)

 

If you remove the $country variable from the WHERE clause, you should see a count of '3' if $url="125".

Link to comment
Share on other sites

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.