Solved

Simple Count in PHP

Posted on 2014-03-08
10
679 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
 
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
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 82

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 108

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Re-imbursement Claim System 3 25
Editing login page in zencart. 2 15
PHP loop not working 4 33
LOAD DATA LOCAL INFILE problem 11 22
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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 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 …

747 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

12 Experts available now in Live!

Get 1:1 Help Now