Lot’s of php developers use MySQL as their database. And when we do it, have to use queries to interact with the database. So we use mysql_query(query) function. The argument for this function varies according to user inputs. We can’t keep fix query string to this function. As a example consider following query for insert a record for database.
INSERT INTO Persons (Name, Age, Address)
VALUES ('Peter', '35', ‘Park Street’); 
In this query string there are three fields. Data for these fields solely depend on user input the web page. So we cant use fixed values for the VALUES part. To over come this issue we use following query string.
VALUES ($Name, $Age, $Address);
I have replace the fixed values of the previous query with php variables. $Name for Name field, $Age for Age field and $Address for Address field. These three variables changes according to the user input. But the above query gives a error, Can you guess what it is ?. Look so closely, if you can’t identify the error you have the bug in your code. Now look at the corrected query.
INSERT INTO Persons (Name, Age, Address)VALUES (‘$Name’, $Age, ‘$Address’);
Can you identify the difference between the two queries. Yes, it is the single quotation marks. Why we need these quotation marks for the query. This query contains three data types they are String and Integer. For Integer there are no problem but for String we have to say this is a string data. For that we use double quotation marks. So the full php code for this will be the following line.
mysql_query(“INSERT INTO Persons (Name, Age, Address)
VALUES (‘$Name’, $Age, ‘$Address’);”);
Keep in mind that the arguments(inputs) for above function is a string and we have to give the string inside double quotation or single quotation. If we use double quotation for this task we have to use single quotation for the variable and vice versa.
Have fun with this and remember to write comments. eeesY....
No comments:
Post a Comment