Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Empty variable

Posted on 2012-03-27
10
Medium Priority
?
262 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 60

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 60

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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 60

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 60

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 60

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In this article, we’ll look at how to deploy ProxySQL.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

604 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