Jump to content

Migrating to MySQLi - not fetching data


josephbupe
Go to solution Solved by mac_gyver,

Recommended Posts

Hi,

I am trying to migrate my project from mysql to mysqli but with little knowledge of the later.

Among the noticeable errors is that the code is no longer fetching data from the database, and I am also getting UNDEFINED VARIABLE errors on the in the mysqli statement that should be binding results to variables.

Here is the code if you can help:

//set connection variables
$host = "localhost";
$username = "root";
$password = "password";
$db_name = "mydb"; //database name

//connect to mysql server
$mysqli = new mysqli($host, $username, $password, $db_name);

//check if any connection error was encountered
if(mysqli_connect_errno()) {
    echo "Error: Could not connect to database.";
    exit;
}

//Just counting ...required for pagination...
$query = "SELECT COUNT(*) FROM t_persons";

$result = mysqli_query($mysqli,$query) or die(mysqli_connect_errno());
$num_rows = mysqli_fetch_row($result);


$pages = new Paginator;
$pages->items_total = $num_rows[0];
$pages->mid_range = 9; // Number of pages to display. Must be odd and > 3
$pages->paginate();

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
    
    if ($stmt = $mysqli->prepare("SELECT p.PersonID,
                p.ImagePath,
                p.FamilyName,
                p.FirstName,
                p.OtherNames,
                p.Gender,
                p.CountryID,
                p.StatusID,
                i.IncidentDate,
                i.IncidentCountryID,
                i.AgencyID,
                i.KeywordID
 
FROM t_incidents i
INNER JOIN
  t_incident_persons ip ON ip.IncidentID = i.IncidentID
INNER JOIN
  t_persons p ON ip.PersonID = p.PersonID    

WHERE p.PersonID>0" . $likes . " ORDER BY p.PersonID DESC $pages->limit")) {    
    
    /* Execute the prepared Statement */
    $stmt->execute();
    
    /* Bind results to variables */
    $stmt->bind_result($PersonID,$ImagePath,$FamilyName,$FirstName,$OtherNames,$Gender,$CountryID,$StatusID,$IncidentDate,$IncidentCountryID,$AgencyID,$KeywordID);
    
    /* fetch values */
    while ($rows = $stmt->fetch()) {
     // display records in a table

//    $PersonID=$row[0];
?>
    <div class="thumbnail">
<a href=details.php?PersonID=<?php echo $PersonID ?> target=gallery><img src="./Persons_Images/<?php echo $row[1]; ?>" width="100" height="130" alt="" /></a><br>
<a href="details.php?PersonID=<?php echo $PersonID; ?>"> <?php echo $row[2]; ?> <?php echo $row[3]; ?></a>
</div>

  <?php }?>

<?php
    }

    /* Close the statement */
    $stmt->close();


/* close our connection */
$mysqli->close();
?>               

I will appreciate.

 

Joseph

 

 

Link to comment
Share on other sites

  • Solution

$stmt->fetch() fetches a row of data from the result set and populates the variables that you used in the $stmt->bind_result($PersonID,$ImagePath,...) statement.

 

the while(){} statement should be - 

while ($stmt->fetch()) {

and you would not use $row[0], $row[1], ... you would use $PersonID,$ImagePath,...

  • Like 1
Link to comment
Share on other sites

$stmt->fetch() fetches a row of data from the result set and populates the variables that you used in the $stmt->bind_result($PersonID,$ImagePath,...) statement.

 

the while(){} statement should be - 

while ($stmt->fetch()) {

and you would not use $row[0], $row[1], ... you would use $PersonID,$ImagePath,...

 

 

Thank you Mac. It worked.

 

;D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.