Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Dell

Warning: mysqli_query() expects parameter 1 to be mysqli

Recommended Posts

Sou iniciante no PHP e estou aprendendo.

Quando montei esse código tudo funcionou, o erro abaixo foi quando tentei fazer duas consultas no banco.

<?php
$host = "127.0.0.1";
$dbuser = "root";
$dbpass = "123";
$dbdatabasename = "teste";

$rs = mysqli_connect ( $host, $dbuser, $dbpass ) or die ( "Could not connect database" );
mysqli_select_db ( $rs, $dbdatabasename ) or die ( "Could not select database" );

$mes = strftime ( "%m", time () );

$totalentrada = "0";
$i = "0";
function selectmes($txt) {
	switch ($txt) {
		case 1 :
			$mess = "Janeiro";
			break;
		case 2 :
			$mess = "Fevereiro";
			break;
		case 3 :
			$mess = "Março";
			break;
		case 4 :
			$mess = "Abril";
			break;
		case 5 :
			$mess = "Maio";
			break;
		case 6 :
			$mess = "Junho";
			break;
		case 7 :
			$mess = "Julho";
			break;
		case 8 :
			$mess = "Agosto";
			break;
		case 9 :
			$mess = "Setembro";
			break;
		case 10 :
			$mess = "Outubro";
			break;
		case 11 :
			$mess = "Novembro";
			break;
		case 12 :
			$mess = "Dezembro";
			break;
	}
	
	return $mess;
}

$entradahtml = "<table border='0' cellpadding='4' width='100%'>";

	

if ($query_entrada = mysqli_query ( $rs, "SELECT * FROM entrada where Month(data)='$mes'" )){
	
	
	while ( $rs = mysqli_fetch_array ( $query_entrada ) ) {
		
		if ($i % 2 == 1) {
			$cor = "#f3f3f3";
		} else {
			$cor = "#ffffff";
		}
		
		$entradahtml .= "	<tr>";
		$entradahtml .= "	<td><font face=arial size=1><b>" . strftime ( "%d", strtotime ( $rs ["data"] ) ) . " de " . selectmes ( strftime ( "%m", strtotime ( $rs ["data"] ) ) ) . "</b></font></td>";
		$entradahtml .= "	<td></td>";
		$entradahtml .= "	</tr>";
		
		$entradahtml .= "	<tr>";
		$entradahtml .= "	<td bgcolor='.$cor.'>" . $rs ["descricao"] . "</td>";
		
		$entradahtml .= "	<td bgcolor='.$cor.' ><a href='default.php?opcao=excluir_entrada&id=" . $rs ["id"] . "><font color=red>x</font></a> <font face=arial size=2>" . substr ( $rs ["descricao"], 0, 30 ) . "...</font></td>";
		
		$entradahtml .= "	<td  bgcolor='.$cor.' align='center' width='70'><font face=arial size=2>" . number_format ( $rs ["valor"], 2 ) . "</font></td>";
		$entradahtml .= "	</tr>";
		
		$entradahtml .= "</table>";
		
		$i ++;
	}
}

echo $entradahtml;






Se eu apagar daqui até  o final a parte de cima funciona, mas quando coloco essa segunda parte provoca o erro. Mas se reparar o código é idêntico eu copie e colei, troquei apenas os comandos necessários.
Os dois códigos funcionam isolados, juntos na mesma página não.
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV






$saidahtml = "<table border='0' cellpadding='4' width='100%'>";



if ($query_saida = mysqli_query ( $rs, "SELECT * FROM saida where Month(data)='$mes'" )){


	while ( $rs = mysqli_fetch_array ( $query_saida ) ) {

		if ($i % 2 == 1) {
			$cor = "#f3f3f3";
		} else {
			$cor = "#ffffff";
		}

		$saidahtml .= "	<tr>";
		$saidahtml .= "	<td><font face=arial size=1><b>" . strftime ( "%d", strtotime ( $rs ["data"] ) ) . " de " . selectmes ( strftime ( "%m", strtotime ( $rs ["data"] ) ) ) . "</b></font></td>";
		$saidahtml .= "	<td></td>";
		$saidahtml .= "	</tr>";

		$saidahtml .= "	<tr>";
		$saidahtml .= "	<td bgcolor='.$cor.'>" . $rs ["descricao"] . "</td>";

		$saidahtml .= "	<td bgcolor='.$cor.' ><a href='default.php?opcao=excluir_entrada&id=" . $rs ["id"] . "><font color=red>x</font></a> <font face=arial size=2>" . substr ( $rs ["descricao"], 0, 30 ) . "...</font></td>";

		$saidahtml .= "	<td  bgcolor='.$cor.' align='center' width='70'><font face=arial size=2>" . number_format ( $rs ["valor"], 2 ) . "</font></td>";
		$saidahtml .= "	</tr>";

		$saidahtml .= "</table>";

		$i ++;
	}
}

echo $saidahtml;
mysqli_close($rs);

?>

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /var/www/financeiro/index.php on line 97

 

Parte do código linha 97

if ($query_saida = mysqli_query ( $rs, "SELECT * FROM saida where Month(data)='$mes'" )){

 

Warning: mysqli_close() expects parameter 1 to be mysqli, null given in /var/www/financeiro/index.php on line 128

Parte do código linha 128
mysqli_close($rs);
Porque está dando o erro, se os códigos isolados funcionam?

Compartilhar este post


Link para o post
Compartilhar em outros sites

Pelo o que eu vi, nessa parte:

while ( $rs = mysqli_fetch_array ( $query_entrada ) )

você troca o valor de $rs, o que faz com que a partir dessa linha $rs não possa ser usado como parâmetro do mysqli. Exceto se você fizer isso de novo após o while:

$rs = mysqli_connect ( $host, $dbuser, $dbpass ) or die ( "Could not connect database" )

Compartilhar este post


Link para o post
Compartilhar em outros sites

troque isso

Se eu apagar daqui até  o final a parte de cima funciona, mas quando coloco essa segunda parte provoca o erro. Mas se reparar o código é idêntico eu copie e colei, troquei apenas os comandos necessários.
Os dois códigos funcionam isolados, juntos na mesma página não.
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV

por isso

mysqli_close($rs);

kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk

 

testa aeee

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.