php 中写 sql语句

比如我知道表名,知道主键名,我想在php中写个 sql语句,能通过表名和表主键ID号 找到表中所要返回的 对应的主键id号的 用户名信息。 求解答。。。
$sql = 'select' . 'username' . 'from' . $tableNmae_user . 'where'. $uid.'='.'connect_uid';
username 表中我要查找的信息名 $tableNmae_user是表名 $uid是我知道的主键id号
connect_uid 是我要查找的表的主键名
这个语句哪里要改? 谢谢

<?php
    <?php 
    header("Content-type:text/html;charset=utf-8"); 
    //用来操作mysql数据
    class Mysql{
      private $host;
      private $port;
      private $user;
      private $password;
      private $dbname;
      private $charset;
   

    //构造方法:初始化
        public function __construct($info = array()){

        $this -> host = isset($info['host']) ? $info['host'] : "localhost";
        $this -> port = isset($info['port']) ? $info['port'] : "3306";
        $this -> user = isset($info['user']) ? $info['user'] : "root";
        $this -> password = isset($info['password']) ? $info['password'] : "root";
        $this -> dbname = isset($info['dbname']) ? $info['dbname'] : "haoyigou";
        $this -> charset = isset($info['charset']) ? $info['charset'] : "utf8";
           $this -> db_connect();
           $this -> db_charset();
           $this -> db_name();
       }
// 数据库连接
    private function db_connect(){
        $res = @mysql_connect($this->host.":".$this->port,$this->user,$this->password);
           if(!$res){
                echo "数据库连接失败!<br/>";
                echo "错误编号是:".mysql_errno()."<br/>";
                echo "错误信息是:".iconv('gbk','utf-8',mysql_error())."<br/>";
                exit;
           }
    }
//封装验证方法
    public function db_query($sql){
        $res =  @mysql_query($sql);
        if(!$res){
             echo "sql语句语法错误!<br/>";
                echo "错误编号是:".mysql_errno()."<br/>";
                echo "错误信息是:".iconv('gbk','utf-8',mysql_error())."<br/>";
                
                exit;
        }
        return $res;
    }
    //设置字符集
    private function db_charset(){
       $sql = "set names {$this->charset}";
              $this->db_query($sql);
    }
//使用数据库
    private function db_name(){
              $sql = "use {$this->dbname}";
              $this->db_query($sql);
    }

    //增删改
    public function db_add($sql){
      //执行语句
             $this->db_query($sql);
             $id = mysql_insert_id();
             return $id ? $id : mysql_affected_rows();
    }
//查:输出单条数据
    public function db_getone($sql){
     $res = $this->db_query($sql);
    
    
     //看不懂私聊我
     $row = mysql_fetch_assoc($res);
     return $row;
    }

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-01-20
你补充的语句字符串加后,词与词之间都没有空格,当然不对。

$sql="SELECT username FROM $tableName_user WHERE connect_uid='$uid'";
第2个回答  2011-01-20
。。。做任务中
第3个回答  推荐于2017-11-23
$sql="select username from user where ID=$id";
其中 user是表名.$id存储你说的用户主键ID的值.
username就是你要查找的用户名,根据你的表修改吧.
有具体的问题,请与我交流...本回答被提问者采纳
第4个回答  2011-01-20
select id from test where name=?;;

其实不懂你在说什么。。

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网