免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 4357 | 回复: 29
打印 上一主题 下一主题

这条修改SQL语句为何无法获取到值? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-08-16 14:44 |只看该作者 |倒序浏览
$query = "select * from $tbl_order where orderID='$orderID' and orderStatus='0'";
$db->ExecQuery($query);
$orderInfo = $db->FetchArray();
$query = "update $tbl_user set userPreMoney=userPreMoney-$orderInfo['totalPrice'] where userID='$userID'";
$db->ExecQuery($query);

上例是修改数据的示例,运行时没有任何反应,打印SQL语句,发现并没有获取到$orderInfo['totalPrice']的值,后来将此处修改为$orderInfo[totalPrice](即去掉了单引号),运行正常,这是为何?如果单单查询$orderInfo['totalPrice']是有效的(即能够获到到值),但修改则不行了,想了半天也不明白,望能得到指导,谢谢。

论坛徽章:
1
技术图书徽章
日期:2013-12-05 23:25:45
2 [报告]
发表于 2006-08-16 17:38 |只看该作者
echo($query);

论坛徽章:
0
3 [报告]
发表于 2006-08-16 17:43 |只看该作者
$query = "update $tbl_user set userPreMoney=userPreMoney-$orderInfo['totalPrice'] where userID='$userID'";
是不是
$query = "update $tbl_user set userPreMoney="userPreMoney-$orderInfo['totalPrice']" where userID='$userID'";

论坛徽章:
0
4 [报告]
发表于 2006-08-16 21:17 |只看该作者
噢,总算忙完了,累死我了,好不容易坐在电脑前哪,我测试了,如果是下面写法:
$query = "update $tbl_user set userPreMoney=userPreMoney-$orderInfo['totalPrice'] where userID='$userID'";
呵呵,什么也不打印出来,一片空白,再修改如下:
$query = "update $tbl_user set userPreMoney=userPreMoney-$orderInfo[totalPrice] where userID='$userID'";
即字段名不加单引号,OK,打印出来了,我上面说过,去掉了单引号一切就正常,加上就什么都不执行,解决的方法是知道了,但原理还是不懂,查看了SQL语法要求也没明白,恳请版主指点一下,不会是单引号不解析字符串中的变量而双引号会解析字符串中的变量这一原因吧,我觉的不是这个原因,希望能得到正确答案,谢谢。

论坛徽章:
1
技术图书徽章
日期:2013-12-05 23:25:45
5 [报告]
发表于 2006-08-16 21:24 |只看该作者
又是一个典型的,不仔细看手册的典型经典案例。

http://w.yi.org/ftp/FAPM/PHP/zh/ ... ypes.string.parsing
看十遍,并且代码测试十遍。、

论坛徽章:
0
6 [报告]
发表于 2006-08-16 21:43 |只看该作者
非常感谢版主的指点,这句我其实都看过了:
和其他两种语法不同,单引号字符串中出现的变量和转义序列不会被变量的值替代

但我测试了下列语句:
<?php
include_once("include/common.php");
$query = "select * from $tbl_order where orderID='1155734020694929' and orderStatus='0'";
$db->ExecQuery($query);
$orderInfo = $db->FetchArray();

echo $orderInfo['totalPrice'];
?>
可以获取到值的,这是单纯查询数据的,但修改数据就不行了,还请版主再予指点一下,我的理解能力真的不好。

论坛徽章:
1
技术图书徽章
日期:2013-12-05 23:25:45
7 [报告]
发表于 2006-08-16 21:46 |只看该作者
变量解析
当用双引号或者定界符指定字符串时,其中的变量会被解析。

有两种语法,一种简单的和一种复杂的。简单语法最通用和方便,它提供了解析变量,数组值,或者对象属性的方法。

复杂语法是 PHP 4 引进的,可以用花括号括起一个表达式。

简单语法
如果遇到美元符号($),解析器会尽可能多地取得后面的字符以组成一个合法的变量名。如果你想明示指定名字的结束,用花括号把变量名括起来。


  1. <?php
  2. $beer = 'Heineken';
  3. echo "$beer's taste is great"; // works, "'" is an invalid character for varnames
  4. echo "He drank some $beers";   // won't work, 's' is a valid character for varnames
  5. echo "He drank some ${beer}s"; // works
  6. echo "He drank some {$beer}s"; // works
  7. ?>
复制代码




同样也可以解析数组索引或者对象属性。对于数组索引,右方括号(])标志着索引的结束。对象属性则和简单变量适用同样的规则,尽管对于对象属性没有像变量那样的小技巧。


  1. <?php
  2. // These examples are specific to using arrays inside of strings.
  3. // When outside of a string, always quote your array string keys
  4. // and do not use {braces} when outside of strings either.

  5. // Let's show all errors
  6. error_reporting(E_ALL);

  7. $fruits = array('strawberry' => 'red', 'banana' => 'yellow');
  8. // Works but note that this works differently outside string-quotes
  9. echo "A banana is $fruits[banana].";
  10. // Works
  11. echo "A banana is {$fruits['banana']}.";

  12. // Works but PHP looks for a constant named banana first
  13. // as described below.
  14. echo "A banana is {$fruits[banana]}.";

  15. // Won't work, use braces.  This results in a parse error.
  16. echo "A banana is $fruits['banana'].";

  17. // Works
  18. echo "A banana is " . $fruits['banana'] . ".";

  19. // Works

  20. echo "This square is $square->width meters broad.";
  21. // Won't work. For a solution, see the complex syntax.
  22. echo "This square is $square->width00 centimeters broad.";
  23. ?>
复制代码




对于任何更复杂的情况,应该使用复杂语法。

复杂(花括号)语法
不是因为语法复杂而称其为复杂,而是因为用此方法可以包含复杂的表达式。

事实上,用此语法你可以在字符串中包含任何在名字空间的值。仅仅用和在字符串之外同样的方法写一个表达式,然后用 { 和 } 把它包含进来。因为不能转义“{”,此语法仅在 $ 紧跟在 { 后面时被识别(用“{\$”或者“\{$”来得到一个字面上的“{$”)。用一些例子可以更清晰:


  1. <?php
  2. // Let's show all errors
  3. error_reporting(E_ALL);

  4. $great = 'fantastic';

  5. // 不行,输出为:This is { fantastic}
  6. echo "This is { $great}";

  7. // 可以,输出为:This is fantastic
  8. echo "This is {$great}";
  9. echo "This is ${great}";

  10. // Works
  11. echo "This square is {$square->width}00 centimeters broad.";

  12. // Works
  13. echo "This works: {$arr[4][3]}";
  14. // This is wrong for the same reason as $foo[bar] is wrong
  15. // outside a string.  In otherwords, it will still work but
  16. // because PHP first looks for a constant named foo, it will
  17. // throw an error of level E_NOTICE (undefined constant).
  18. echo "This is wrong: {$arr[foo][3]}";
  19. // Works.  When using multi-dimensional arrays, always use
  20. // braces around arrays when inside of strings
  21. echo "This works: {$arr['foo'][3]}";

  22. // Works.
  23. echo "This works: " . $arr['foo'][3];

  24. echo "You can even write {$obj->values[3]->name}";

  25. echo "This is the value of the var named $name: {${$name}}";
  26. ?>
复制代码

论坛徽章:
0
8 [报告]
发表于 2006-08-16 22:06 |只看该作者
版主,我看了又看,还是无法理解,版主您就给点思路吧,点一下,让我开个窍。

论坛徽章:
0
9 [报告]
发表于 2006-08-16 22:07 |只看该作者
老是思路堵住,真的不好意思。

论坛徽章:
1
技术图书徽章
日期:2013-12-05 23:25:45
10 [报告]
发表于 2006-08-16 22:17 |只看该作者

  1. <?php
  2. // Let's show all errors
  3. error_reporting(E_ALL);

  4. $great = 'fantastic';

  5. // 不行,输出为:This is { fantastic}
  6. echo "This is { $great}";

  7. // 可以,输出为:This is fantastic
  8. echo "This is {$great}";
  9. echo "This is ${great}";

  10. // Works
  11. echo "This square is {$square->width}00 centimeters broad.";

  12. // Works
  13. echo "This works: {$arr[4][3]}";
  14. // This is wrong for the same reason as $foo[bar] is wrong
  15. // outside a string.  In otherwords, it will still work but
  16. // because PHP first looks for a constant named foo, it will
  17. // throw an error of level E_NOTICE (undefined constant).
  18. echo "This is wrong: {$arr[foo][3]}";
  19. // Works.  When using multi-dimensional arrays, always use
  20. // braces around arrays when inside of strings
  21. echo "This works: {$arr['foo'][3]}";

  22. // Works.
  23. echo "This works: " . $arr['foo'][3];

  24. echo "You can even write {$obj->values[3]->name}";

  25. echo "This is the value of the var named $name: {${$name}}";
  26. ?>
复制代码



我这次真的是无话可说。

你还是花钱,请一个家教为好。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP