因此,我在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)..有谁知道这个问题?谢谢!
答案 0 :(得分: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中,然后将其返回。
答案 1 :(得分: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));
}