使用PHP和MYSQLI搜索数据库以获得匹配结果

时间:2022-09-10 19:26:04

I am kind of new to all of this trying to figure out why things work and why things don't.

我对这一切都很陌生,试图弄清楚为什么事情有效,为什么事情没有。

So I was aiming for a simple search form to display all of the database users with the same first name.

所以我的目标是一个简单的搜索表单来显示所有具有相同名字的数据库用户。

This code outputs all the names of the users in my table "users" and it works.

此代码输出我的表“users”中的所有用户名,并且它可以正常工作。

<?php
include 'connect.php'; //working connection to the DB


$sql="SELECT name FROM users ORDER BY name ASC";

$sqlresult=mysqli_query($con,$sql);

$afct=mysqli_affected_rows($con);

while($user=mysqli_fetch_array($sqlresult,MYSQLI_ASSOC)){ 
    $num[]=$user['name'];
    $num++;
}

$i=1;

while($i < $afct)
{
    echo $i.': '.$num[$i];
    echo'<br>';
    $i++;

}
?>

So now I wanted to add a variable that stores the user input, to match with the users first name.

所以现在我想添加一个存储用户输入的变量,以匹配用户的名字。

<?php
include 'connect.php';    //working connection to the DB

$input = 'Marcus';       // later will be $input = $_GET(['name']);

$sql="SELECT name FROM users WHERE name='".$input."'  ORDER BY name ASC"; 

//tried with "SELECT * FROM users WHERE name='".$input."'" ORDER BY name ASC";
//also

$sqlresult=mysqli_query($con,$sql);

$afct=mysqli_affected_rows($con);

while($user=mysqli_fetch_array($sqlresult,MYSQLI_ASSOC)){ 
    $num[]=$user['name'];
    $num++;
}

$i=1;

while($i < $afct)
{
    echo $i.': '.$num[$i];
    echo'<br>';
    $i++;

}
?>

And all it dose is output a blank and sexy page. Thank You in Advance.

而它所有的剂量输出一个空白和性感的页面。先感谢您。

//Marcus

2 个解决方案

#1


0  

The problem is that you initialize $i to 1. Array indexes in PHP start at 0, just like almost every other programming language. Since your second query only returns 1 row, the only element will be $num[0]. So you should initialize $i to 0.

问题是你将$ i初始化为1. PHP中的数组索引从0开始,就像几乎所有其他编程语言一样。由于您的第二个查询只返回1行,因此唯一的元素是$ num [0]。所以你应该将$ i初始化为0。

Or use foreach:

或者使用foreach:

foreach ($num as $i => $name) {
    echo "$i: $name<br>";
}

Your first script was also wrong, you just didn't notice that it skipped one of the users in the table.

你的第一个脚本也错了,你只是没注意到它跳过了表中的一个用户。

#2


0  

i think that the second while didn't run because of $i=1 try with while($i <= $afct)

我认为第二次没有运行因为$ i = 1尝试while($ i <= $ afct)

#1


0  

The problem is that you initialize $i to 1. Array indexes in PHP start at 0, just like almost every other programming language. Since your second query only returns 1 row, the only element will be $num[0]. So you should initialize $i to 0.

问题是你将$ i初始化为1. PHP中的数组索引从0开始,就像几乎所有其他编程语言一样。由于您的第二个查询只返回1行,因此唯一的元素是$ num [0]。所以你应该将$ i初始化为0。

Or use foreach:

或者使用foreach:

foreach ($num as $i => $name) {
    echo "$i: $name<br>";
}

Your first script was also wrong, you just didn't notice that it skipped one of the users in the table.

你的第一个脚本也错了,你只是没注意到它跳过了表中的一个用户。

#2


0  

i think that the second while didn't run because of $i=1 try with while($i <= $afct)

我认为第二次没有运行因为$ i = 1尝试while($ i <= $ afct)