Home  >  Article  >  Backend Development  >  How to check whether a field exists in php

How to check whether a field exists in php

藏色散人
藏色散人Original
2022-11-14 09:32:581413browse

How to implement the PHP query field: 1. Create a PHP sample file; 2. Connect to the database; 3. Query a certain value through the "select * from" statement; 4. Use "if (!mysql_num_rows( $result)){...}else{...}" statement can be used to determine whether the value exists.

How to check whether a field exists in php

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

How to check whether a field exists in php?

php Query the database table to determine whether a certain value exists

The code is as follows:

<?php  
$sql="select * from checklist where game_id=$gid and task=&#39;$task&#39; and status=&#39;$status&#39;";  
$result=mysql_query($sql);  
$row = mysql_fetch_array($result, MYSQL_ASSOC);  
    if (!mysql_num_rows($result))  
        {  
            echo "record doesn&#39;t exist~~~~~!!!!!!";  
        }  
    else  
        {             // echo mysql_num_rows($result)."\n";  
            echo $row[&#39;game_id&#39;];  
            echo $row[&#39;task&#39;];  
        }  
?>

Extended information

General steps to access MySql

1) Connect to MySql server

Use the mysql_connect() function to establish a connection with the MySql server.

2) Select the MySql database

Use the mysql_select_db() function to select the database on the MySql server and establish a connection with the database.

3) Execute SQL statements

Use the mysql_query() function to execute SQL statements. (Including adding, deleting, modifying, querying, and displaying)

4) Close the result set

Use mysql_free_result($result) to close the result set to release resources.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to check whether a field exists in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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