r/PHPhelp • u/Suspicious-Travel113 • 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
View all comments
Show parent comment
3
u/MateusAzevedo Jan 19 '25
Nothing really changed regarding basic syntax and definitely nothing about
echo.As someone else mentioned, the only thing that looks wrong (and I didn't notice the first time) are the lines
$var1 = '$_GET["var1"]';, with a single quote around the$_GETvariable. If it was like that 15 yeas ago, it should never worked at all. In any case, make sure you don't have any quotes around variables, unless you want to use string interpolation with double quotes, but that isn't needed when you just have the variable as the value.That would happen when the query didn't match any results. Simple stuff you can do to validate that (this is the process of debugging code I mentioned):
Add an
else:This way you can confirm that the "issue" is no records in the database matched the query.
Write the query as a variable and echo it, so you can see what it looks like:
$sql = "SELCT * FROM..."and thenecho $sql;. Copy that query, open your preferred MySQL client and execute it directly in your database. Play around with it until it return the expected results. Then make sure your PHP code generates the correct query.If I understood correctly what that query should be doing, I guess a better to write it is like
WHERE state = $var1 AND country IN ($var2, $var3, $var4).One last thing, even when you don't see any error, it's still possible that one happened. Make sure you enable full error reporting and then open the page source code (right click on your browser, show source code) and look for any error message there. Sometimes the error message ends up in a place that the browser consider invalid (from HTML perspective) and it won't be rendered.
Oh, by the way, also make sure you connected to the right database that contains your site's data. It's silly I know, but silly mistakes happens all the time.