0

So, I'm writing an application in PHP and I'm trying to retrieve unique user data based on what they entered. Here is my code:

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.");
$student1name; 
$query3 = mysqli_query($con3,"SELECT * FROM 'users' WHERE 'username' = '$username123' and '$student1name' = 'student1'");
return "$student1name";

}

So, the user enters there username which they registered earlier on and then I run a query where the username field is equal to the username variable (The input) and that the student1name variable is equal to the student1 field where the username is the same as the one entered. I then return the student1name variable. But when I test this all that returns is "". I can't figure out the problem

4

3 回答 3

0

PHP 会自动将您在字符串中使用的任何变量替换为该变量的值。例如,如果 $student1name = 'foo' 则将 $student1name 放在 SELECT 语句中的任何位置都会输出一个字符串 'foo' 来代替该变量。就 mysqli 语句而言,您没有使用 PHP 值,而是使用了文字字符串 'foo'。因此,期望您的 $student1name var 根据 sql 语句神奇地改变是不会发生的。

要检索列的值,您需要在执行查询后获取结果行:

if ($row = $query3->fetch_assoc()) {
  return $row["student1"];
}

如果您期望多个结果行,那么您只需要为每一行再次调用 fetch_assoc() 直到它没有返回任何内容(标记结果行的结尾)。

于 2013-07-15T22:14:55.690 回答
0

您没有获取查询结果,请在查询后尝试类似的操作..

$row = mysqli_fetch_array($query3, MYSQLI_ASSOC);

return $row['studentname'];

我假设学生的名字在你的数据库中名为 studentname 的列中

于 2013-07-15T21:58:05.947 回答
0

您必须将结果绑定到结果数组中并像这样访问它,这样的事情应该可以工作:

return $query3["student1name"];

应该会更有效。

如果您只在查询中选择一个变量,那么您可以简单地回显 $query3。

如果您查看准备好的陈述,它也会更有效。

注意

$student1name

没有做任何事情,因为你从来没有给它一个适当的价值。

于 2013-07-15T21:59:26.050 回答