Question:
Using Variables In MySQL Queries?
Corey
2008-12-13 20:56:21 UTC
First time EVER i'm actually stuck, i'm trying to use variables in a mysql query. It would look as such:
"...
mysql_query("CREATE TABLE $username(id INT NOT NULL
...
"
and
"
...
mysql_query("INSERT INTO $username
(age, location, joined, email, name, password) VALUES('$age', '$location', '$date', '$email', '$name', '$password') ")
...
"
None of them work, though if i replace the variables with plain text it does. I cant find any answers for this problem and i have been trying to fix it for 4 hours straight now!
Three answers:
expertaziz
2008-12-13 21:45:28 UTC
Is $username is your table field ?



just enclose the correct username tablefield before age like

("INSERT INTO (usernameTableField,age,location,joined,email,name,password) values (variableusername,variableage,variablelocation,variabledate,variableemail,variablepassword)
lkool
2008-12-13 21:09:52 UTC
what is the error that is returned?



It sounds like you have a variable that is not initialized before it is used in the query.



Also check to see that you don;t have integers that are wrapped in single quotes.
czarnecki
2016-10-16 16:44:16 UTC
$sq. = "decide on * FROM `shops` the place `city`= ' " . $city . " ' AND (`Postal code`= ' " . $postal_code . " ' OR `Postal code` > ' " . $postal_code . " ' OR `Postal code`< ' " . $postal_code . " ' ) ORDER via `Postal code` ASC cut back 0,3'); (areas extra for readability) a million. A field call with an area? " Postal code " ??? exchange to "postal_code" or "p_code", NO areas! 2. (`Postal code`= ' " . $postal_code . " ' OR `Postal code` > ' " . $postal_code . " ' OR `Postal code`< ' " . $postal_code . " ' ) ??? if laptop == 10 or laptop > 10 or laptop < 10 ==> ALL publish codes! Your new (simplified) question: $sq. = "decide on * FROM `shops` the place `city`= ' " . $city . " ' ORDER via `Postal code` ASC cut back 0,3 " ; :-)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...