0

I did search for answer before I posted :) !

Now my problem is php mysql script have to count records in mysql database cells I will try to draw my db now

table name: tableurl (table contains two columns url and clicks)

----------------
 url      clicks
clhgfghfh    6
hgjhgjhgh    0
kjhgjhgjh    0
khgjhgjhg    1
asdasddsg    5
-----------------

Now I want to count all the clicks so output should be 12 ! here is my code

<?php
/* db connection included in head*/

$sql ="SELECT count(clicks) FROM tableurl";
if ($result=mysqli_query($con,$sql))
  {  
  $rowcount=mysqli_num_rows($result);
  printf("Clicks together %d \n",$rowcount);
  mysqli_free_result($result);
  }
?>

P.S. clicks in db is defined as INT lenght/value is 1 default is NULL

Any input what I'm doing wrong ?

1
  • mysqli_num_rows counts the rows returned from the db, so it correctly says 1 Commented Jan 27, 2015 at 7:32

2 Answers 2

3

You want to sum the clicks and not count the number of records in your table. Use SUM()

SELECT sum(clicks) as click_sum 
FROM tableurl

and then use something like

if($row = $result->fetch_assoc()) {
    $sum = $row["click_sum"];
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try with -

$sql ="SELECT SUM(clicks) as totalClicks FROM tableurl";
if ($result=mysqli_query($con,$sql))
  {  
  $res = mysqli_fetch_array($result);
  printf("Clicks together %d \n",$res['totalClicks']);
  mysqli_free_result($result);
  }

1 Comment

thank you for reply's I manageg to put together code and it works now thank you juergen d and thank you sgt ! code for anyone in same trouble. <?php /* Gets all click's */ $query = "SELECT SUM(clicks) As clickstogether FROM tableurl"; $result = mysqli_query($con, $query); $row = $result->fetch_assoc(); $median = $row['clickstogether']; echo $row['clickstogether']; ?> Anyone can fix my comment ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.