?
Solved

Simple Count in PHP

Posted on 2014-03-08
10
Medium Priority
?
729 Views
Last Modified: 2014-03-09
Im trying to display a simple count query on a PSP page but it keeps coming up blank is there.  Should I have an error for the "show count" statement also


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

$query = "SELECT COUNT(*) from em_subsribers";
$result = mysql_query($query);
$values = mysql_fetch_assoc($result);
$num_rows = $values['total'];
echo $num_rows;

mysqli_close($con);
?>
0
Comment
Question by:ncomper
[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
10 Comments
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39915185
On this: $query = "SELECT COUNT(*) from em_subsribers";
if your table is called em_subscribers, then the query will return 0 results.

HTH,
Dan
0
 
LVL 5

Author Comment

by:ncomper
ID: 39915188
Sorry I dont understand ?
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39915189
BTW, you have multiple problems in that code. You're mixing and matching mysql and mysqli, and that's not good.
Plus, you have no field called total in your query, so why it should return one?
Try this:
<?php
$con=mysqli_connect("localhost","xxxx","xxxx","xxxx");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$query = "SELECT COUNT(*) AS total FROM em_subscribers"; 
$result = mysqli_query($con, $query); 
$values = mysqli_fetch_assoc($result); 
$num_rows = $values['total']; 
echo $num_rows;

mysqli_close($con);
?>

Open in new window

0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 5

Author Comment

by:ncomper
ID: 39915203
No just a blank page :( but if I put in the wrong sql details it will show me an error so its logs into the DB ?
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39915216
Then it's time for debugging.
<?php
$con=mysqli_connect("localhost","xxxx","xxxx","xxxx");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$query = "SELECT COUNT(*) AS total FROM em_subscribers"; 
echo "<pre>";
print_r ($query);
$result = mysqli_query($con, $query); 
echo "<pre>";
print_r ($result);
$values = mysqli_fetch_assoc($result); 
echo "<pre>";
print_r ($values);
$num_rows = $values['total']; 
echo $num_rows;
echo "<pre>";
print_r ($num_rows);

mysqli_close($con);
?>

Open in new window


Post the output, please.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39915273
I took Dan's code from http://www.experts-exchange.com/Database/MySQL/Q_28383752.html#a39915189 , put my own info in it, and it worked perfectly.
0
 
LVL 5

Author Comment

by:ncomper
ID: 39915638
Amazing that worked. However got this as a result.  

how would I hide the cost here so I just had

Total = XXXXX

Thanks ever so much

Terry


SELECT COUNT(*) AS total FROM em_subscriber
mysqli_result Object
(
    [current_field] => 0
    [field_count] => 1
    [lengths] =>
    [num_rows] => 1
    [type] => 0
)
Array
(
    [total] => 281504
)
281504
281504
0
 
LVL 5

Author Comment

by:ncomper
ID: 39915677
Fantastic that worked, however how do I hide all the code from the page (as this is what I got below).  Now all I really want is:

Total = XXXX


SELECT COUNT(*) AS total FROM em_subscriber
mysqli_result Object
(
    [current_field] => 0
    [field_count] => 1
    [lengths] =>
    [num_rows] => 1
    [type] => 0
)
Array
(
    [total] => 281504
)
281504
281504
0
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 2000 total points
ID: 39915696
Remove the debugging code:
<?php
$con=mysqli_connect("localhost","xxxx","xxxx","xxxx");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$query = "SELECT COUNT(*) AS total FROM em_subscribers"; 
$result = mysqli_query($con, $query); 
$values = mysqli_fetch_assoc($result); 
$num_rows = $values['total']; 
echo "Total = " . $num_rows;

mysqli_close($con);
?>

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39915800
If you want some background on how to use the MySQL extensions, this article may be helpful:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
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 dynamically set the form action using jQuery.
Suggested Courses

800 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