0

I have a php code SQL QUERY GOES LIKE

where and are the variable user will enter in the form. i have a database where there are certain values for gpm and certain values for ewt For ex as value ranging from 1-5 with difference of 0.5 eg: 1,1.5,2,2.5 etc and ewt has values ranging from 30-100 with differerence of 10 eg: 30,40,50

so if users enters nd it returns me values from databse but if user enters gp and ewt as it returns me not found How can i know that database did'nt found gpm and it found ewt

Thanks in advance

1 Answers1

2

First, use prepared statements, see How can I prevent SQL injection in PHP?

Use OR instead of AND, and test which was matched in the SELECT list.

$command = "SELECT *, gpm = :uniluxGpm AS gpm_found, ewt = :uniluxEwt AS ewt_found from {$uniluxModel} where gpm = :uniluxGpm OR ewt = :uniluxEwt";
$stmt = $conn->prepare($command);
$stmt->execute([':uniluxGpm' => $uniluxGpm, ':uniluxEwt' => $uniluxEwt]);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • instead of ? should i use my variable names? also what does iiii means? – HalfSparrow Oct 27 '21 at 19:12
  • 1
    See my earlier comment about using a prepared statement to prevent SQL injection. – Barmar Oct 27 '21 at 19:12
  • `iiii` means that the parameters are integers. If they were strings you would use `ssss`. – Barmar Oct 27 '21 at 19:13
  • it shows this error Fatal error: Uncaught Error: Call to undefined method PDOStatement::bind_param() in C:\xampp\htdocs\result.php:81 Stack trace: #0 {main} thrown in C:\xampp\htdocs\result.php on line 81 line 81 is $stmt->bind_param("dddd", $uniluxGpm, $uniluxEwt, $uniluxGpm, $uniluxEwt); – HalfSparrow Oct 27 '21 at 19:17
  • Most posters use mysqli rather than PDO, so that's what I assumed. I'll translate ot PDO. – Barmar Oct 27 '21 at 19:18
  • how can i echo gpmfound and ewtfound please help? – HalfSparrow Oct 27 '21 at 19:23
  • `if ($row['gpm_found'] == 1) { echo "GPM Found"; }` – Barmar Oct 27 '21 at 19:23
  • if ($row['gpm_found'] == 1 AND $row['ewt_found'] == 1 ) { echo "both found"; } elseif ($row['gpm_found'] == 1) { echo "GPM Found"; } elseif ($row['ewt_found'] == 1) { echo "EWT FOUND"; } This is what i have entered and it shows correctly for both elseif statement but when i enter both gpm and ewt correctly it does shows both found – HalfSparrow Oct 27 '21 at 19:39
  • Or Statement does not work, it only returns one value even if both values are present. – HalfSparrow Oct 28 '21 at 15:03
  • Of course. Either a row matches the conditions or it doesn't. If you want separate results for each condition, use separate queries with `UNION`. – Barmar Oct 28 '21 at 15:05
  • What i want is, i want to know what value is not returned so that i can use interpolation and find value for that as well, for example if gpm is present and ewt is not present i only need to interpolate values for ewt, similarly is ewt is present and gpm is not present i only need to interpolate values for gpm and if both are not present i need to interpolate values for both. Can you please help for this? – HalfSparrow Oct 28 '21 at 15:09
  • That's what the `gpm_found` and `ewt_found` columns do. – Barmar Oct 28 '21 at 15:16
  • That's for OR, is there any other way than OR because it doesn't show both both the value. – HalfSparrow Oct 28 '21 at 15:33
  • You could do three differen queries: One with `gpm = X AND ewt != Y`, a second with `gpm != X and ewt = Y`, and third with `gpm = X AND ewt = Y`. – Barmar Oct 28 '21 at 15:41