获取PHP函数中声明的变量的值

时间:2014-11-16 03:53:49

标签: php mysql function mysqli

我有2页,“signup.php”和“globalfunctions.php”。在signup.php上,我从表单提交中获取所有信息,我将密码哈希(通过附加在globalfunctions.php中生成的随机字符串),然后使用我定义的函数executeSQL

signup.php:

include('/home/www/portaldev.samgoodman.co/processes/globalfunctions.php');
$singleAppendString = generateRandomAppend(16);
$form_email = $_POST['email'];
$form_password = $_POST['password'];
$form_name = $_POST['name'];
$form_school = $_POST['schoolid'];
$form_grad = $_POST['gradyear'];
$form_ip = $_SERVER['REMOTE_ADDR'];
$password_with_hash = $form_password.$singleAppendString;
$hashedPassword = sha1($password_with_hash);

executeSQL("$nextUserQuery", "SELECT id FROM users ORDER BY id DESC LIMIT 0 , 1");

这是我想从数据库中获取$nextUserQuery的值的地方,但我需要在函数中返回值。

executeSQL("$insertUser", "INSERT INTO users (id, name, email, password, school_id, grad_year, lvl, signup_ip) VALUES ('".$calc_userid."', '".$form_name."', '".$form_email."', '".$hashedPassword."', '".$form_school."', '".$form_grad."', '0','".$form_ip."')");
executeSQL("$insertHash", "INSERT INTO vault (id, hash) VALUES ('".$calc_userid."', '".$singleAppendString."')");

globalfunctions.php

function generateRandomAppend($length) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $randomString;
}

function executeSQL($varName, $query) {
    global $varName;
    $con=mysqli_connect("localhost", "hugopak1_spm", "Massavailable1", "hugopak1_spm");
    $varName = mysqli_query($con, $query);
    return $varName;
}

1 个答案:

答案 0 :(得分:0)

您目前所做的并不是获取executeSQL函数返回值的正确方法。从$varName函数

中删除executeSQL参数
function executeSQL($query) {
    $con = mysqli_connect("localhost", "hugopak1_spm", "Massavailable1", "hugopak1_spm");
    $varName = mysqli_query($con, $query);
    return $varName;
}

并声明一个包含返回值的新变量

$nextUserQuery = executeSQL("SELECT id FROM users ORDER BY id DESC LIMIT 0 , 1");

在上面的示例中,$nextUserQuery的值是executeSQL("SELECT id FROM users ORDER BY id DESC LIMIT 0 , 1")返回的值。您应该将相同的内容应用于其他两行,如下所示

$insertUser = executeSQL("INSERT INTO users (id, name, email, password, school_id, grad_year, lvl, signup_ip) VALUES ('".$calc_userid."', '".$form_name."', '".$form_email."', '".$hashedPassword."', '".$form_school."', '".$form_grad."', '0','".$form_ip."')");
$insertHash = executeSQL("INSERT INTO vault (id, hash) VALUES ('".$calc_userid."', '".$singleAppendString."')");