SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2011
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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 Code:
    <?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
    Last edited by SpacePhoenix; Mar 30, 2012 at 00:27. Reason: placed php tags around the php code

  2. #2
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    4,909
    Mentioned
    96 Post(s)
    Tagged
    0 Thread(s)
    What error is php giving?
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2011
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    SpacePhoenix,

    Here is the error message I am getting...

    Notice: Undefined variable: totalpartners in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\partners\totalpartners.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

  4. #4
    Just Blow It bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    7,204
    Mentioned
    106 Post(s)
    Tagged
    1 Thread(s)
    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 Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style

  5. #5
    SitePoint Enthusiast
    Join Date
    May 2011
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 Code:
    <?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

  6. #6
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,094
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    change this,
    PHP Code:
      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.
    What I lack in acuracy I make up for in misteaks

  7. #7
    SitePoint Enthusiast
    Join Date
    May 2011
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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';
    ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •