简体   繁体   English

PHP / MYSQL选择单个对象返回col名称而不是value

[英]PHP/MYSQL select single object returns col name instead of value

I can't figure out why, but when I run this script, it's only displaying the $row[0] column and not any other columns I put in. 我不知道为什么,但是运行此脚本时,它仅显示$row[0]列,而不显示我放入的任何其他列。

$mlsnum = mysql_real_escape_string($_GET['mlsnum']);

$link = mysql_connect('localhost','user','password');
mysql_select_db('singleprop', $link);

$query = "SELECT 'MSTLISTPRC' FROM jos_mls WHERE MSTMLSNO = '".$mlsnum."';";

$return = mysql_query($query);

$result = mysql_fetch_array($return);

$price = $result['MSTLISTPRC'];

echo $price;

Instead of echoing the value of the column that meets the WHERE criteria, it echoes the column name. 它不回显满足WHERE条件的列的值,而是回显列名。

Drop the single quotes. 删除单引号。 They indicate a literal string. 它们指示文字字符串。

SELECT MSTLISTPRC ...

A string is a sequence of bytes or characters, enclosed within either single quote (“ ' ”) or double quote (“ " ”) characters. Examples: 字符串是由字节或字符组成的序列,用单引号(“ ' ))或双引号(” " ”))括起来。

'a string'

"another string"

Documentation 文献资料

You also need to use an associative array in order to call the column name: 您还需要使用关联数组才能调用列名:

$result = mysql_fetch_assoc($return);

Also, please stop using mysql_ functions. 另外,请停止使用mysql_函数。 They are no longer maintained and are officially deprecated . 它们不再维护,已正式弃用 See the red box ? 看到红色框了吗? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. 相反,要了解准备好的语句 ,并使用PDOMySQLi- 本文将帮助您确定哪一个。

删除'MSTLISTPRC'周围的引号=问题已解决

For selecting column you do not use quotes. 选择列时不使用引号。 We use "`" character 我们使用“`”字符

change $query = "SELECT 'MSTLISTPRC' FROM jos_mls WHERE MSTMLSNO = '".$mlsnum."';"; 更改$ query =“从jos_mls中选择'MSTLISTPRC'WHERE MSTMLSNO ='”。$ mlsnum。“';”; to $query = "SELECT `MSTLISTPRC` FROM jos_mls WHERE MSTMLSNO = '".$mlsnum."';"; to $ query =“从jos_mls中选择“ MSTLISTPRC`”,其中MSTMLSNO ='”。$ mlsnum。“';”;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM