[英]What is wrong with my sql query? (PHP)
What is wrong with this code? 此代码有什么问题? Why can't I get the
username
from the members
table in website
database. 为什么我不能从
website
数据库的members
表中获取username
。 Here's the screenshot of database information: 这是数据库信息的屏幕截图:
I am trying to make a function for it so that I can use it later. 我正在尝试为其创建一个函数,以便以后使用。 Here's the code:
这是代码:
<?php
function get_username($username){
$host = 'localhost';
$username = 'root';
$password = '';
$database = 'website';
$con = mysqli_connect($host, $username, $password, $database);
$query = "SELECT * FROM members WHERE username = '{$username}'";
$result = mysqli_query($con, $query);
$row = mysqli_fetch_array($result);
return $row['name'];
mysqli_close($con);
}
echo get_username('iamfaizahmed');
?>
Your username is overwritten in the function 您的用户名在函数中被覆盖
$username = 'root'; // here
So no matter what you pass in the function argument it will always use 因此,无论您在函数参数中传递什么,它都将始终使用
where username = 'root'
So use a different variable name 因此,请使用其他变量名称
you are mixing between $username
of the function and $username
of your connect variable 你之间的混合
$username
的功能和$username
的连接变量
change this 改变这个
function get_username($username){
to 至
function get_username($user){
and do your query like that 然后像这样查询
$query = "SELECT * FROM members WHERE username = '$user'";
First thing, your $username
variable. 首先,您的
$username
变量。
You're passing it through the function and also using it for db connection. 您正在通过函数传递它,并且还将它用于数据库连接。
Change your dbusername in the function as $dbusername
在函数中将您的dbusername更改为
$dbusername
Second, the query. 第二,查询。 Keep it as:
保持为:
$query = "SELECT * FROM members WHERE username = '$username'";
Now, once you're done with the basics, I recommend you to visit this link: Click Here 现在,一旦您掌握了基础知识,我建议您访问此链接: 单击此处
That should get your somewhere! 那应该让你到某个地方!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.