Bring result of a sum on screen

Asked

Viewed 47 times

0

I’m trying to create a sum and display on screen. But almost every example I see uses the call of $conn and in my case I am using a file that already brings the connection to the bank and the select + query + Row are showing error.

Follows the code:

<?php
require 'config.php';
?>

<?php
$gasto = "select sum(buy) from home"
$resultgasto = mysqli_query($gasto);
$resultGasto_query = mysqli_fetch_row($resultgasto);

?>

<html>
   <head>
      <title>Tabela poker</title>
      <link rel="stylesheet" type="text/css" href="css/home.css">
   </head>
   <body>

      <table  style="padding-top: 20px;height: 202px;width: 171px;" ID="tabelabk1"  >
         <tr>
            <th>(%)</th>
            <th>VALOR</th>
         </tr>
         <tr>
            <td bgcolor="darkgreen">Gasto</td>
           <?php <td bgcolor="#FF6347">.$$resultGasto_query['buy'].</td>?>
         </tr>
         
         
         
         
         
         <?php
require_once 'config.php';
?>

<?php
$gasto = "select sum(buy) as buy from home;";
$resultgasto = mysqli_query($pdo, $gasto);
$resultGasto_query = mysqli_fetch_assoc($resultgasto);
?>

<html>
   <head>
      <title>Tabela poker</title>
      <link rel="stylesheet" type="text/css" href="css/home.css">
   </head>
   <body>
 
         <tr>
            <td bgcolor="darkgreen">Gasto</td>
            <td bgcolor="#FF6347"><?php echo $resultGasto_query['buy']; ?></td>
         </tr>



<?php

$dsn = "mysql:dbname=poker;host=localhost";
$dbuser = "root";
$dbpass = "";


try {
	$pdo = new PDO($dsn, $dbuser, $dbpass);
	
} 

catch(PDOExeption $e) {
	echo "falhou: ".$e->getMessage();
}

?>


( ! ) Warning: mysqli_query() expects parameter 1 to be mysqli, object given in C:\wamp\www\poker\home\index.php on line 7
Call Stack
#	Time	Memory	Function	Location
1	0.0003	135360	{main}( )	...\index.php:0
2	0.0040	142680	mysqli_query ( )	...\index.php:7

( ! ) Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in C:\wamp\www\poker\home\index.php on line 8
Call Stack
#	Time	Memory	Function	Location
1	0.0003	135360	{main}( )	...\index.php:0
2	0.0555	143080	mysqli_fetch_assoc ( )	...\index.php:8





    <?php
    $sql = "SELECT * FROM home ORDER BY id_home";
    $sql = $conexao->query($sql);
  
    if($sql->rowCount() > 0) {
       foreach ($sql->fetchAll() as $home) {
        
        echo '<tr>';
        echo '<td>'.$home['id_home'].'</td>';
        echo '<td>'.$home['buy'].'</td>';
        echo '<td>'.$home['premio'].'</td>';
        echo '<td>'.$home['torneio'].'</td>';
        echo '<td>'.$home['jogadores'].'</td>';
        echo '<td>'.$home['saldo'].'</td>';
        echo '<tr>';
       }
       
    }

<?php
require_once 'config.php';
?>

<?php
$gasto = "select sum(buy) as buy from home;";
$resultgasto = mysqli_query($pdo, $gasto);
$resultGasto_query = mysqli_fetch_assoc($resultgasto);
?>

<html>
   <head>
      <title>Tabela poker</title>
      <link rel="stylesheet" type="text/css" href="css/home.css">
   </head>
   <body>

         <tr>
            <td bgcolor="darkgreen">Gasto</td>
            <td bgcolor="#FF6347"><?php echo $resultGasto_query['buy']; ?></td>
         </tr>



<?php

$dsn = "mysql:dbname=poker;host=localhost";
$dbuser = "root";
$dbpass = "";


try {
    $pdo = new PDO($dsn, $dbuser, $dbpass);

} 

catch(PDOExeption $e) {
    echo "falhou: ".$e->getMessage();
}

?>

( ! ) Warning: mysqli_query() expects Parameter 1 to be mysqli, Object Given in C: wamp www poker home index.php on line 7 Call Stack # Time Memory Function Location 1 0.0003 135360 {main}( ) ... index.php:0 2 0.0040 142680 mysqli_query ( ) ... index.php:7

( ! ) Warning: mysqli_fetch_assoc() expects Parameter 1 to be mysqli_result, null Given in C: wamp www poker home index.php on line 8 Call Stack # Time Memory Function Location 1 0.0003 135360 {main}( ) ... index.php:0 2 0.0555 143080 mysqli_fetch_assoc ( ) ... index.php:8

Fatal error: Call to Undefined method mysqli_result::rowCount() in C: wamp www poker home index.php on line 126 Call Stack

Time Memory Function Location

1 0.0002 135728 {main}( ) ... index.php:0

1 answer

0


in my case I am using a file that already bring the connection to the bank

For starters, include and require have significant differences. In your case, assuming you are loading a configuration, should wear require_once.

Recommended reading: Sopt - What to use require/include/require_once/include_once?

Continuing...

select+query+Row is showing error

The function mysqli_query() wait for 2 parameters to be passed: query in itself.

In the examples you saw, this variable $conn is, normally, the connection to the database. Therefore it should be passed as first parameter in function mysqli_query().

How are you "bringing the connection in another file", You must have this file something like this:

config.php:

<?php
$conexao = mysqli_connect(
    'localhost',
    'usuario_do_db',
    'senha_do_db',
    'nome_do_db'
);

Now, to finish, fix your code:

<?php
require_once('config.php');

$gasto = "select sum(buy) as buy from home;";  // Estava faltando delimitar (;)
$resultgasto = mysqli_query($conexao, $gasto);
$resultGasto_query = mysqli_fetch_assoc($resultgasto);
?>

Down in the code you are searching for the key "buy":

<?php <td bgcolor="#FF6347">.$$resultGasto_query['buy'].</td>?>

To better understand, see the differences between mysqli_fetch_assoc() and mysqli_fetch_row().

How are you trying to return value by key buy, associative matrix should be used. Note that in query I requested to be returned as "buy": sum(buy) as buy.

Another thing! The PHP will not render the table cell with this syntax! You can do this in several ways. I will demonstrate some in order to correct this row:

<td bgcolor="#FF6347"><?php echo $resultGasto_query['buy']; ?></td>
<td bgcolor="#FF6347"><?= $resultGasto_query['buy']; ?></td>
<?php echo '<td bgcolor="#FF6347">'.$resultGasto_query['buy'].'</td>'; ?>
<?= '<td bgcolor="#FF6347">'.$resultGasto_query['buy'].'</td>'; ?>
<?php echo "<td bgcolor=\"#FF6347\">$resultGasto_query['buy']</td>"; ?>

In short: <td bgcolor="#FF6347">...</td> is HTML; $resultGasto_query['buy'] is PHP, so it should stay between the tags of PHP: <?php ?> or <?= ?> (echo abbreviated).

If you followed the tips correctly, your code should work. If not, remember to edit the question and post "exactly the error message returned by PHP", instead of:

is showing error.

  • Lipe, thank you so much for the answer, I’m still new to programming, but your answers were very clear, and quite accurate. I read about the topics you sent me and I already made the adjustments changed the error and now it can be by the way that this mounted my config file. I will send you the error and the file config.

  • Glad you like it, @Gabrielalves! Edit the question and post the error and the part of the code that generates the error.

  • edited my question, see if this right as I did now please.

  • @Gabrielalves, notice you’re mixing mysqli with PDO. You’re wrong, Uai! Use the connection as mentioned here in the reply, with mysqli_connect().

  • Lipe, it worked by changing the connection to that format, the problem now and that I was already calling some select in the rest of the code with Pdo, and now generates error there, I will post in the code the excerpt.

  • So now just go correcting your code. Just remember not to mix the PDO with the Mysqli... It would be nice if you used one or the other, but nothing against you using both... Mysqli is interesting to have structured functions and object orientation. PDO is only object oriented...

Show 1 more comment

Browser other questions tagged

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