字段本身用变量来表示,
我试过用“$字段变量”
也试过'".$字段变量."'
还试过$字段变量
还有'$字段变量'均错,求正确方法,感谢。有网友这么写 "select fileds from table where $filed_name='$filed_value'"    也失败了

解决方案 »

  1.   

    $filed_name字段名?
    ASQL="select fileds from table where "+$filed_name+"='"+$filed_value+"'"  
      

  2.   

    忘记说了,变量是php的。  这个是php页面里的
      

  3.   

    PHP 代码中先ECHO这个语句看看到底是什么, select fileds from table where $filed_name='$filed_value'这个语句如果直接到MYSQL中是无法执行的。
      

  4.   

    是的,的确无法执行,我的意思很简单,select *  from 表 where 字段=值       我现在想让字段用一个php的变量代替,比如$a   值也是一个变量所以说是select where 变量=变量
      

  5.   

    $字段 = 'id';
    $值 = 123;
    $sql = "select * from 表 where $字段='$值'";mysql_query($sql)
      

  6.   

    楼上哥们,你那个我试过了,错误。  下面是错误代码  
    check the manual that corresponds to your MySQL server version for the right syntax to use near '='栏目1'' at line 1select *  from ***_ecms_news where ='栏目1'
      

  7.   


    $sql = "select * from table where id='{$id}'";
    mysql_query($sql);这个试试,不对就奇了怪了
      

  8.   

    字段名也是一个变量的话$sql = "select * from table where ".${fieldname}."='{$id}'";
    mysql_query($sql);
      

  9.   

    错了,字段名没有{}$sql = "select * from table where ".$fieldname."='{$id}'";
    mysql_query($sql);
      

  10.   


    成功啦,谢谢哥们了。又成功排除了一条错误答案。  呵呵 开个玩笑它提示 select c* from ***_ecms_news where ='栏目1'
      

  11.   

    它提示 select c* from ***_ecms_news where ='栏目1'是你的字段名 那个变量没有赋值吧,怎么那么多* 号
      

  12.   

    select * from ***_ecms_news where ='栏目1'已经赋值了  
      

  13.   


    $fieldname = "id";
    $id = "1";
    $sql = "select * from table where ".$fieldname."='{$id}'";
    echo $sql;你确定你赋值了吗?这个你运行看 看
    输出的应该是select * from table where id='1'
      

  14.   

    我的代码如下
    <?php 
    $ab=city;
    include('../../shuchu.php');
    ?>   
    shuchu.php 代码如下
    连接数据库$da=北京;
    $num=$empire->gettotal("select count(*) as total from {$dbtbpre}ecms_news where ".$ab."='{$da}'");  
    $num=$empire->gettotal  这个是cms的自带函数,大意是统计信息条数。然后执行错误  代码就是我贴的那个。 我从新建立个php试一下楼上你的那个代码
      

  15.   

    试过了,你是对的,我的问题出在了include上,include的php不是自动继承上面所声明的变量吗?既然这个问题已解决,结分,感谢。