简体   繁体   中英

Returning variables from a prepared statement function PHP

I have multiple pages on a website that require the same data from a table. So instead of writing out an sql statement on each page I want to write a function.

Here is my function:

function getInfo($u_id) {

    global $conn;
    $stmt = $conn->prepare("SELECT report.*, users.* FROM report INNER JOIN users ON report.report_user_id=users.user_id WHERE report_user_id = ? ORDER BY report_id DESC LIMIT 5") or die ('Problem Preparing Query');
    $stmt->bind_param("i", $u_id);
    $stmt->execute();

    $result = $stmt->get_result();
    while($row = $result->fetch_assoc()) {

        //reports
        $report_user_id     = $row['report_user_id'];   
        $rep_date           = $row['rep_date'];
        $rep_location       = $row['rep_location'];
        $rep_notes          = $row['rep_notes'];

        //users 
        $user_id            = $row['user_id'];
        $username           = $row['username'];
        $fname              = $row['user_firstname'];  
        $lname              = $row['user_lastname'];
        $user_email         = $row['user_email'];
        $user_image         = $row['user_image'];
        $user_number        = $row['user_number']; 
    }
    $stmt->close();
}

The function works, in that it fetches the data from the table but I don't know how to output the variables. Any example I have seen using prepared statements in functions are for inserting data into a table rather than fetching data.

I have tried binding the results, creating an array, returning each variable but in every case I have errors or can only return the first variable.

The function as is does not throw up any errors on its own but further down my webpage when I try to use one of the fetched variables it tells me that it is undefined.

 $u_id = 1;
 getInfo($u_id);

<a class="text-inherit" href="profile/index.html"><?php echo $fname .' '. $lname; ?></a>


Notice: Undefined variable: fname in....

So my question is, how can I get the variables that have been fetched in this function?

Of course it's not working since $fname and $lname are local variables in that function and when the function returns they are gone.

Put all of them inside an array and either return them or put them inside $GLOBAL .

function getInfo($u_id) {
    global $conn;

    $stmt = $conn->prepare("SELECT report.*, users.* FROM report INNER JOIN users ON report.report_user_id=users.user_id WHERE report_user_id = ? ORDER BY report_id DESC LIMIT 5") or die ('Problem Preparing Query');
    $stmt->bind_param("i", $u_id);
    $stmt->execute();
    $rows = [];
    $result = $stmt->get_result();

    while($row = $result->fetch_assoc()) {
        $rows[] = $row;
    }

    $stmt->close();   
    return $rows;
}

Here is a single row result solution:

function getInfo($u_id){
  global $conn;
  $stmt = $conn->query("SELECT * FROM report INNER JOIN users ON report.report_user_id=users.user_id WHERE report.report_user_id=$u_id ORDER BY DESC LIMIT 1") || die ('Problem Preparing Query');
  if($stmt->num_rows > 0){
    return $stmt->fetch_object();
  }
  else{
    // no results were found
  }
  return false;
}
$obj = getInfo($u_id);
if($obj){
  echo "<a class='text-inherit' href='profile/index.html'>{$obj->user_firstname} {$obj->user_lastname}</a>";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM