How to use php mysql_query()

青灯夜游
Release: 2023-03-12 15:16:02
Original
3224 people have browsed it

php mysql_query() function is used to execute sql statements. When querying, updating, and adding data, you can write the sql statement, and then use the mysql_query() function to execute the sql statement to operate the specified database; syntax " mysql_query($sql [,$con])".

How to use php mysql_query()

The operating environment of this tutorial: windows7 system, PHP5 version, DELL G3 computer

php mysql_query function is used to execute sql statements, such as in queries When updating or adding data, we generally write the sql statement and then use mysql_query to perform the operations required by sql.

Let’s look at an example first:

<?php
  $host="mysql153.secureserver.net";
  $uname="root";
  $pass="password";
  $database="manongjc";
  
  $connection= mysql_connect ($host, $uname, $pass) or die ("Database connection failed!");
  $result=mysql_select_db ($database) or die ("Database could not be selected");
  $query = "drop table if exists mytable";
  $result = mysql_query ($query) or die ("Query failed.");
?>
Copy after login

The steps for php to operate the database can be roughly divided into the following steps:

  • 1. Connect to the database server

  • 2. Select the database to be operated

  • 3. Execute the SQL statement

  • 4. The result set obtained by processing the SQL statement

  • 5. Close or disconnect the database server

The third step is required to execute the sql statement. mysql_query() function, as shown in the above example:

  $query = "drop table if exists mytable";
  $result = mysql_query ($query)
Copy after login

This SQL statement is the command to delete the table mytable. If the execution is successful, it returns true, otherwise it returns false. But the mysql_query function does not necessarily return only true and false.

The mysql_query function returns a resource identifier for a SELECT, SHOW, EXPLAIN, or DESCRIBE statement, or FALSE if the query is executed incorrectly. For other types of SQL statements, mysql_query() returns TRUE on success and FALSE on error.

Note:

mysql_query() is deprecated as of PHP 5.5.0 and will be removed as of PHP 7.0.0. It should be replaced with the MySQLi or PDO_MySQL extension. Alternatives to this function are:

  • mysqli_query()

  • PDO::query()

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to use php mysql_query(). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template