-3

所以,我正在用 PHP 编写这个应用程序,其中用户有一个“学生姓名”,每个用户都有一个唯一的学生姓名。所以,在我进一步解决我的问题之前,这里是代码 *注意我已经阻止了 SQL 注入

    function hello($username123) {
       // Connect to Database //
$host3     = "db"; 
$username3 = "db"; 
$password3 = "db"; 
$db3       = "db";
$con3 = mysqli_connect($host3,$username3,$password3,$db3) or die("Can not connect to Server.");
$query3 = mysqli_query($con3,"SELECT student1 FROM users WHERE username = '$username123'");
$student1name = "$query3";
return $student1name;

所以,这个人输入了事先注册的用户名,每个用户都有一个学生姓名。我开始一个查询,选择 student1,student1 等于 student1name。然后将学生 1 的姓名定义为 query3。当我全部测试时,我得到的只是(null)..有人知道这个问题吗?谢谢!

4

2 回答 2

1

我怀疑你想要的是这样的:

function hello($username123) {
    // Connect to Database //
    $host3     = "db"; 
    $username3 = "db"; 
    $password3 = "db"; 
    $db3       = "db";
    $con3 = mysqli_connect($host3,$username3,$password3,$db3) or die("Can not connect to Server.");
    $query3 = mysqli_query($con3,"SELECT student1 FROM users WHERE username = '$username123'");
    while ($row = mysqli_fetch_array($query3))
    {
        $student1name = $row['student1'];
    }
    return $student1name;

这会将查询的最后一个返回行(列“student1”)的内容放入变量 $student1name 中,然后返回它。

于 2013-07-16T01:13:15.507 回答
0

您没有从结果中获取数据。尝试这个:

function hello($username123) {
    // Connect to Database //
    $host3     = "db"; 
    $username3 = "db"; 
    $password3 = "db"; 
    $db3       = "db";

    $con3 = mysqli_connect($host3,$username3,$password3,$db3)
    if (!$con3)
        throw new Exception("Connection error");

    $result = mysqli_query($con3,"SELECT student1 FROM users WHERE username = '$username123'");
    if ($result)
        return $result->fetch_object();
    else
        throw new Exception("Query error: " . mysqli_error($con3));
}
于 2013-07-16T01:25:32.643 回答