I know I posted something similiar to this a month or two ago, but since then my SQL database was not working, but now it's alive and kicking but I misplaced the answer I was given. So anyways, here's my situation:

I have a database of files, each file has a field where it specifies the number of downloads of that specific file. This INT increaes by one (1) every time someone downloads that file.

I was wondering how I could loop through the database and get a result of the total number of downloads of ALL the files.

Thanks again for your help!

    Hello,
    You could use the following.

    // Connect to DB
    $connect = mysql_connect($dbhost, $dbuser, $dbpass);

    // Change to the proper DB. Not really needed
    // But we use it to make sure.
    mysql_select_db($db) or die("Unable to select $db");

    // get the info in the database.
    $query = mysql_query("SELECT * FROM requests") or die("Cant find in database");

    // Make sure we did get them
    if ($query) {

    // Set total to zero
    $total = 0;

    // loop through the database
    while ($row = mysql_fetch_array($query)) {

    // Extract the totals for each and add them to the total.
    $total = $total + $row["prowname"];
    }

    // Print out the total
    echo $total;

    }else{
    // do something else here
    }

    Hope this helps.

    Regards,
    Ray

      Hahah! Sweet! That's exactly what I wanted! Thanks so much, man. You are God. 🙂

        I know there has been a lot of judgement of people and people defending eachother and that is not what I am about to say just so that you know.

        Please do not call someone God because it may seem like nothing but consider those of us who are christians and believe that there is a God and he may not feel great that his name is being thrown around and used for people, there is many other ways to thank someone

        Thanks a lot,
        mark

          Yes, you're right. My mis-use of the word "God" was uncalled for. I apologize to everyone out there.

          Just to cl3ear things up: I didn't mean any disrespect to anyone and I was just trying to show how appreciative I was.

          Once again, Sorry.

            Your welcome for the input. Glad I could help.

            Regards,
            Ray

              Write a Reply...