Jump to content

Recommended Posts

Hello I'm trying to set up a user area for my site where it displays the current logged in users ranking and other information in the future.  

<?
 ini_set('display_errors', 1);
require_once "header.php";

$sql = "SELECT * FROM users WHERE username = ?";

  if($stmt = mysqli_prepare($link, $sql)){
  
    mysqli_stmt_bind_param($stmt, 's', $_SESSION['username']);
  
   if(mysqli_stmt_execute($stmt)){
                
   
   $info = mysqli_fetch_array($stmt);
   
   echo "Current rank:" . $info['rank'];
   
   
   } else {
   
   echo "Can't find user";
   }
   }
   mysqli_stmt_close($stmt);





?>

That's the code I currently have but it gives me the error "but get an error message of mysqli_fetch_array() expects parameter 1 to be mysqli_result"

@ginerjm https://www.php.net/manual/en/mysqli-result.fetch-array.php that's what I found about the fetch array on PHP.net. so for the results var it would be something like  like "$results = mysqli_store_result($stmt) or am I misunderstanding?

fetching data from a mysqli prepared query doesn't work the way you are used to for fetching data from a query. you will need to consult the documentation for the extra code necessary, or you can switch to the much simpler PDO database extension, which does work for fetching the data from a prepared query in the way you are used to.

also, if you use exceptions for database statement errors, and in most cases let php catch and handle the exception, you won't have to add logic (or can remove the logic you have now) that's testing the result of the prepare() and execute() statements, simplifying the code.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.