Solved

Display MY SQL advanced Count in PHP

Posted on 2014-03-09
4
662 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
ID: 39916201
$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
ID: 39916210
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 109

Expert Comment

by:Ray Paseur
ID: 39916293
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
ID: 39916331
Amazing thank you
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

825 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