从mysql数据库回显特定的行 [英] Echo specific rows from a mysql database
问题描述
我创建了一个表格,要求用户添加:名,姓,位置,状态
I have created a form that ask the user to add: first_name, last_name,location,status
一段时间后,我收到了5个输入.mysql表被命名为users,表数据如下:
After some time i have received 5 inputs. The mysql table is named users and the table data are like below:
id first_name location status
== ========== ======== ========
1 Chris UK Married
2 Anton Spain Single
3 Jacob UK Single
4 Mike Greece Married
5 George UK Married
我也是通过POST方法接收输入的方式.所以:
Also the way i am receiving the inputs its via POST method. So:
$firstname=$_POST['FIRST_NAME']; // First Name: <input type="text" name="FIRST_NAME">
$location=$_POST['LOCATION']; // Location: <input type="text" name="LOCATION">
$status=$_POST['STATUS']; // Status: <input type="text" name="STATUS">
我创建了一个查询,以选择来自英国的所有已婚用户:
I created a query to select all users from UK that are married:
$query = "SELECT * FROM users WHERE location='UK' AND status='Married'";
$result = mysqli_query($dbc,$query); //$dbc is the connection to my database
$row = mysqli_fetch_array($results, MYSQLI_BOTH);
换句话说:
id first_name location status
== ========== ======== ========
1 Chris UK Married
5 George UK Married
问题:
1)$ row数组是否如下图所示:
$row= array(
array(1 Chris UK Married),
array(5 George UK Married)
);
2)在实施WHERE location ='UK'AND status ='Married'过滤之后,如何回显数据库的内容?
我需要这样:
Hello Chris! You are from UK and you are married!
Hello George! You are from UK and you are married!
我知道我必须使用foreach循环(回声数组),但是我已经尝试过了,但是它不起作用.我尝试过的事情之一是在php.net中找到的东西:
I know that i have to use foreach loop (echo arrays) but i have tried it and it doesnt work.One of the things i tried was something i found in php.net:
使用list()打开嵌套数组的包装
Unpacking nested arrays with list()
(PHP 5> = 5.5.0)
(PHP 5 >= 5.5.0)
PHP 5.5通过提供list()作为值,增加了对数组数组进行迭代并将嵌套数组解压缩为循环变量的功能.
PHP 5.5 added the ability to iterate over an array of arrays and unpack the nested array into loop variables by providing a list() as the value.
例如:
<?php
$array = [
[1, 2],
[3, 4],
];
foreach ($array as list($a, $b)) {
// $a contains the first element of the nested array,
// and $b contains the second element.
echo "A: $a; B: $b\n";
}
?>
当我使用上面的代码时,我收到以下错误:
When i use the above i am receiving the error below:
Parse error: syntax error, unexpected T_LIST in C:\wamp\www....
有什么建议吗?
据我了解,我必须以某种方式将ID与其他数据相关联.
As i understand, i somehow have to link the ID with the other data..
谢谢.
推荐答案
您可以使用类似这样的东西:
You may use something like this:
$query = "SELECT * FROM users WHERE location='UK' AND status='Married'";
$result = mysqli_query($dbc,$query);
while($row = mysqli_fetch_array($result, MYSQLI_BOTH)){
printf("Hello %s! You are from %s and you are %s!\n", $row['first_name'], $row['location'],$row['status']);
}
这篇关于从mysql数据库回显特定的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!