Mysql_query () function requires two arguments, where the first argument is filled with MySQL query, and the second argument is filled with the results of the connection link mysql_connect () function. The second argument is optional.
Here is the basic format of writing mysql_query () function in PHP:
To understand how to use mysql_query () function, we just entered into the example program. In the following program example, I will create a query to show all existing databases in MySQL.
Here is an example of the writing program mysql_query function in PHP:
Here is the basic format of writing mysql_query () function in PHP:
$result = mysql_query("query_mysql",[$link_koneksi_mysql]);$ result is the variable that will hold the results of the function mysql_query (). If
the function mysql_query () was successfully executed, the variable $
result will contain the query results, but if the query fails, this
variable will contain a boolean value FALSE. $ Result variable of type resources (together with the results of mysql_conncect function ()). The name of this variable does not have to be written as $ result, you are free if you want to replace it with another name.query_mysql' is the first argument function mysql_query (). In this section we write MySQL query. The query will then be sent to the MySQL Server for processing. This argument must pertipe string and follow the rules of writing string in PHP. Examples of this query, such as "SELECT * FROM students", or "CREATE DATABASE university".
$ link_koneksi_mysql is the second argument of the function mysql_query (). This argument is filled with the outcome variable function call mysql_connect (). This argument is optional, and if ignored PHP will use the variable mysql_connect () that are connected at this time.
To understand how to use mysql_query () function, we just entered into the example program. In the following program example, I will create a query to show all existing databases in MySQL.
Here is an example of the writing program mysql_query function in PHP:
If you run the above command, inside a web browser will display all the names in the MySQL database.On the 3rd line of the above program, I made a connection with MySQL using mysql_connect () function. This function uses the user 'root' and no password. I save the results of a function connected to the variable $ links.https://www.blogger.com/blogger.g?blogID=6893155570823628933#editor/target=post;postID=7887712292500467804Later
in the 6th line I check the value of the connection mysql_connect () by
making the logic of the IF to the variable $ links. If MySQL fails accessible, then the function die () will instruct PHP to stop the program.However,
if the connection is successful, the line of the 12th I made the
mysql_query () function to run the query 'SHOW DATABASES'. This query is the command to display the entire name of the existing database in MySQL.The program code on line 15 is used to display the query results. Function mysql_fetch_row () I have not discussed, and we will learn in the next tutorial.If
the query used is long and complex, you can store it in a variable
first, then run with mysql_query (), as the following example:
$query="SELECT * FROM mahasiswa WHERE IPK>5";$result=mysql_query($query,$link);
By moving the query into a variable, will make our program becomes more orderly.
The second argument of the function mysql_query () may also not be written, and PHP will use the last MySQL connection link is available, as the following example: $query="SELECT * FROM mahasiswa WHERE IPK>5";$result=mysql_query($query);Function mysql_connect () and mysql_query () is the most basic functions in PHP with a MySQL connection. To display the results of the query, PHP provides a variety of ways that can be used for a variety of situations. The access way will we learn in subsequent articles. 
No comments:
Post a Comment