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
8
u/MateusAzevedo Jan 19 '25
So what's the problem? We can't foretell what's wrong if you don't describe what's not working as intended.
The only tips I can give right now:
ANDandORconditions. Your current query most likely doesn't return the expected result;$id =/$name =lines after the thewhileloop, they're doing nothing useful and concatenating with an empty string, both at the beginning and end, makes no sense at all;3
u/Big-Dragonfly-3700 Jan 19 '25 edited Jan 19 '25
In addition to the list of practices that u/MateusAzevedo posted -
3
u/JeffTS Jan 19 '25
You are calling the variables incorrectly.
Also, you aren't sanitizing those variables which opens you up to SQL injection attacks.
1
u/someoneatsomeplace Jan 19 '25
Nobody has mentioned:
Which is an error. Get rid of the u/
This code above, never worked. First thing you should do is find the version of it that did. Between what I just pointed out and the single-quotes around the $_GET variables, this code absolutely never worked for you, this isn't the code you were successfully using before.
2
u/paradoxthecat Jan 19 '25 edited Jan 19 '25
Based on your comment that you get no errors and no output, a combination of $result returning true because the query ran successfully, and num_rows being zero because no records matched your query would result in this behaviour.
Try adding an else clause with some output.
Edit, just noticed that you are quoting the $_GETs, which means you are using that exact text, not the values of the querystrings. This is why you are getting zero results. Use
etc.
2
u/ray_zhor Jan 19 '25
i would use prepared statements in mysqli.
will error out if any of var1,2,3, or 4 are not set
align is deprecated. use:
0
u/SnakeRiverWeb Jan 19 '25
I take it that $var1 and $var2 so on are text values
try putting in single quotes
0
u/tommyboy11011 Jan 19 '25
mysqli_num_rows
0
u/isoAntti Jan 19 '25
I wonder if $_GET gets populated nowadays
-2
u/deepak993635 Jan 19 '25
Nbbmbm