1

I am trying to add numbers located in my database. so I tried sum() in SQL but it didn't show the total number. I don't know whats wrong with my code.

This is my code

 <?php
session_start();

    $email =  $_SESSION['login_user'];

  $con = mysqli_connect("localhost" , "root" , "" , "db_name");
   if (mysqli_connect_errno()) {
  echo "failed to connect:" . mysqli_connect_error();
 } 

 $sql = "SELECT SUM(points) FROM points WHERE email='$email '";
 $result = mysqli_query($con, $sql);
$row = mysqli_fetch_array($result, MYSQLI_NUM);

 echo $row[0]. "total number" ;

 mysqli_free_result($result);

   mysqli_close($con);

   ?> 

it just shows total number. i don't know what is wrong here

  • You should be using prepared statements and bind the values - this would probably stop `'$email '` having a space after the field and so probably not matching the rows you want it to. – Nigel Ren Jun 16 '18 at 08:33
  • I'm missing some required context, like some example data, and the output it gives vs the output you expected. – GolezTrol Jun 16 '18 at 08:44
  • can you tell me in more detail? sorry, i am actually new to PHP and MySQL stuff. so I don't know very much about prepared statements. @NigelRen – Karma Akabane Jun 16 '18 at 09:00
  • soory, i didn't understand what are saying @GolezTrol – Karma Akabane Jun 16 '18 at 09:01
  • Have a look at https://stackoverflow.com/questions/1290975/how-to-create-a-secure-mysql-prepared-statement-in-php. BUT does removing the space I pointed out make the code work? – Nigel Ren Jun 16 '18 at 09:02
  • thanks for your advice, I will try. If I have any problem I will tell you. @NigelRen – Karma Akabane Jun 16 '18 at 09:04

1 Answers1

0

Use as total, and row of 'total' by mysqli_fetch_assoc.