josephbupe Posted October 10, 2014 Share Posted October 10, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/291556-migrating-to-mysqli-not-fetching-data/ Share on other sites More sharing options...
paddy_fields Posted October 10, 2014 Share Posted October 10, 2014 (edited) 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 October 10, 2014 by paddy_fields Quote Link to comment https://forums.phpfreaks.com/topic/291556-migrating-to-mysqli-not-fetching-data/#findComment-1493245 Share on other sites More sharing options...
ginerjm Posted October 10, 2014 Share Posted October 10, 2014 Why do you do your connect after you run your query??? Quote Link to comment https://forums.phpfreaks.com/topic/291556-migrating-to-mysqli-not-fetching-data/#findComment-1493252 Share on other sites More sharing options...
Solution mac_gyver Posted October 10, 2014 Solution Share Posted October 10, 2014 $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,... 1 Quote Link to comment https://forums.phpfreaks.com/topic/291556-migrating-to-mysqli-not-fetching-data/#findComment-1493258 Share on other sites More sharing options...
josephbupe Posted October 13, 2014 Author Share Posted October 13, 2014 $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. Quote Link to comment https://forums.phpfreaks.com/topic/291556-migrating-to-mysqli-not-fetching-data/#findComment-1493436 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.