Jump to content
josephbupe

Migrating to MySQLi - not fetching data

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

 

 

Share this post


Link to post
Share on other sites

Turn on error reporting, and let us know which row is producing an error.

error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', '1');
Edited by paddy_fields

Share this post


Link to post
Share on other sites

Why do you do your connect after you run your query???

Share this post


Link to post
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,...

  • Like 1

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.