Show login user column sum. error

Asked

Viewed 72 times

0

Good morning, everyone, help me? i do not understand much of php, but for me to finish my site missing only one thing: I need to post the sum of the values that are in the table but based on the logged in user, I already tried all the ways but I can not follow the last one I tested:

<?php    
    $_SESSION['usuario'] = 'user_login';
    $sql = $pdo->prepare("SELECT SUM(valor) AS total FROM investimentos WHERE usuario= usuario");

    //Passa o ID do usuário para a query
    $sql->bindValue(1, $_SESSION['user_login'], PDO::PARAM_INT);
    $sql->execute();
    $ln = $sql->fetchObject();
    echo $ln->total;    
?>

I’ve tried everything or take only column values without being logged in with you page is all blank.

  • Voce ta creating a session variable as a string 'user_login'..

4 answers

0

I believe the problem lies in your query. At the end "WHERE user= user", the user is as a string. You need to leave it as a variable, like this: '$user'.

I’ve never used PHP this way, object oriented, but I think it should solve.

  • Unfortunately no good does nothing for me gets only blank, I’m already giving up this tense, and I find no other way to do it.

0

Do so:

$usuario = $_SESSION['usuario'];

$sql = $pdo->prepare("SELECT SUM(valor) AS total FROM investimentos WHERE usuario= $usuario");

0

I believe your problem is when passing the value to be replaced in the query.

$sql->bindValue(1, $_SESSION['user_login'], PDO::PARAM_INT);

The line above will replace the first character "?" of your query by the second parameter passed in the call of the bindValue method which in your case is $_SESSION['user_login'], however your query does not have the character "?". Therefore you should replace your query with the following:

$sql = $pdo->prepare("SELECT SUM(valor) AS total FROM investimentos WHERE usuario = ?");
  • continues the same thing will I make the call to the database? just do not know how to do properly

  • Getting some error message?

  • Add the following code in the first line after opening the php tag to display the errors: ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);

  • appeared this error: Notice: Undefined variable: Pdo in /home/vol14_5/epizy.com/epiz_21930618/fastinvest.ml/htdocs/teste1.php on line 3 Fatal error: Uncaught Error: Call to a Member Function prepare() on null in /home/vol14_5/epizy.com/epiz_21930618/fastinvest.ml/htdocs/teste1.php:3 Stack trace: #0 {main} thrown in /home/vol14_5/epizy.com/epiz_21930618/fastinvest.ml/htdocs/teste1.php on line 3 and line 3 is SELECT

  • By the received message the error is in the $Pdo variable, there is not initialized for example: $Pdo = new PDO("mysql:host=localhost;dbname=database;","root","");

  • how much I test the error on the Execute line()

  • Can you show the wrong? the whole code too

  • Fatal error: Uncaught TypeError: PDO::__construct() expects parameter 4 to be array, string given in /home/vol14_5/epizy.com/epiz_21930618/fastinvest.ml/htdocs/teste.php:3 Stack trace: #0 /home/vol14_5/epizy.com/epiz_xxxxxx/fastinvest.ml/htdocs/teste.php(3): PDO->__construct('sql300.epizy.co...', 'epiz_xxx', 'xxxxxxx', 'epiz_21xxxx_w...') #1 {main} thrown in /home/vol14_5/epizy.com/epiz_xxxxx/fastinvest.ml/htdocs/test.php on line 3 this is the blessed error

  • As the error message is why you are passing a quarter parameter in the PDO constructor. You should call it as follows: $Pdo = new PDO("mysql:host=localhost;dbname=database;","root",""); replacing "localhost" with your server ip and "database" with your server’s database name, as seeing is passed only 3 parameters, dns, username and password.

Show 4 more comments

0

Try replacing the PDO parameter this way:

<?php    
    $_SESSION['usuario'] = 'user_login';
    $sql = $pdo->prepare("SELECT SUM(valor) AS total FROM investimentos WHERE usuario= :usuario");

    //Passa o ID do usuário para a query
    $sql->bindValue(':usuario', $_SESSION['user_login'], PDO::PARAM_INT);
    $sql->execute();
    $ln = $sql->fetchObject();
    echo $ln->total;    
?>

make sure the session variable is coming right, add some var_dump or print_r to see what the value is, and move on.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.