Jump to content

using a variable to name a database


amplexus

Recommended Posts

Hi,

 

I'm trying to use a variable- "$newdbname" in a script that sets up a new database.  I'm using this variable because I have an include file that sets all login info and the database name as variables.

 

here's the code. or at lest the relevant part.

<?php
require 'dbinfo.inc.php';

$db = mysql_connect($servname,$dbusername,$dbpassword) or 
    die ('Unable to connect. Check your connection parameters.');
mysql_select_db($database, $db) or die(mysql_error($db));

// create the user table
$query = 'CREATE TABLE  ($newdbname)(
        user_id  int(30)	 NOT NULL AUTO_INCREMENT,
        username varchar(20) NOT NULL,
        password varchar(41) NOT NULL,

        PRIMARY KEY (user_id)
    )
    ENGINE=MyISAM';
mysql_query($query, $db) or die (mysql_error($db));

 

if I run the script like this, I get the following error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '($newdbname)( user_id int(30) NOT NULL AUTO_INCREMENT, usernam' at line 1

 

if I run the code as this, without parentheses around the variable, it creates a table named "$newdbname"

<?php
require 'dbinfo.inc.php';

$db = mysql_connect($servname,$dbusername,$dbpassword) or 
    die ('Unable to connect. Check your connection parameters.');
mysql_select_db($database, $db) or die(mysql_error($db));

// create the user table
$query = 'CREATE TABLE  $newdbname (
        user_id  int(30)	 NOT NULL AUTO_INCREMENT,
        username varchar(20) NOT NULL,
        password varchar(41) NOT NULL,

        PRIMARY KEY (user_id)
    )
    ENGINE=MyISAM';
mysql_query($query, $db) or die (mysql_error($db));

 

what am I doing wrong?

 

Link to comment
Share on other sites

You need to use double quotes because single quotes do not have variable interpolation. And just to nitpick, you're creating a table, not a database.

 

$query = "CREATE TABLE  $newdbname (
        user_id  int(30)	 NOT NULL AUTO_INCREMENT,
        username varchar(20) NOT NULL,
        password varchar(41) NOT NULL,

        PRIMARY KEY (user_id)
    )
    ENGINE=MyISAM";

Link to comment
Share on other sites

okay, so that got me through part of it, but now I get this:

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''users1'( user_id int(30) NOT NULL AUTO_INCREMENT, username va' at line 1

 

The "users1" part is the string from the variable, thanks for that much so far!

 

what else is going wrong?  what syntax is off?

Link to comment
Share on other sites

removed quotes as advised.  received following message. 

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(users234) (user_id, username, password) VALUES (1, "23",' at line 1

 

 

 

which seems to be referring to teh second part of my code:

<?php
require 'dbinfo.inc.php';

$db = mysql_connect($servname,$dbusername,$dbpassword) or 
    die ('Unable to connect. Check your connection parameters.');
mysql_select_db($database, $db) or die(mysql_error($db));

// create the user table
$query = "CREATE TABLE  $newdbname(
        user_id  int(30)	 NOT NULL AUTO_INCREMENT,
        username varchar(20) NOT NULL,
        password varchar(41) NOT NULL,

        PRIMARY KEY (user_id)
    )
    ENGINE=MyISAM";
mysql_query($query, $db) or die (mysql_error($db));


$query = 	"INSERT IGNORE INTO ($newdbname)
        (user_id, username, password) 
    VALUES
        (1, '23', PASSWORD('23')),
        (2, 'amplexus', PASSWORD('password'))";

mysql_query($query, $db) or die (mysql_error($db));

echo 'Success!';
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.