简体   繁体   中英

Counting values of a MySQL table in PHP

I have a table with users and printed pages like this:

user  |  pages
---------------
maria |  5

helen |  6

maria |  7

nick  |  10

nick  |  3

nick  |  6

I want to count how many pages has printed nick, helen, maria. I have tried some ways, but I don't get the right number of pages.

Any ideas?


Till now i was saving the csv file in a specific folder with copy/paste, now i am trying to give the option to a user to upload it himself. Here are my html and a part of php files.

 <form action="data.php" method="post" enctype="multipart/form-data">
                <label for="file">   </label>
                <input  type="file" name="file" id="file" />
          </br></br>      <input type="submit" value="Load data file into database" 
            name="button1"/>
    </form>    

data.php

<?php
$file_name=$_FILES["file"]["name"];
$file_type=$_FILES["file"]["type"]; echo"type:".$file_type;
$destination="D:\xampp\mysql\data\readcsvdb";
if($file_type!='text/csv')
                        {
                                echo "Please the input file should be a .csv file";
                        }
else
move_upload_file($file_name,$destination);

connect_db(); 

//Fill the table log of readcsv database with the csv's data

$q="LOAD DATA 
                 INFILE '$file_name' INTO TABLE log 
                 FIELDS TERMINATED BY ',' 
                 LINES TERMINATED BY \"\n\" 
                 IGNORE 2 LINES
                 (time,user,pages,copies,
                 printer,doc_name,client,paper_size,language,height,width,duplex,grayscale,size)";
                mysql_query($q) or die(mysql_error());

--------------------- I am getting an error: type:application/vnd.ms-excelPlease the input file should be a .csv fileFile 'D:\\xampp\\mysql\\data\\readcsvdb\\papercut-print-log-2012-10.csv' not found (Errcode: 2)

Try this

SELECT user, SUM(pages) AS totalPages 
FROM users 
GROUP BY user

SQLFiddle Demo

像这样使用Group By语句:

SELECT *, COUNT(*) FROM users GROUP BY user

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM