Tony Ito Tony Ito - 2 months ago 8
PHP Question

For loop for information_schema column data type not working

I've tried many combinations but I can't seem to figure out why all the input types become text!

$columnquery = $conn->query("SELECT column_name, column_comment, data_type FROM information_schema.columns
WHERE table_schema = '$dbname'
AND table_name = '$tableName'");
if ($columnquery->num_rows > 0){

while ($columnRows = $columnquery->fetch_assoc()) {
$columnName = $columnRows["column_name"];
$columnType = $columnRows["data_type"];

if ($columnRows['column_comment'] != 'Pups' && $columnType = "varchar"){
echo "$columnName: $columnType <input type='text' name='$columnName'><br>";
}
elseif ($columnRows['column_comment'] != 'Pups' && $columnType = "int") {
echo "$columnName: $columnType <input type='number' name='$columnName'><br>";
}
elseif ($columnRows['column_comment'] != 'Pups' && $columnType = "date") {
echo "$columnName: $columnType <input type='date' name='$columnName'><br>";
}

}

Answer

single "=" is used to make variables equal to something in order to compare u should use "==" e-g $columnquery = $conn->query("SELECT column_name, column_comment, data_type FROM information_schema.columns WHERE table_schema = '$dbname' AND table_name = '$tableName'"); if ($columnquery->num_rows > 0){

while ($columnRows = $columnquery->fetch_assoc()) {
  $columnName = $columnRows["column_name"];
  $columnType = $columnRows["data_type"];

  if ($columnRows['column_comment'] != 'Pups' && $columnType == "varchar"){
  echo "$columnName: $columnType <input type='text' name='$columnName'><br>";
}
elseif ($columnRows['column_comment'] != 'Pups' && $columnType == "int") {
  echo "$columnName: $columnType <input type='number' name='$columnName'><br>";
}
elseif ($columnRows['column_comment'] != 'Pups' && $columnType == "date") {
  echo "$columnName: $columnType <input type='date' name='$columnName'><br>";
}

}