Need help with calling a COUNT in php

Hello Forum,

I’m still new to php and I was hoping someone here could help me with my code.
here is my php function:

<?php 
include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';

function totalpartners()
{
  $result = mysqli_query($link, 'SELECT COUNT(*) FROM partners');
  if (!$result)
  {
     $error = 'Database error counting partners!';
     include 'error.html.php';
     exit();
  }

  $row = mysqli_fetch_array($result);

  return $row[0];
}
?>

My error comes from this code that is calling the function…

<?php echo htmlspecialchars($totalpartners, ENT_QUOTES, ‘UTF-8’); ?>

Thank you for taking the time to read my post and any help would be greatly appreciated. --Ben

What error is php giving?

SpacePhoenix,

Here is the error message I am getting…

Notice: Undefined variable: totalpartners in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\partners\ otalpartners.html.php on line 14

Also, here is the simple page that I am trying to get to work…

<?php include_once $_SERVER[‘DOCUMENT_ROOT’] . ‘/includes/totfunction.inc.php’; ?> //points to function

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>

<html xmlns=“http://www.w3.org/1999/xhtml” xml:lang=“en” lang=“en”>

<head>
<title>Total Partners</title>
</head>

<body>

<?php echo htmlspecialchars($totalpartners, ENT_QUOTES, ‘UTF-8’); ?>

</body>
</html>

Thank you again for taking the time to respond to my post. --Ben

You never defined a value for $totalpartners, which is a variable.

so you can either change the call to use totalpartners() instead or set the value of $totalpartners first (i.e. $totalpartners = totalpartners();).

Dave,

Thank you for the response, I was trying to follow an example in the SitePoint book “Build Your Own Database Driven Web Site Using PHP & MYSQL”, 4th Edition. On page 188 to 190 this PHP script is shown (below), however it never shows how to call the total count—it just gives you the function in the book. If there is a simpler way to call total count via a function, can you give me an example PHP function and script?

Here is the function I’ve tried with the $GLOBALS array as the book shows, yet I still can’t get it to work:


<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';

function totalpartners()
{
  $result = mysqli_query($GLOBALS['link'], 'SELECT COUNT(*) FROM partners');
  if (!$result)
  {
     $error = 'Database error counting partners!';
     include 'error.html.php';
     exit();
  }

  $row = mysqli_fetch_array($result);

  return $row[0];
}
?>

Thank you again for taking the time to read my post. --Ben

change this,


  if (!$result)
  {
     $error = 'Database error counting partners!';
     include 'error.html.php';
     exit();
  }

to

   if (!$result)
  {
     $error = 'Database error counting partners!';
     $totalpartners = '';// define it here to be nothing
     include 'error.html.php';
     exit();
  }

This will get rid of the error but will not fix anything because in your error.html.php file, $totalpartners has no value and will always be empty or NULL.

I just want to say thank you to everyone that has responded. I was able to figure out how the manual made this function work. I included this small script on my index page and it did the trick.

<?php
include_once ‘totfunction.inc.php’;

$totalpartners = totalpartners();

include ‘output.html.php’;
?>