0

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?

Edited by seularts: n/a

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by seularts
0

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)
)

Edited by pritaeas: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.