gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:54:39 24 4
gpt4 key购买 nike

我有 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;
}

最佳答案

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

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."')");

关于php - 获取在 PHP 函数中声明的变量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26953564/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com