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
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.
– Gabriel Alves
Glad you like it, @Gabrielalves! Edit the question and post the error and the part of the code that generates the error.
– LipESprY
edited my question, see if this right as I did now please.
– Gabriel Alves
@Gabrielalves, notice you’re mixing
mysqli
withPDO
. You’re wrong, Uai! Use the connection as mentioned here in the reply, withmysqli_connect()
.– LipESprY
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.
– Gabriel Alves
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...
– LipESprY