简体   繁体   English

以数据库中的纯文本(而不是数组)显示用户的名字和姓氏

[英]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.

相关问题 如何从数据库中提取特定值,而不是将其显示在表中,而是显示为纯文本 - How to pull a specific value from a database, and display it NOT in a table, but as plain text 从mysql数据库中检索名字和姓氏 - Retrieving first name and last name from mysql database 从文本blob中检测名字和姓氏的最佳方法 - Best approach to detect first and last name from text blob 从表中获取数据以显示为纯文本 - Take data from a table to display as plain text 使用first,1,2,…,last显示数据库字段 - Display database fields using first,1,2,…, last 从数据库中提取名称或姓氏,同时从数据库中提取或通过PHP函数从数据库中提取后,将其大写? - Capitalization of First Name and Last Name at Database end, while fetching from database or via PHP Functions after fetchin it from database? 如何仅显示从数据库中获取的数组的第一个结果? - how to display only the first result of an array that is fetched from the database? 在 php 中将文本字符串拆分为 $first 和 $last name - Split text string into $first and $last name in php 我的用于从数据库表中搜索名字或姓氏的PHP代码未在我的网页上打印任何内容 - My PHP code to search for a first name or last name from a table in a database is not printing anything to my webpage 在关联数组的第一个键之后,PHP代码显示为纯文本 - PHP Code Displaying as Plain Text After First Key in Associative Array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM