?
Solved

Empty variable

Posted on 2012-03-27
10
Medium Priority
?
257 Views
Last Modified: 2012-03-27
I'm trying to figure out why $line['total1'] is empty when echo'd back?

$query = "select author_name, count(*) as total, 1 as type
from `posts` 
where from_unixtime(post_date) >=" . "'" . $startdate . "'" .
"and from_unixtime(post_date) <" . "'" . $enddate . "'" . 
"and new_topic = 0
group by author_name
union
select starter_name, count(*) as total1, 2 as type
from `topics`
where from_unixtime(start_date) >=" . "'" . $startdate . "'" .
"and from_unixtime(start_date) <" . "'" . $enddate . "'" . 
"group by starter_name
order by total desc";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

//Create the result set
while ($line= mysql_fetch_array($result))
	 {
	echo "Username: " .  $line['author_name'];
	echo "</br>";
	echo "Threads: " .  $line['total1'];
	echo "</br>";
	echo "Posts: " .  $line['total'];
	echo "</br>";
	echo "Overall Points: " .  (($line['total'] * $posts) + ($line2['total1'] * $topics));
	echo "</br>";
	echo "</br>";
	echo $line['total1'];
	}

Open in new window

0
Comment
Question by:Nathan Riley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 57

Accepted Solution

by:
HainKurt earned 2000 total points
ID: 37773586
select 10 as total
union
select 20 as total1

>>>>

total
10
20

the name from first query will be used to name columns... there wont be a new column...
0
 
LVL 57

Expert Comment

by:HainKurt
ID: 37773601
and use

if $line['type']=="1" echo "Threads: " . $line['total'];
if $line['type']=="2" echo "Posts: " . $line['total'];

instead of line 21-22-23
0
 
LVL 12

Author Comment

by:Nathan Riley
ID: 37773638
I tried, but I get the same number for posts and topics.

Code Changed:
$query = "select author_name, count(*) as total, 1 as type
from `posts` 
where from_unixtime(post_date) >=" . "'" . $startdate . "'" .
"and from_unixtime(post_date) <" . "'" . $enddate . "'" . 
"and new_topic = 0
group by author_name
union
select author_name, count(*) as total, 2 as type
from `posts` 
where from_unixtime(post_date) >=" . "'" . $startdate . "'" .
"and from_unixtime(post_date) <" . "'" . $enddate . "'" . 
"and new_topic = 1
group by author_name
order by total desc";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

//Create the result set
while ($line= mysql_fetch_array($result))
	 {
	echo "Username: " .  $line['author_name'];
	echo "</br>";
	if ($line['type'] = "2")
	echo "Threads: " .  $line['total'];
	echo "</br>";
	if ($line['type'] = "1")
	echo "Posts: " .  $line['total'];
	echo "</br>";
	echo "Overall Points: " .  (($line['total'] * $posts) + ($line2['total1'] * $topics));
	echo "</br>";
	echo "</br>";
	echo $line['total1'];;
	}

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 57

Expert Comment

by:HainKurt
ID: 37773687
you will get a result like

author_name total type
aaaa 100 1
bbbb 125 1
ccccc 256 2
dddd 342 2

Open in new window


first 2 will be posts, last 2 will be threads...

and I guess you should use == not =

if ($line['type'] == "1") echo "Posts: " .  $line['total'];
if ($line['type'] == "2") echo "Threads: " .  $line['total'];
echo "</br>";
0
 
LVL 12

Author Comment

by:Nathan Riley
ID: 37773721
Strange, if I change to:

if ($line['type'] == "1")
	echo "Threads: " .  $line['total'];
	echo "</br>";
	if ($line['type'] == "2")
	echo "Posts: " .  $line['total'];

Open in new window


It ends up giving the the post count for threads and nothing for posts.
0
 
LVL 57

Expert Comment

by:HainKurt
ID: 37773755
what about this

$query = "select author_name, 
       sum(case when new_topic = 0 then 1 else 0 end) post_total,
       sum(case when new_topic = 1 then 1 else 0 end) thread_total
from `posts` 
where from_unixtime(post_date) >=" . "'" . $startdate . "'" .
"and from_unixtime(post_date) <" . "'" . $enddate . "'" . 
group by author_name
order by total desc";

with above query you will get

author_name post_total thread_total
HainKurt  234 512
SomeOne 102 253

Open in new window


one query returns all data...
0
 
LVL 12

Author Comment

by:Nathan Riley
ID: 37773805
All that data looks good that it returns, how would I modify my "if" statements then?
0
 
LVL 12

Author Comment

by:Nathan Riley
ID: 37773831
Ah it eliminates it, got it working, thanks for all the help!
0
 
LVL 57

Expert Comment

by:HainKurt
ID: 37773944
I did not look at query closely at first, thought they are getting data from different tables :)
With new query, you will get all data in same row, so you dot need if in your code...
0
 
LVL 12

Author Comment

by:Nathan Riley
ID: 37773949
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
In this article, we’ll look at how to deploy ProxySQL.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question