revo
revo

Reputation: 48761

mysqli and fetching data

I'm new to mysqli, I wrote a function as below.

1 - I couldn't find a way for SELECT * query and having bind_result to assign each column value to the same name variable. (e.g. name column value of #row stores to $name)

I think bind_result() has no function on a SELECT * query?

2 - So I tried another option, to fetch all rows and assign them to appropriate variable manually through a loop. I think I should use $query->fetch_all() or $query->fetch_assoc() for looping but I encounter with this:

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

or

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

However I did a phpinfo() and saw mysqlnd was enabled and php version is 5.4.7 (running XAMPP v1.8.1)

And 3- what finally I did is below idea that doesn't work either.

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();
        }
    }
}

I need a way to store all fetched data as what bind_result() does, or having them in an array for later use, and it's much better to know both. tnx

Upvotes: 1

Views: 1320

Answers (2)

Your Common Sense
Your Common Sense

Reputation: 158005

One word to answer all your questions at once - PDO
It has everything you are trying to get from mysqli (in vain):

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

Note the proper way of using a function. it should never echo anything, but only return the data for the future use

Upvotes: 1

Panji
Panji

Reputation: 32

If your mysqli code doesn't have binding_param() you can just write code like below :

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

If you use binding_param() code , you also need to set 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");

Reference :

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

Upvotes: 0

Related Questions