Solved

Display MY SQL advanced Count in PHP

Posted on 2014-03-09
4
655 Views
Last Modified: 2014-03-09
Hi I have this PHP script below which works for a selcect count(*) from x

However when I put a more advanced MYSQL syntax in there it gives the following error"Notice: Undefined index: total in /var/www/dupe.php on line 12 Total ="

Any ideas






<?php
$con=mysqli_connect("localhost","ccc","N3wcccc21","actccccaign");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$query = "select count(*) from activecampaign.em_exclusion
Inner join activecampaign.em_subscriber
ON em_exclusion.email = em_subscriber.email";

$result = mysqli_query($con, $query);

echo $result;

mysqli_close($con);
?>
0
Comment
Question by:ncomper
4 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
$result = mysqli_query($con, $query);

this will only run the query, and $result will not be the "number" of the count(*), but a resource identifier. after that line, you need to fetch the row into either an object or an array, so after that line, you will do:
http://php.net/mysqli_fetch_array
$result_data = mysqli_fetch_array($result);
echo $result_data[0]; 

Open in new window

0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
Since there is no total in your sample code I'm going to assume you are not showing us the real code and expecting us to be psychic.

$query = "select count(*) as total from activecampaign.em_exclusion
Inner join activecampaign.em_subscriber
ON em_exclusion.email = em_subscriber.email"; 

$result = mysqli_query($con,$query);

$rows = mysqli_fetch_row($result );

echo $rows[0];

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
When in doubt, you can check the online man page for the return values from PHP functions.  Example: Ask yourself, "What is the expected return value from this function?"
http://php.net/manual/en/mysqli.query.php

Some learning resources to help you get started with things like this are available in this article.  Buy any of the books and give yourself a few weeks to read, study and practice.  In no time you'll be able to do this yourself!
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
0
 
LVL 5

Author Closing Comment

by:ncomper
Comment Utility
Amazing thank you
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now