1

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

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

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

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

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

4

2 回答 2

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'];
}

注意使用函数的正确方法。它不应该显任何东西,而只返回数据以供将来使用

于 2013-04-11T14:58:53.813 回答
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

于 2014-10-21T03:47:25.823 回答