Creating a table

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Creating a table

Post by toasty2 »

This for some reason doesn't work. I'm pretty sure I understand my PHP book. Is this not right? It doesn't create the table.

Code: Select all

<?php
include 'inc.php'; // This is the file that I have the DB username, password, etc in
mysql_connect($db['host'], $db['user'], $db['pass']);
mysql_select_db($db['db']);

$query = "CREATE TABLE members (
	email VARCHAR(128) NOT NULL PRIMARY_KEY,
	password VARCHAR(64) NOT NULL,
	regdate VARCHAR(12) NOT NULL,
	lastlogin VARCHAR(12),
	country VARCHAR(3) NOT NULL,
	lastip VARCHAR(50),
	PRIMARY KEY(email) )";

mysql_query($query);
mysql_close();
?>
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Have you checked what mysql_error reports after the call to mysql_query?
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

Does it output the error to the user? because it doesn't show me any. (I do have errors on)
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

No, it doesn't. You need to print it.
Theory?
Forum Contributor
Posts: 138
Joined: Wed Apr 11, 2007 10:43 am

Post by Theory? »

Shouldn't it be:

Code: Select all

$connection = mysql_connect($db['host'], $db['user'], $db['pass']);
mysql_select_db($db['db'], $connection);
?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Under the assumption that OP has only one dbconnection it's not required to pass the handle around..

(My guess is that the user simply doesn't have create rights...)
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

No, I've installed a forum before in this database. My queries seem to do nothing. I manually created a table in my DB and i tried this code:

Code: Select all

require 'inc.php';
error_reporting(E_ALL);
$connection = mysql_connect($dbinfo_host, $dbinfo_user, $dbinfo_pass) or die('Couldnt connect to DB.');
mysql_select_db($dbinfo_db, $connection) or die('DB error.');

$query = "SHOW TABLES";
$tables = mysql_query($query);
echo $tables;
mysql_close($connection);
I get no errors, and it says "Resource id #3". What am I doing wrong? (I've never used MySQL before)

And yes, all those mysql-related variables I use have been defined.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You have to fetch the results like any other select query.
Post Reply