Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
442 views
in Technique[技术] by (71.8m points)

php - Mysqli - Bind results to an Array

I recently switched to mysqli from mysql and started using prepared statements. in mysql we do

$result = mysql_query("SELECT * FROM table WHERE id = ?");

So we get the array of whole table in one variable.

But in mysqli we do

mysqli_stmt_bind_result($stmt, $result);

so basically here only one variable gets bind to variable result. How can we get the same variable(array) that we got from mysql?
PS - Hope my ques is clear enough. I know their are not many methods possible but i am luking for the best one.
PSS - I am more comfortable with the procedural way.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Finally this code is Working !!!!!

  <?php 
    $host = 'localhost'; 
    $user = 'root'; 
    $pass = '1234'; 
    $data = 'test'; 

    $mysqli = new mysqli($host, $user, $pass, $data); 
    /* check connection */ 
    if (mysqli_connect_errno()) { 
        printf("Connect failed: %s
", mysqli_connect_error()); 
        exit(); 
    } 

    if ($stmt = $mysqli->prepare("SELECT * FROM sample WHERE t2 LIKE ?")) { 
        $tt2 = '%'; 

        $stmt->bind_param("s", $tt2); 
        $stmt->execute(); 

        $meta = $stmt->result_metadata(); 
        while ($field = $meta->fetch_field()) 
        { 
            $params[] = &$row[$field->name]; 
        } 

        call_user_func_array(array($stmt, 'bind_result'), $params); 

        while ($stmt->fetch()) { 
            foreach($row as $key => $val) 
            { 
                $c[$key] = $val; 
            } 
            $result[] = $c; 
        } 

        $stmt->close(); 
    } 
    $mysqli->close(); 
    print_r($result); 
    ?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...