2011-10-05 73 views
0

解决 - 用户错误 - 帮助,如果一个声明一个变量,不是么?PHP函数 - 我错过了什么?

帮助,

我有以下的PHP函数代码:

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); 
} 

对于我的生活,它将UUID添加到数据库中根本没有问题 - 但它没有返回,所以我可以在代码中使用它 - 我错过了什么/做错了什么!

在此先感谢,

荷马。

+1

'$ code'是从来没有宣布我没有逃脱它。 – Prisoner

+0

我把它的id字段设置为AUTO INCREMENT? $代码不设置为任何东西,如果你想func返回的ID,不应该是返回($ respondent_id)? – SW4

+0

YUP - 如下 - NUMPTY! –

回答

1

您返回未定义的变量$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($respondent_id); 
} 
+0

RUDDY HELL !!!有时,你看不到看!谢谢! –

1

试试这个

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); 
} 

注意,因为它的安全

1

你就忘记了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); 
    }