Need help converting a tiny script from PHP5 to 7

Links for php scripts

Moderators: egami, macek, gesf

Post Reply
guardian46w
New php-forum User
New php-forum User
Posts: 5
Joined: Sun Mar 01, 2020 9:53 am

Tue Mar 03, 2020 12:35 pm

I've been trying to convert this little script but have gotten stumped (created it mostly from examples)
<?php

$mysqli = new mysqli("localhost","myname","mypwd","thedb");

if ($mysqli -> connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
exit();
}

//mysqli_select_db("mytable", $mysqli);

//$result = mysqli_query("SELECT * FROM blog ORDER BY id DESC LIMIT 4");

$result = $mysqli->query("SELECT * FROM blog ORDER BY id LIMIT 4")

//echo '<table class='blog'>;

while ($row = mysql_fetch_array($result))
{
echo "<table class='blog'>";
echo "<tr>";
echo "<td class='date'><hr>" . $row['date'] . "</td>";
echo "<tr><td class='item'>" . $row['item'] . "</td>";
echo "</tr>";
echo "</table>";
}

mysqli_close($mysqli);
?>
I have it working down to the WHILE statement. The server log says there is a syntax error in that line. The script worked just fine in PHP5 and displayed my blog entries on my private home page. I know this should be easy (and probably is for most of you) but I can't find an example that works for me. Thanks
User avatar
Strider64
php-forum GURU
php-forum GURU
Posts: 327
Joined: Sat Mar 23, 2013 8:24 am
Location: Livonia, MI
Contact:

Tue Mar 03, 2020 5:28 pm

While I don't use mysql(i) istead I use PDO (Which I recommend) shouldn't the following line be?

Code: Select all

while($row = mysqli_fetch_assoc($result))
or

Code: Select all

while($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
instead of

Code: Select all

while ($row = mysql_fetch_array($result))
Life is a fig newton of your imagination! https://www.pepster.com/index
guardian46w
New php-forum User
New php-forum User
Posts: 5
Joined: Sun Mar 01, 2020 9:53 am

Wed Mar 04, 2020 7:29 pm

Got it to work! Thanks for your suggestions, Strider.
Post Reply