Php help needed for newbie

cyberock

Member
Apr 19, 2002
95
0
0
I was wondering if it is possible to put an if statement inside a mysql statement. I'd like to do a check for some defined variables inside my query. I was hoping to do something like this but this of course doesn't work...

query = "SELECT * ".
"FROM table ".
"where id = 1 "
if(isset($_GET[variable1])){
."and variable1 = $_GET[variable1] "}
if(isset($_GET[variable2])){
."and variable2 = $_GET[variable2] "};


Any suggestions how I could get something like this to work? Thanks!
 

igowerf

Diamond Member
Jun 27, 2000
7,697
1
76
How about:

$query = "SELECT * FROM table where id = 1";

if(isset($_GET[variable1])){
$query = $query." and variable1 =". $_GET[variable1];
}

if(isset($_GET[variable2])){
$query = $query." and variable2 =". $_GET[variable2];
}
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
$query = "SELECT * FROM table WHERE id=1";

if(isset($_GET["variable1"])) $query .= " AND variable1='{$_GET['variable1']}' ";
if(isset($_GET["variable2"])) $query .= " AND variable2='{$_GET['variable2']}' ";

A couple things:

if's are statements. You can't stick them inside of other statements or expressions like a string assignment. You can use the ternary operator like that, but it doesn't make much sense in this case.

You quote string array keys, because you quote strings everywhere. $foo["bar"]. The only time you wouldn't quote bar is when it's a constant that you defined with define(). Which is not the case in your example.

Generally when you put variables from an array inside of a string, you surround them with {}, to help the parser.

If these variables are text, then you need to quote them in the sql statements (like I did in my example).

You need to make sure that these variables have been checked properly, so people can't inject the code of their choice into your web app.

I think that's all..
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |