For my sins, I have to migrate (or, put simply, to rewrite) the backed functionality that was written to work solely with MySQLi. Now I am trying to do everything in PDO but for some bits of the code I think that it can be sped up by reusing bits of the old script and re-purposing them.
So far it has worked like a charm - changing the functions slightly and doing small to almost insignificant changes to the code to get it working. But here I got stumped.
I have always used while($row = $result->fetch_assoc())
to print all of the rows I have off a database. I really do want to keep the code similar to the structure of the old one. Down bellow is my attempt at having the rows printed but in the SQLSRV it does not work the same way. I can easily display the rows (with no content) by using a for loop like for ($i=0; $i < $rowCount; $i++)
but that deviates from my code too much.
After observing my sample solutions beneath, is it possible to recreate the code above into an SQLSRV solution AND keep the same structure?
My working MySQLi solution:
$sql = "SELECT * FROM tasks ORDER BY PRIORITY DESC;";
$result = $conn->query($sql);// $conn is `$conn = new mysqli($servername, $username, $password, $db);`
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
echo "<br> FROM :".$row["FROM"];
echo "<br> DEPT :".$row["DEPT"];
echo "<br> TASK_NAME :".$row["TASK_NAME"];
echo "<br> PRIORITY :".$row["PRIORITY"];
}
}
And here is my attempt at solving the issue using MS SQLSRV:
$stmt = $GLOBALS['conn']->prepare("SELECT * FROM tasks;");
$stmt->execute();
$stmt->fetchAll();
//
$rowCount = $stmt->rowCount();
//echo "<br> Row Count: ". $rowCount;
while ($rowCount > 0)
{
for ($row = $stmt->fetch())// For evident reasons that doesn't not work
{
echo "<br> FROM :".$row["FROM"];
echo "<br> DEPT :".$row["DEPT"];
echo "<br> TASK_NAME :".$row["TASK_NAME"];
echo "<br> PRIORITY :".$row["PRIORITY"];
}
}