Check if column exist in Mysql table via php

后端 未结 8 1444
余生分开走
余生分开走 2021-01-04 08:55

I created mysql tables and there I have put some columns.

Now I want to check that a certain column exists in the database via php.

Like this:



        
8条回答
  •  借酒劲吻你
    2021-01-04 09:16

    Ok I spent 5 hours on this issue looking through every (sometimes ridiculously convoluted) StackOverflow answer, never finding a working answer, and the answer is so frustratingly simple you will kick yourself (as did I):

    php 7

    $mysqli = new mysqli($mysql_url, $mysql_user, $mysql_password, $db_name);
    
    $column_name = "my_column";
    $table_name = "my_table";
    
    if ($mysqli->query("SELECT $column_name FROM $table_name")){
            //my_column exists in my_table
        }
    else{
            //my_column doesn't exist in my_table
    }
    

    Databases 101....

提交回复
热议问题