MySQL Forums
Forum List  »  PHP

Assistance with MySQLI Conversion After Upgrade to 5.7
Posted by: Julius Perkins
Date: September 13, 2019 05:48AM

I'm coming in blind here because although I maintain the server, I didn't stand up this particular site & database.

I recently updated a LAMP server where I'm hosting a website & database for a friend and after upgrading from MySQL 5.6 to 5.7 we're seeing loads of errors as a result of antiquated mysql_ commands vs mysqli_.

On one page we generate a report that spits out a CSV with the results of the query, basically header information at the top then all the rows in that particular db. Combing through the documentation (PHP's manual in particular) I've successfully updated other sections of code but I'm struggling with two loops.



Original Code:
$result=mysql_query($query, $thecon) or die("Query ($query) sucks!");
$fields=mysql_num_fields($result);

for ($i=0; $i < mysql_num_fields($result); $i++) //Table Header
{
print "".mysql_field_name($result, $i).",";
}
print "\n";
while ($row = mysql_fetch_row($result))
{
for ($f=0; $f < $fields; $f++)
{
if ($f == 9 && $row[$f] != "")
$quote = "'";
else
$quote = "";

print "$quote$row[$f]$quote";
print ",";
}
print "\n";
}
:EOF



I /think/ this is correct (it's at least producing output) but am curious if this is the most efficient way. (Am I using seven words when four will do?)

$result=mysqli_query($thecon, $query);
$fields=mysqli_num_fields($result);

for ($i=0; $i < mysqli_num_fields($result); $i++) //Table Header
{
print "".mysqli_fetch_field_direct($result, $i)->name.",";
}
print "\n";
while ($row = mysqli_fetch_row($result))
{
for ($f=0; $f < $fields; $f++)
{
if ($f == 9 && $row[$f] != "")
{
$quote = "'";
}
else
{
$quote = "";
}

print "$quote$row[$f]$quote";
print ",";
}
print "\n";
}

Options: ReplyQuote


Subject
Written By
Posted
Assistance with MySQLI Conversion After Upgrade to 5.7
September 13, 2019 05:48AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.