简体   繁体   English

使用PHP从SQL获取行

[英]Get row from SQL with PHP

I am using the following method to query from my SQL database: 我正在使用以下方法从我的SQL数据库查询:

function query() {
    global $link;
    $debug = false;
    //get the sql query
    $args = func_get_args();
    $sql = array_shift($args);

    //secure the input
    for ($i=0;$i<count($args);$i++) {
        $args[$i] = urldecode($args[$i]);
        $args[$i] = mysqli_real_escape_string($link, $args[$i]);
    }

    //build the final query
    $sql = vsprintf($sql, $args);

    if ($debug) print $sql;

    //execute and fetch the results
    $result = mysqli_query($link, $sql);
    if (mysqli_errno($link)==0 && $result) {

        $rows = array();

        if ($result!==true)
        while ($d = mysqli_fetch_assoc($result)) {
            array_push($rows,$d);
        }
        //return json
        return array('result'=>$rows);
    } else {
            //error
        return array('error'=>'Database error');
    }
}


$result = $result = query("SELECT * FROM users WHERE email='$email' limit 1");

$name = (what goes here?)

I am trying to get the string name from users, how can I do this? 我正在尝试从用户那里获取字符串名称,该怎么办?

You forgot to pass the value to the function 您忘记了将值传递给函数

function query($sql) {
    global $link;
    $debug = false;
    //get the sql query
    $args = func_get_args();
    $sql = array_shift($args);

    //secure the input
    for ($i=0;$i<count($args);$i++) {
        $args[$i] = urldecode($args[$i]);
        $args[$i] = mysqli_real_escape_string($link, $args[$i]);
    }

    //build the final query
    $sql = vsprintf($sql, $args);

    if ($debug) print $sql;

    //execute and fetch the results
    $result = mysqli_query($link, $sql);
    if (mysqli_errno($link)==0 && $result) {

        $rows = array();

        if ($result!==true)
        while ($d = mysqli_fetch_assoc($result)) {
            array_push($rows,$d);
        }
        //return json
        return array('result'=>$rows);
    } else {
            //error
        return array('error'=>'Database error');
    }
}


$result = $result = query("SELECT * FROM users WHERE email='$email' limit 1")
$name = ""; 
if(! isset($result["error"]) and isset($result["name"])) // check it returns error or not. then check name field exist or not.

{
  $name = $result["name"];
}

echo $name;

If your query is right then 如果您的查询是正确的,那么

try this: 尝试这个:

function query() {
        global $link;
        $debug = false;
        //get the sql query
        $args = func_get_args();
        $sql = array_shift($args);

        //secure the input
        for ($i=0;$i<count($args);$i++) {
            $args[$i] = urldecode($args[$i]);
            $args[$i] = mysqli_real_escape_string($link, $args[$i]);
        }

        //build the final query
        $sql = vsprintf($sql, $args);

        if ($debug) print $sql;

        //execute and fetch the results
        $result = mysqli_query($link, $sql);
        if (mysqli_errno($link)==0 && $result) {

            $rows = array();

            if ($result!==true)
            while ($d = mysqli_fetch_assoc($result)) {
                array_push($rows,$d);
            }
            //return json
            return array('result'=>$rows);
        } else {
                //error
            return array('error'=>'Database error');
        }
    }


     $result = query("SELECT * FROM users WHERE email='$email' limit 1");

    $name = $result['result'][0]['name'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM