在Php中解析我自己的SQL请求

时间:2017-10-06 08:55:40

标签: php mysql parsing

目前我正在制作一个可以带有left join(只有两个表)的SQL请求的PHP程序,目标是显示它们并进行编辑。问题是,如果我想编辑它们,我必须知道变量来自哪个(哪个表...),我想解析我的请求知道它。

SELECT users.login, users_info.linkedin, users.password, users.region, users.email, users.business, users.firstname, users.name, users.version, users.updatable, users.bodyshop_id, users.mac, users.register_date, users.lastvisite_date , users_info.address1, users_info.address2, users_info.address3, users_info.zip, users_info.cellphone, users_info.phone, users_info.direct_dial_phone, users_info.birthday, users_info.date_in, users_info.date_out, users_info.picture_private, users_info.picture_public, users_info.bodyshop, users_info.enterprise, users_info.department, users_info.job, users_info.facebook, users_info.twitter, users_info.instagram
FROM users
LEFT JOIN users_info ON users.id = users_info.id

所以我只想创建一个以“login”为参数的函数,并返回“users”或者linkedin返回给我users_info。你知道在php中使用它的正确方法吗? 谢谢!

4 个答案:

答案 0 :(得分:0)

SELECT users.login, users.password, users.region, users.email, users.business, users.firstname, users.name, users.version, users.updatable, users.bodyshop_id, users.mac, users.register_date, users.lastvisite_date , users_info.address1, users_info.address2, users_info.address3, users_info.zip, users_info.cellphone, users_info.phone, users_info.direct_dial_phone, users_info.birthday, users_info.date_in, users_info.date_out, users_info.picture_private, users_info.picture_public, users_info.bodyshop, users_info.enterprise, users_info.department, users_info.job, users_info.facebook, users_info.twitter, users_info.linkedin, users_info.instagram
FROM users as users 
LEFT JOIN users_info as users_info ON users.id = users_info.id
WHERE user.you_field_name = your_condition

答案 1 :(得分:0)

今天在php中连接mysql的最现代的语法是使用PDO。文件index.php,例如:

<?php
        class ConnectPDO {

            private $db_name = 'my_db_name';
            private $db_login = 'root';
            private $db_password = '12345678';
            private $host = 'localhost';
            private $opt = [
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
                PDO::ATTR_EMULATE_PREPARES => false,
            ];
            public $connection = null;

            function __construct() {
                $this->connection = new PDO('mysql:host=' . $this->host . ';dbname=' . $this->db_name . ';charset=utf8', $this->db_login, $this->db_password, $this->opt);
            }
        }



    $c=new ConnectPDO();

    $params=array();
    $params['login']='Frodo';

    $prepared=$c->prepare("SELECT * FROM users WHERE login=:login");
    $prepared->execute($params);
    // fetching data array
    $data=$prepared->fetchAll();

    // view into browser
    echo '<pre>';
    print_r($data);
    echo '</pre>';

为防止注射并使用最佳实践编码,众所周知的做法是使用准备和执行方法。您可以在Windows上快速安装xampp并测试此代码。工作文件夹为/xampp/htdocs

此参数需要由您的凭据更改。

private $db_name = 'my_db_name';
            private $db_login = 'root';
            private $db_password = '12345678';
            private $host = 'localhost';

答案 2 :(得分:0)

使用$stmt->fetch(PDO::FETCH_NUM);将数据放入数字索引数组中,然后使用$stmt->getColumnMeta($col_num);获取有关列的信息。

答案 3 :(得分:0)

最后我这样做了,谢谢你的帮助!

public function getTableWithField($field) {

    $tableName1 = "users";
    $tableName2 = "users_info";

    $pos = strpos($this->SQLCMD, $field);

    $tryOne = substr($this->SQLCMD, $pos - strlen($tableName1) - 1, strlen($tableName1));
    $tryTwo = substr($this->SQLCMD, $pos - strlen($tableName2) - 1, strlen($tableName2));

    if ($tableName1 == $tryOne)
        return $tableName1;

    else if ($tableName2 == $tryTwo)
        return $tableName2;

    return "";
}