如何使用php和mysql从数据库中获取用户给定输入的值?
function getStudent($id){
global $connection;
$query="select * Student where id={$id}";
$result=mysql_query($query);
if($result){
while($row=$result->fetch_assoc()){
echo "Name : ".$row["name"]."<br> Age : ".$row["age"] ;
}
}
return $result;
}
请查找并修正错误
答案 0 :(得分:2)
请在查询中添加“发件人”
$query="select * from Student where id='".$id."'";
答案 1 :(得分:1)
试试这样:
function getStudent($id){
global $connection;
$query = "select * from Student where id='".$id."'"; //use single quotes.
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){ //use mysql_fetch_array for retrieving data
echo "Name : ".$row["name"]."<br> Age : ".$row["age"] ;
}
return $result;
}
答案 2 :(得分:0)
试试这个,
function getStudent($id){
global $connection;
$query="select * from Student where id={$id}";
$result=mysql_query($query);
while($row=mysql_fetch_array($result)){
$res[] array("Name"=>$row["name"], "Age"=>$row["age"] ;
}
return $res;
}
答案 3 :(得分:0)
您正在编写的查询中存在语法错误
$query="select * Student where id={$id}";
应该是:
$query="select * from Student where id='".$id."'";
在映射数据库列时,看到您将它映射到正确的列I,e列名称在后端和前端都应该相同。
答案 4 :(得分:0)
mysql_query()在成功时返回资源,如果出错则返回FALSE。如果要返回整个结果而不是返回fetch_assoc()的结果。像
if($result){
while($row=$result->fetch_assoc()){
$data[] .= $row;
}
}
返回此$ data变量。