Bring result of a sum on screen

0

I'm trying to create a sum and display it on screen. But almost all of the examples I see use the $conn call and in my case I'm using a file that already has the connection to the database and the query + row p>

Follow 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

    
asked by anonymous 23.12.2018 / 02:21

1 answer

0
  

In my case I'm using a file that already has the connection to the bank

To begin with, include and require have significant differences. In your case, assuming you're loading a configuration , you should use require_once .

  

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

Continuing ...

  

select + query + row is displaying error

The mysqli_query() function expects 2 parameters to be passed: the connection and the query itself.

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

As you are "bringing the connection to another file" , you should 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, correct 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 looking 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 () .

Trying to return the value with the buy key, you should use associative array. Notice that in query I requested that it be returned as "buy": sum(buy) as buy .

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

<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 be between PHP tags: <?php ?> or <?= ?> echo in> 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:

  

You're experiencing an error.

    
23.12.2018 / 06:04