Using config.php file with mysqli

I was recently told to stop using mysql_query()

and switch to mysqli()

. Needless to say, I am having difficulty making changes to my code. I'd love to fix what I have now, but I'm also looking for the most efficient way (reading the least typing) loop in the example below.

In my config.php file

<?php
    $host = 'mysql.host.com';
    $user = 'userName';
    $password = 'password';
    $database = 'database';
    $link = new mysqli();
    $link->connect($host, $user, $password, $database);
    if (mysqli_connect_errno()) {
        exit('Connect failed: '. mysqli_connect_error());
    }
?>

      

In my index.php

$i = 0;
$getFundsQuery = "SELECT * FROM fund";
$getFundsResult = $link->query($getFundsQuery);
while($i < $getFundsResult->num_rows){
    echo "<option value = '".$getFundsResult['fundID']."'>".$getFundsResult['name']."</option>";
    $i++;
}

      

The immediate problem is that no data is being returned. And as stated above, I am also looking for a method that would give the least typing to loop through the results

+3


source to share


1 answer


First of all, you can omit $link->connect(..)

with:

$mysqli = new mysqli($host, $user, $password, $database);

      



Second, write down your results like this:

while ( $row = $getFundsResult->fetch_object() ) {
    echo "<option value = '" . $row->fundID ."'>" . $row->name . "</option>";

}

      

+5


source







All Articles