php - Add column in MySQL with number of total columns as title -
how can add new column in table named "tische" column-title of total columns in table? tried following codem doesn't work...
<?php //creating sql query $number = "select count(*) information_schema.columns table_name = 'tische'"; $rs = mysqli_query($number); $result = mysqli_fetch_array($rs); $add = "alter table tische add column `$result` varchar(100) not null after zeit"; //importing our db connection script require_once('dbconnect.php'); //executing query database if (mysqli_query($con, $add)) { echo 'element erfolgreich hinzugefügt'; } else { echo 'fehler!'; } //closing database mysqli_close($con);
when execute querys in console of phpmyadmin, works. not in php-script... please me
add require_once('dbconnect.php');
in page of top. add $con
variable in mysqli_query
first param $rs = mysqli_query($con, $number);
syntax of mysqli_query :
mysqli_query(mysqli $con , string $query [, int $resultmode = mysqli_store_result ])
your correction code bellow :
<?php //importing our db connection script require_once('dbconnect.php'); //creating sql query $number = "select count(*) total information_schema.columns table_name = 'tische'"; $rs = mysqli_query($con, $number); $column_name = 0; if(mysqli_num_rows($rs) > 0) { while($result = mysqli_fetch_array($rs)) { $column_name = $result['total']; } $add = "alter table tische add column `$column_name` varchar(100) not null after zeit"; //executing query database if(mysqli_query($con,$add)){ echo 'element erfolgreich hinzugefügt'; } else { echo 'fehler!'; } } //closing database mysqli_close($con); ?>
Comments
Post a Comment