Select 语句中的 PHP 变量
我已经编写了这个正在运行的 PHP 脚本,现在我想将行名称更改为变量(不确定行是否正确),我的意思是 select name 中的“名称” ... 我几乎尝试了所有方法,但都没有给我带来正确的结果。 我知道在像 ("'. $var .'") 这样的语句中使用变量的正常做法是行不通的。
<?php
require_once 'config.php';
$id = $_GET["id"]; //ID OF THE CURRENT CONTACT
$user = $_GET["user"]; //ID OF THE CURRENT USERS
$query = mysql_query("SELECT name FROM contacts WHERE contact_id='". mysql_real_escape_string( $id ) ."' and user_id='1';");
$retval = mysql_fetch_object($query)->name;
$retval = trim($retval);
echo $retval;
?>
I've written this PHP-Script which is working, and now I want to change the row name into a variable to (not sure if row is correct), I mean the "name" from the select name...
I've tried nearly everything, but nothing gave me the right result.
I know that the normal thing how I can use variables in a statement like ("'. $var .'") won't work.
<?php
require_once 'config.php';
$id = $_GET["id"]; //ID OF THE CURRENT CONTACT
$user = $_GET["user"]; //ID OF THE CURRENT USERS
$query = mysql_query("SELECT name FROM contacts WHERE contact_id='". mysql_real_escape_string( $id ) ."' and user_id='1';");
$retval = mysql_fetch_object($query)->name;
$retval = trim($retval);
echo $retval;
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这容易多了不是吗?
This is much easier isn't it?
您要找的是这个吗?即使你的德语问题对我来说也不太清楚:
Is it this you're looking for? Even your question in German isn't that clear to me :
你可以像这样使用它。目前我假设您只返回一行并且只想使用一个字段。
You can usi it something like this. Currently i assume you get only one row back and want to use only one field.
我相信由于您使用“行”一词,您(无意中)混淆了事情。从你的例子来看,你的意思是字段/列。听起来您希望使用变量指定要选择的字段,可以通过这些方法中的任何一种来完成...
NB 在 $fields 元素中拥有安全日期非常重要,我会建议使用允许值的白名单,即
I believe you are confusing matters (unintentionally) due to your use of the word 'row'. Judging by your example you mean field/column. It sounds like you wish to specify the fields to select using a variable which can be done by any of these methods...
NB it is important that you have secure date in the $fields element, I would suggest using a whitelist of allowed values i.e.
这不行吗?
Wouldn't this work?
您永远不应该将变量放入字段列表中。
如果想要变量字段名称,请选择 *
然后使用您的变量来获取特定字段
You should never put a variable into field list.
If want a variable field name, select *
and then use your variable to fetch particular field