Jump to content

[SOLVED] How to check if a column exists, if not then make it? Not working help please.


physaux

Recommended Posts

Here is my try at it:

<?php

$tableFields = mysql_list_fields("DB_NAME", "TABLE_NAME");
		$columns = mysql_num_fields($tableFields);   //LINE 21
		for ($i = 0; $i < $columns; $i++) {
			$field_array[] = mysql_field_name($tableFields, $i);
		}
		if (!in_array(NEW_COLUMN; $field_array)) {
			$result = mysql_query("ALTER TABLE `".TABLE_NAME."` ADD `".NEW_COLUMN."` INT(11) NOT NULL DEFAULT ‘1′;");
			echo "ADDED TABLE<br>";
		}else{
			echo "ALREADY EXISTS<br>";
		}

?>

 

I just get a blank screen, no output.

Idk anyone got any ideas?

 

EDIT:

 

I now get this error:

mysql_num_fields() expects parameter 1 to be resource, boolean given in Line 21

check this line:

 

if (!in_array(NEW_COLUMN; $field_array))

 

change out that ; for a ,

 

you have error_reporting on?  you should've received a parse error.

 

EDIT: what is  &#8216;1&#8242;; at the end of the query?

I have no clue why that semicolon got there, it is just a comma in my code. I even copy- pasted it. It was changing my apostraphes to encoded crazy values so I removed them and put " in preview before post.

 

check this line:

 

if (!in_array(NEW_COLUMN; $field_array))

 

change out that ; for a ,

 

you have error_reporting on?  you should've received a parse error.

 

EDIT: what is  &#8216;1&#8242;; at the end of the query?

I added the following under the first line:

if($tableFields===true or $tableFields===false)

echo "WAS BOOLEAN";

 

my page says "WAS BOOLEAN"

 

why am I getting a boolean?

 

EDIT: aaaaaahhhh I see what i did, I had my "CONSTANT" in quotes... :facewall:

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.