Homer_J
Homer_J

Reputation: 3323

PHP Function - what am I missing?

SOLVED - USER ERROR - helps if one declares a variable doesn't it...

Help,

I have the following PHP Function code:

function new_respondent() {
    global $link;
    $proc = mysqli_prepare($link, "INSERT INTO trespondent_bps (code) VALUES (uuid());");
    mysqli_stmt_execute($proc);
    $respondent_id = mysqli_insert_id($link);
    mysqli_stmt_fetch($proc);
    mysqli_stmt_close($proc);
    mysqli_clean_connection($link);
    return($code);
}

For the life of me, it's adding the UUID into the database no problems at all - but it's not returning it so I can use it in the code - what am I missing/ doing wrong!!!

Thanks in advance,

Homer.

Upvotes: 0

Views: 168

Answers (3)

Pheonix
Pheonix

Reputation: 6052

you are forgetting the mysqli_stmt_bind_result($code);

    function new_respondent() {
        global $link;
        $proc = mysqli_prepare($link, "INSERT INTO trespondent_bps (code) VALUES (uuid());");
        mysqli_stmt_execute($proc);
        $respondent_id = mysqli_insert_id($link);
mysqli_stmt_bind_result($code);
        mysqli_stmt_fetch($proc);
        mysqli_stmt_close($proc);
        mysqli_clean_connection($link);
        return($code);
    }

Upvotes: 1

max4ever
max4ever

Reputation: 12140

try this

function new_respondent() {
    global $link;
    $code = uniqid();

    $proc = mysqli_prepare($link, "INSERT INTO trespondent_bps (code) VALUES (" . $code .");");
    mysqli_stmt_execute($proc);
    $respondent_id = mysqli_insert_id($link);
    mysqli_stmt_fetch($proc);
    mysqli_stmt_close($proc);
    mysqli_clean_connection($link);
    return($code);
}

note that i didn't escape it since it's safe

Upvotes: 1

Alfwed
Alfwed

Reputation: 3282

You return a variable $code that is undefined.

function new_respondent() {
    global $link;
    $proc = mysqli_prepare($link, "INSERT INTO trespondent_bps (code) VALUES (uuid());");
    mysqli_stmt_execute($proc);
    $respondent_id = mysqli_insert_id($link);
    mysqli_stmt_fetch($proc);
    mysqli_stmt_close($proc);
    mysqli_clean_connection($link);
    return($respondent_id);
}

Upvotes: 1

Related Questions