r/PHPhelp • • Jan 19 '25

Outdated PHP Code

Hello everyone. This is my first time here. I am resurrecting a page that I setup about 15 years ago, and I'm having trouble getting the MySQL/PHP to work like it used to, as I'm sure the coding has changed over this time. It is a member listing, where the visitors may sort by various criteria, which I pass along using URL variables. This worked great over a decade ago.

I'm posting one of my queries and hoping you can point out what needs to be updated to be current. Thanks everyone.

$conn = new mysqli($servername, $username, $password, $database);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$var1 = '$_GET["var1"]';
$var2 = '$_GET["var2"]';
$var3 = '$_GET["var3"]';
$var4 = '$_GET["var4"]';
$result = u/mysqli_query($conn, "SELECT * FROM `sec_tblusers` WHERE state = $var1 AND country = $var2 or state = $var1 AND country = $var3 or state = $var1 AND country = $var4");
if (!$result) {
  echo("<p>Error performing query3: " . mysqli_error() . "</p>");
   exit();
 } 
if ($result->num_rows > 0) {
  while ($row = mysqli_fetch_array($result,MYSQLI_BOTH)) {
                                $id= "" . $row["recid"]. "";
                                $name= "" . $row["name"]. "";
                                $add1= "" . $row["address_line1"]. "";
                                $add2= "" . $row["address_line2"]. "";
                                $city= "" . $row["city"]. "";
                                $state= "" . $row["state"]. "";
                                $zip= "" . $row["zip_post_code"]. "";
                                $country= "" . $row["country"]. "";
                                $email= "" . $row["payer_email"]. "";
                                $photo= "" . $row["photo"]. "";
                                $bio= "" . $row["bio"]. "";
                                $category= "" . $row["category"]. "";
                 
                 
                 echo "<tr>
<td align=center>$category</td>
<td align=center>$name</td>
<td align=center>$city</td>
<td align=center>$state</td>
<td align=center>$country</td>
<td align=center>$email</td>
</tr>";
}  
}        
3 Upvotes

28 comments sorted by

View all comments

Show parent comment

1

u/Big-Dragonfly-3700 Jan 19 '25 edited Jan 19 '25

There are actually only three things that have changed in php that affect the posted code -

  1. Elimination of magic quotes, which provided some protection against sql special characters in a string value being able to break the sql query syntax.
  2. Elimination of the mysql extension.
  3. Use of exceptions for errors.

For point #1, the simplest solution is to use prepared queries, which provide protection for all data types. For point #2, switch to either the mysqli or the much simpler and better designed PDO extension. For point #3, this actually simplifies the code, since you eliminate any existing discrete error handling logic. You only catch and handle database exceptions in your code for user recoverable errors, such as when inserting/updating duplicate user submitted data.

These three things require going through all the database specific code and updating it.

All the rest of the points being made are either due to incorrect or incomplete conversion of the code, unnecessary things the code is doing, and things that the code should be doing that it isn't.

The migration appendixes in the php documentation list the changes that have taken place over time.

1

u/Suspicious-Travel113 Jan 19 '25

Thank you for your reply. I have condensed the original code a lot based upon recommendations. I've scaled it back to simply searching by state only at this point.

$conn = new mysqli($servername, $username, $password, $database);
$state = $_GET["state"];
$sql = "SELECT * FROM sec_tblusers WHERE state=? ORDER BY name ASC"; 
$stmt= $conn->prepare($sql);
$stmt->bind_param("s", $state);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_array()) {
echo ("<tr>
<td style='text-align: center; vertical-align: middle;'>" . $row["category"]. "</td>
<td style='text-align: center; vertical-align: middle;'>" . $row["name"]. "</td>
<td style='text-align: center; vertical-align: middle;'>" . $row["city"]. "</td>
<td style='text-align: center; vertical-align: middle;'>" . $row["state"]. "</td>
<td style='text-align: center; vertical-align: middle;'>" . $row["country"]. "</td>
<td style='text-align: center; vertical-align: middle;'>" . $row["payer_email"]. "</td>
</tr>");
}