I
I
Ivan2021-06-15 23:01:40
MySQL
Ivan, 2021-06-15 23:01:40

How to check if a field exists in a table?

There is a table test_category, how to check the existence of a category in it, so as not to try to add an already existing one on a new one?

$id = 'test';

$db->query( "SELECT * FROM " . PREFIX . "_category WHERE alt_name='$id'" );

while ( $row = $db->get_row() ) {
    
$cat_info[$row['alt_name']] = array ();

....

}


How do I check so that I don't add the test category again?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan, 2021-06-16
@9StarRu

Another solution to the problem, check
SHOW COLUMNS FROM `category` LIKE 'alt_name';

R
Rsa97, 2021-06-15
@Rsa97

To deliver on the field UNIQUE an index and to interpose through INSERT IGNORE.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question