Jump to content
Sign in to follow this  
Moneybag

MySQLI fecth_assoc

Recommended Posts

Moneybag

Boas malta

 

Tou com um problema no meu código e não consigo listar uma tabela de uma base de dados MySql  nesta função de PHP

 

o seguinte código  continua a dar echo de "0 results":

function showLog($table, $mysqli)
{
	if ($stmt = $mysqli->prepare("SELECT id, log_date, log_text FROM $table")) {
        $stmt->execute();    
        $result = $stmt->get_result();
		
		if ($stmt->num_rows > 0) {
			while($row = $result->fetch_assoc()) {
			//echo "LOG ID: (" . $row["id"].") Date: ". $row["log_date"] . "│ TEXT: " . $row["log_text"]. "<br>";
			}
		}else
		{
			echo "0 results";
		}
		
		$stmt->close();	
	}
}

 

 

obrigado pela atenção

 

Edited by Ascensao

Share this post


Link to post
Share on other sites
Moneybag

entretanto encontrei a resposta no stackoverflow.

 

Quote

That's because fetch_assoc is not part of a mysqli_stmt object. fetch_assoc belongs to the mysqli_result class. You can use mysqli_stmt::get_result to first get a result object and then call fetch_assoc:

$selectUser = $db->prepare("SELECT `id`,`password`,`salt` FROM `users` WHERE `username`=?"); 
$selectUser->bind_param('s', $username); 
$selectUser->execute(); 
$result = $selectUser->get_result(); 
$assoc = $result->fetch_assoc();
$selectUser = $db->prepare("SELECT `id`,`password`,`salt` FROM `users` WHERE `username`=?"); 
$selectUser->bind_param('s', $username); 
$selectUser->bind_result($id, $password, $salt);
$selectUser->execute(); 
while($selectUser->fetch()) 
{ 
//$id, $password and $salt contain the values you're looking for
}

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.