0

I have a function that is supposed to give me a staff members name from his ID number so my php is like this

$staffId = $_GET['staff_id'];
$staff = staff_load($staffId);

and my function is like this

function staff_load()
{
$dbh = dbh_get(); //connects to database

$sql = 'select user_name from user_staff where user_id = ?';
$stmt = $dbh->prepare($sql);
$stmt->execute();
$staff = $stmt->fetch();

dbh_free($dbh); //disconnects from database
return $staff;
}

But when I try and use the $staff variable, it shows nothing. I can't work out what I'm doing wrong. I've tried a bunch of variants and gotten nowhere except frustrated.

<td>Book for ' . $staff . '</td>
11
  • 2
    Your function definition (function staff_load()) needs to accept the value passed to it (function staff_load($id)), and bind it in the SQL query Commented Jan 23, 2017 at 9:24
  • 2
    function staff_load() takes no parameter, it should be function staff_load($id) and then inside your execute() add execute([$id]) Commented Jan 23, 2017 at 9:24
  • 1
    You should be getting errors from this code. Look at your error log Commented Jan 23, 2017 at 9:25
  • 1
    You ->prepare() a query, but you dont bind the parameter to it Commented Jan 23, 2017 at 9:26
  • 1
    Add error reporting to the top of your file(s) while testing right after your opening PHP tag for example <?php error_reporting(E_ALL); ini_set('display_errors', 1); to see if it yields anything. Commented Jan 23, 2017 at 9:26

2 Answers 2

3

First, you forgot to include the parameter in your function definition:

function staff_load($id) {

Then you need to bind the parameter of the prepared statement.

$sql = 'select user_name from user_staff where user_id = $1';
$stmt = $dbh->prepare($sql);
$stmt->execute(array($id));

Next, fetch() returns an array, you need to extract the user_name element from the array:

$row = $stmt->fetch();
if ($row) {
    $staff = $row['user_name'];
} else {
    $staff = false;
}
Sign up to request clarification or add additional context in comments.

9 Comments

changed function to this but still nothing function staff_load($staffId) { $dbh = dbh_get(); $sql = 'select user_name from user_staff where user_id = :staffId'; $stmt = $dbh->prepare($sql); $stmt->execute(); $stmt->bindParam(':staffId', $staffId); $row = $stmt->fetch(PDO::FETCH_ASSOC); if ($row) { $staff = $row['user_name']; } else { $staff = false; } dbh_free($dbh); return $staff; }
Are there any errors? Have you enable PDO's error signalling? Can you confirm that you're using PDO as I assumed?
I assumed it was PDO because $stmt->fetch() is the syntax for that API. What API are you using?
can it be written without api? I'm not a programmer, but doesn't have pdo in other functions. It's just php to postgresql database
this is a statement that works function user_options() { $dbh = dbh_get(); $options = ''; $sql = 'select user_id, user_name from user_staff where user_id>7 ORDER BY user_name'; $stmt = $dbh->prepare($sql); $stmt->execute(); while (true) { $r = $stmt->fetch(); if (is_bool($r)) break; $options .= '<option value="' . $r['user_id'] . '">' . $r['user_name'] . '</option>'; } dbh_free($dbh); return $options; }
|
0

first of all you have to pass parameter to staff_load function

function staff_load($staffId)

secondly you should bind parameter

$sql = 'select user_name from user_staff where user_id = :user_id';
$stmt = $dbh->prepare($sql);
$stmt->bindParam(':user_id', $staffId);

1 Comment

user_id should equal the $staffId value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.