mysqli和获取数据

时间:2013-04-11 14:47:10

标签: php mysqli

我是mysqli的新手,我写了一个函数如下。

1 - 我找不到SELECT *查询的方法,并让bind_result将每个列值分配给同一名称变量。 (例如#row的name列值存储到$name

我认为bind_result()SELECT *查询中没有任何功能?

2 - 所以我尝试了另一个选项,获取所有行并通过循环手动将它们分配给适当的变量。我想我应该使用$query->fetch_all()$query->fetch_assoc()进行循环,但我遇到了这个问题:

Fatal error: Call to undefined method mysqli_result::fetch_all()

Fatal error: Call to undefined method mysqli_result::fetch_assoc()

但是我做了一个phpinfo()并看到mysqlnd已启用且php版本为5.4.7(运行XAMPP v1.8.1)

3 - 我最后做的是低于无效的想法。

function the_names($name)
{
    global $db;
    if($query = $db->prepare("SELECT * FROM users where name=?"))
    {
        $query->bind_param('s', $name);
        if($query->execute())
        {
            $query->store_result();
            if($query->num_rows > 1)
            {
                while($row = $query->fetch())
                {
                    echo $row['name']; // Here is the problem
                }
            }
            else
                echo "not valid";
            $query->close();
        }
    }
}

我需要一种方法来存储所有提取的数据,就像bind_result()所做的那样,或者将它们放在一个数组中供以后使用,并且知道它们两者要好得多。 TNX

2 个答案:

答案 0 :(得分:1)

一次性回答所有问题 - PDO
它拥有你想要从mysqli获得的一切(徒劳):

function the_names($name)
{
    global $db;
    $query = $db->prepare("SELECT * FROM users where name=?");
    $query->execute(array($name));
    return $query->fetchAll();
}

$names = the_names('Joe');
foreach ($names as $row) {
    echo $row['name'];
}

请注意使用函数的正确方法。它应永不回应任何内容,但仅返回数据以供将来使用

答案 1 :(得分:0)

如果您的mysqli代码没有binding_param(),您只需编写如下代码:

$mysqli = new mysqli("localhost" , "root" , "" , "database_name");
$result = $mysqli->query( "SELECT * FROM users where name=" . $name) ;
while ( $row =  $result->fetch_assoc() ) {
    echo $row["name"];
}

如果您使用binding_param()代码,则还需要设置bind_result()

$db = new mysqli("localhost" , "root" , "" , "database_name");
function the_names($name){
    global $db;


    /* Prepared statement, stage 1: prepare */
    if (!($query = $db->prepare("SELECT * FROM users where name=?"))) { # prepare sql
    echo "Prepare failed: (" . $db->errno . ") " . $db->error;
    }


    /* Prepared statement, stage 2: bind and execute */
    if (!$query->bind_param("s", $name)) { # giving param to "?" in prepare sql
        echo "Binding parameters failed: (" . $query->errno . ") " . $query->error;
    }

    if (!$query->execute()) {
        echo "Execute failed: (" . $query->errno . ") " . $query->error;
    }


    $query->store_result(); # store result so we can count it below...
    if( $query->num_rows > 0){ # if data more than 0 [ that also mean "if not empty" ]

        # Declare the output field of database
        $out_id    = NULL;
        $out_name  = NULL;
        $out_age   = NULL;
        if (!$query->bind_result($out_id, $out_name , $out_age)) {
            /* 
             * Blind result should same with your database table !
             * Example : my database
             * -users
             * id   (  11     int )
             * name ( 255  string )
             * age  (  11     int )
             * then the blind_result() code is : bind_result($out_id, $out_name , $out_age)
             */
            echo "Binding output parameters failed: (" . $query->errno . ") " . $query->error;
        }

        while ($query->fetch()) {
            # print the out field 
            printf("id = %s <br /> name = %s <br /> age = %s <br />", $out_id, $out_name , $out_age);
        }

    }else{
        echo "not valid";
    }

}
the_names("panji asmara");

参考:

http://php.net/manual/en/mysqli.quickstart.prepared-statements.php