dqf67993 2012-10-11 08:25
浏览 41

如何在mysql_query php中选择一个表

hi to all m new to OOP PHP. i have following piece of code here in the following:

test.php

<?php
class test 
{
    private $dbhost;
    private $dbname;
    private $user;
    private $pass;
    public function __construct()
    {
        $this->dbhost = 'localhost';
        $this->dbname = 'XXXX';
        $this->user = 'root';
        $this->pass = '';
    }
    public function open($obj)
    {
        $con = mysql_connect($this->dbhost,$this->user,$this->pass) or die(mysql_error());
        $db = mysql_select_db($this->dbname)or die(mysql_error());
        $query = mysql_query("SELECT * FROM '".$obj."'") or die(mysql_error());

    }

}

?>

in the other file named test2.php i have the following piece of code:

<?php
    require_once('testClass.php');
    $obj = new test();
    $obj2 = 'user';
    $obj->open($obj2);
?>

and i got the following error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''user'' at line 1

thnx in advance for help

  • 写回答

2条回答 默认 最新

  • douzhao1912 2012-10-11 08:28
    关注

    $obj should be tablename and you doesn't need to put single quote around table name.

    mysql_query("SELECT * FROM ".$obj) or die(mysql_error());
    

    Edit:

     while($row = mysql_fetch_array($query)){
        echo $row['userId'];
     } 
    

    Suggestion: Your query must be affected by mysql injection attack. It is really good to use PDO or Mysqli lib.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分