Solved

Simple Count in PHP

Posted on 2014-03-08
10
696 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
10 Comments
 
LVL 34

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 34

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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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 34

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 34

Accepted Solution

by:
Dan Craciun earned 500 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 109

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Creating and Managing Databases with phpMyAdmin in cPanel.
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.

809 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