Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder Troy297's Avatar
    Join Date
    Oct 2006
    Location
    Earth
    Posts
    314
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Question Count Specific Records In DB

    Hey All,

    I am making a private messaging script as an addon for a script I have already made and was wondering how I would go about counting the number of messages in a table that are to a certain user.... eg... the database

    +--------------------+
    |todj | message |
    --------------------- |
    |User 1 | Blah blah... |
    |User 2 | Blah blah... |
    |User 1 | Blah blah... |
    +--------------------+

    Now what query would I use to ask it how many messages are there for User 1 and then echo it in text form? Something like this maybe?

    PHP Code:
    <?php
    $result 
    mysql_query("COUNT to WHERE to="$_SESSION['username']");
    ?>
    The table also contains a subject, date, and fromdj field if it makes any difference. Any help is always great! Thanks again!
    Everyone hears what you say, friends listen to what you say, best friends listen to what you don't say.
    Radio DJ Panel v3 - It's Here!

  • #2
    Regular Coder
    Join Date
    Nov 2006
    Posts
    247
    Thanks
    13
    Thanked 26 Times in 24 Posts
    Your table should have an ID row. Just select the count, like this:

    PHP Code:
    <?php

    $query 
    mysql_query("SELECT count(id) AS `total` FROM `table_name` WHERE `to`='$_SESSION[username]'");
    $result mysql_fetch_array($query);
    $total $result['total'];

    ?>
    Last edited by Majoracle; 01-27-2007 at 05:19 AM.

  • #3
    Regular Coder Troy297's Avatar
    Join Date
    Oct 2006
    Location
    Earth
    Posts
    314
    Thanks
    10
    Thanked 0 Times in 0 Posts
    I'm not sure what happened... heres the error that I got...

    Code:
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/qscriptz/public_html/radiodjpanel/navigation.php on line 108
    and the actual code....

    Code:
    <?php
    include('connect.php');
    if($_SESSION['rank'] == 'Administrator'){ 
    echo "
    <b>Radio</b><br>
    links go here
    <p>
    <b>Messaging</b><br>
    -<a href='inbox.php'>View Inbox (";
    $query = mysql_query("SELECT count(id) AS total FROM rp_pm WHERE to='$_SESSION[username]'");
    $result = mysql_fetch_array($query);
    $total = $result['total'];
    echo ")</a><br>
    -<a href='newmessage.php'>New Message</a><br>
    <p>
    <b>Your Account</b><br>
    links go here
    <p>
    <b>Admin Panel</b><br>
    links go here....
    <p>";
    }
    ?>
    So basically I want the link to show "View Inbox(#)" and where the # is where the current number of messages that have the "to" as their username. Any other ideas? Thanks again!
    Everyone hears what you say, friends listen to what you say, best friends listen to what you don't say.
    Radio DJ Panel v3 - It's Here!

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    There's an error with your query that you need to catch. Put a check just after the mysql_query() call (and assign the query text to a string so you can display it):

    PHP Code:
    $sql "SELECT count(id) AS total FROM rp_pm WHERE to='$_SESSION['username']'";
    $query mysql_query($sql);
    if (!
    $result) {
        die(
    "SQL ERROR! query: $sql<br />error text: ".mysql_error());


  • #5
    Regular Coder Troy297's Avatar
    Join Date
    Oct 2006
    Location
    Earth
    Posts
    314
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Thanks for the adivce but I played around with the code above and got it to work. I used the following...

    PHP Code:
    $result mysql_query("SELECT COUNT(id) FROM rp_pm WHERE todj='$_SESSION[username]'");
    $total mysql_fetch_array($result);
    echo 
    "View Inbox ("$total['COUNT(id)'] .")"
    Thanks for all the help!
    Everyone hears what you say, friends listen to what you say, best friends listen to what you don't say.
    Radio DJ Panel v3 - It's Here!

  • #6
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    I guess if you're prepared to spend a day on every query error you run across, you can ignore my suggestion

    Or you can check the query to see if it worked and if not display an error telling you exactly why it didn't work...

  • #7
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,634
    Thanks
    4
    Thanked 148 Times in 139 Posts
    Ooh, ooh, I'm in for the lengthy trouble shooting option! ;-)


  •  

    Posting Permissions

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