I need a bit of help to make a connection to a mysql DB with a function and then create an admin table. I got this far:

function dbconn($db_host, $db_username, $db_pass, $db_name) {
	
	
		/* Global research function */
		
		global $researchDbLink;
		
		if($researchDbLink) {
		
			return $researchDbLink;
			
		}
		
		
		/* Connect to DB */
		
		$researchDbLink = mysql_connect($db_host, $db_username, $db_pass);
		
		if (!$researchDbLink) {
		
		    echo "<br>MySQL SERVER CONNECTION ERROR.<br>";
		   
		    return false;
		}
		
		
		/* Select DB  */
		
		if (!mysql_select_db($db_name, $researchDbLink)) {
		    echo "<br>DATABASE SELECTION ERROR: ".mysql_error()."<br>";
		    return false;
		}
		

	}

I used the dbconn($db_host, $db_username, $db_pass, $db_name) in another page and tried to make this:

dbconn($db_host, $db_username, $db_pass, $db_name)

/* Create Admin */

	$sqlCommand = "CREATE TABLE admin(
			id int(11) NOT NULL auto_increment,
			username varchar(24) NOT NULL,
			password varchar(24) NOT NULL,
			last_log_date date NOT NULL,
			UNIQUE KEY username (username)
			)";
	
	if(mysql_query($sqlCommand)){
	
		echo 'The Admin Table was created successfully!';
		
	}else{
	
		echo 'FATAL ERROR: The Admin Table failed dramatically to be created!';
		
	}

And as seed in this context, I get the Fatal Error, but the argument about the connection is valid. What am I missing?

Recommended Answers

All 2 Replies

Output mysql_error(). You'll see that you need a primary key on id.

CREATE TABLE admin(
  id int(11) NOT NULL AUTO_INCREMENT,
  username varchar(24) NOT NULL,
  password varchar(24) NOT NULL,
  last_log_date date NOT NULL,
  PRIMARY KEY (id),
  UNIQUE KEY (username)
)

Thanks a lot, I knew it was something obvious :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.