[英]Display users First and last name in plain text from the database and not in array
I have a html page that sends value phone
to log.php: 我有一个html页面,它将价值phone
发送到log.php:
<?php
$PHONE = $_POST['phone'];
mysql_connect('host.com', 'user', 'pass'); mysql_select_db('userdb');
$results = mysql_query(sprintf("SELECT FNAME,LNAME FROM `details` WHERE PHONE='$PHONE'",
mysql_real_escape_string($PHONE))) or die(mysql_error());
while($row = mysql_fetch_assoc($results))
{
$rows[1] = $row;
}
print_r($rows)
?>
I get the result in log.php file in the form of an array from the database- 我从数据库中以数组形式在log.php文件中获取结果-
Array ( [1] => Array ( [FNAME] => Vikrant [LNAME] => Kelkar ) )
I have table 'details' created 我创建了表格“明细”
FNAME-First name FNAME-名字
LNAME-Lastname LNAME-姓氏
BIRTHDAY-Birthday 生日-生日
PHONE- user's no. 电话号码-用户编号
My Question is , I want to just display ' Vikrant Kelkar ' as a result in log.php file , and not the whole array but just the First & Last Name of user. 我的问题是,我只想在log.php文件中显示“ Vikrant Kelkar”,而不是整个数组,而仅显示用户的名和姓。 Any help would be greatfull ? 有什么帮助的吗?
只需访问数组:
echo $rows[0]["FNAME"] . " " . $rows[0]["LNAME"];
you do not need to make it into an array unless you have more than one result. 除非得到多个结果,否则无需将其放入数组。
while($row = mysql_fetch_assoc($results))
{
$full_name = $row['FNAME'].' '.$row['LNAME'];
//$full_name[] = $row['FNAME'].' '.$row['LNAME']; // multi result
}
echo $full_name;
//echo $full_name[0]; // loop thro foreach
think about learning pdo/mysqli as the mysql library will not function in php soon. 考虑学习pdo / mysqli,因为mysql库很快将无法在php中运行。
http://www.php.net/manual/en/book.pdo.php http://www.php.net/manual/zh/book.pdo.php
http://uk1.php.net/manual/en/mysqlinfo.api.choosing.php http://uk1.php.net/manual/zh/mysqlinfo.api.choosing.php
Why you are using while loop. 为什么要使用while循环。 SQL WHERE clause condition is phone number so it will be unique and return only one row.therefore,just remove while loop and do simply the following. SQL WHERE子句条件是电话号码,因此它将是唯一的,并且仅返回一行。因此,只需删除while循环并简单地执行以下操作即可。
<?php
$PHONE = $_POST['phone'];
mysql_connect('host.com', 'user', 'pass'); mysql_select_db('userdb');
$results = mysql_query(sprintf("SELECT FNAME,LNAME FROM `details` WHERE PHONE='$PHONE'",mysql_real_escape_string($PHONE))) or die(mysql_error());
$row = mysql_fetch_assoc($results);
echo $row['FNAME']." ".$row['LNAME'];
?>
You can MySQL's CONCAT function to build the full name when querying the database. 查询数据库时,可以使用MySQL的CONCAT函数构建全名。 For example:- 例如:-
<?php
mysql_connect('host.com', 'user', 'pass'); mysql_select_db('userdb');
$results = mysql_query(sprintf(
"SELECT CONCAT(FNAME, ' ', LNAME) AS NAME FROM `details` WHERE PHONE='%s';",
mysql_real_escape_string($_POST['phone'])
));
$rows = array();
if($results)
{
while($row = mysql_fetch_assoc($results))
{
$rows[] = $row;
}
}
print_r($rows)
?>
Anthony. 安东尼
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.