Connecting to mysql with variables

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

Moderator: General Moderators

Linkjames
Forum Commoner
Posts: 90
Joined: Tue Sep 16, 2003 8:39 am

Connecting to mysql with variables

Post by Linkjames »

I am attempting to connect to a MySql database and create a table. The information on connecting is in config.php. The code I have so far is

Code: Select all

<?php
function dbcreate()
{
include config.php();
$host = $set['dbhost']
$uname = $set['dbuname']
$pass = $set['dbpass']
$dbcn = @mysql_connect($host, $uname, $pass) or die("Could not connect: " . mysql_error()); 
$select = mysql_select_db("CMS");
$create = "CREATE TABLE test ( 
id int NOT NULL AUTO_INCREMENT, 
firstname varchar(25), 
lastname varchar(20), 
nick varchar(12), 
email varchar(35), 
salary int, 
PRIMARY KEY (id), 
UNIQUE id (id) 
);"; 
$result = mysql_query($create);
echo mysql_errno() . ": " . mysql_error() . "\n";
echo "Table created";
}
?>
But when I call the function I get a blank page, no error message, and the table isn't created. Am I calling the variables wrong? Have I missed some basic syntax (I hope not, I have just spent an hour going over it with a fine toothcomb). Any ideas?

cheers guys.
LinkJames
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

$host = $set['dbhost'] 
$uname = $set['dbuname'] 
$pass = $set['dbpass']
At least you need to add semicolons at the end of this lines.
Linkjames
Forum Commoner
Posts: 90
Joined: Tue Sep 16, 2003 8:39 am

Post by Linkjames »

Yeah, thanks for that, but unfortunatly it does not help.
Linkjames
Forum Commoner
Posts: 90
Joined: Tue Sep 16, 2003 8:39 am

Post by Linkjames »

Ok, the code now spits out an error, which I suppose is an improvement.

Code: Select all

<?php
function dbcreate()
{
include config.php;
$host = $set['dbhost'];
$uname = $set['dbuname'];
$pass = $set['dbpass'];
$dbcn = @mysql_connect($host, $uname, $pass) or die("Could not connect: " . mysql_error()); 
$select = mysql_select_db("CMS");
$create = "CREATE TABLE test ( 
id int NOT NULL AUTO_INCREMENT, 
firstname varchar(25), 
lastname varchar(20), 
nick varchar(12), 
email varchar(35), 
salary int, 
PRIMARY KEY (id), 
UNIQUE id (id) 
);"; 
$result = mysql_db_query($create);
echo mysql_errno() . ": " . mysql_error() . "\n";
echo "Table created";
}
?>
Which gives the error "Could not connect: Access denied for user: 'ODBC@localhost' (Using password: NO)"

Which suggests its not reading all the variables. Any ideas?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Try to echo variables $uname,$pass,$host and check if it's defined properly. Then try to var_dump($set).
Linkjames
Forum Commoner
Posts: 90
Joined: Tue Sep 16, 2003 8:39 am

Post by Linkjames »

Thanks Weirdan. There was a problem with my config.php not being put in correctly. However, the variables are now going in and it is still not creating the table.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Without any error messages?
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

why don't you just go:

Code: Select all

$host = "localhost";
$uname = "username";
$pass = "password"; 
$dbcn = @mysql_connect($host, $uname, $pass) or die("Could not connect: " . mysql_error());
?
Linkjames
Forum Commoner
Posts: 90
Joined: Tue Sep 16, 2003 8:39 am

Post by Linkjames »

ok, the code works now.

Code: Select all

<?php
function dbcreate()
{
include 'config.php';
$host = $set['dbhost'];
$uname = $set['dbuname'];
$pass = $set['dbpass'];
$dbcn = @mysql_connect($host, $uname, $pass) or die("Could not connect: " . mysql_errno() . ": " . mysql_error()); 
$select = mysql_select_db("CMS");
echo mysql_errno() . ": " . mysql_error() . "\n";
$create = "CREATE TABLE `cms_user` (
`id` INT NOT NULL AUTO_INCREMENT, 
`status` INT(2) NOT NULL, 
`username` VARCHAR(20) NOT NULL, 
`password` VARCHAR(50) NOT NULL,
UNIQUE (`id`)
);";
$result = mysql_query($create);
echo mysql_errno() . ": " . mysql_error() . "\n";




echo "<P>Table created</P>";
?>
thanks for all your help guys
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

just wondering, but would it be possible to do this with the below code?

Code: Select all

<?php

function dbcreate($db_name, $table, $row1, $datatype1, $row2, $datatype2, $row3, $datatype3, $row4, $datatype4, $unique_value)
{ 
   include 'config.php'; 
   $host = $set['dbhost']; 
   $uname = $set['dbuname']; 
   $pass = $set['dbpass']; 
   $dbcn = @mysql_connect($host, $uname, $pass) or die("Could not connect: " . mysql_errno() . ": " . mysql_error()); 
   $select = mysql_select_db("".$db_name."") or die(mysql_error()); 

   $create = "CREATE TABLE $table ($row1 $datatype1, $row2 $datatype2, $row3 $datatype3, $row4 $datatype4, UNIQUE $unique_value)";

   $result = mysql_query($create) or die(mysql_error());

   echo "<P>Table created</P>"; 
?>
with usage like :

Code: Select all

<?php

dbcreate('CMS', 'cms_user', 'id', 'INT NOT NULL AUTO_INCREMENT', 'status', 'INT(2) NOT NULL', 'username', 'VARCHAR(20) NOT NULL',`password`,'VARCHAR(50) NOT NULL','id')

?>

was just wondering...cuz then you could reuse it over and over for other databases you needed to create, and tables for that matter...
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post by Paddy »

Not sure if it would work, looks like it would, but I don't think I would use it. Would mean you could only have four fields...

Unless, does php support function overloading? Or parametric polymorphism for that matter?
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

dunno. surely there is a way to make an arrayable variable within that function statement... like function bob ($field[1..500]) or something like that...

would be useful, as it would lessen the overall code, but would be bad because it would make the language easier then it already is :twisted:
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post by Paddy »

Linkjames, how come you aren't using phpmyadmin anyway?
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

yeah, you could put phpadmin in your administration page. much easier ( http://www.phpmyadmin.net )
Linkjames
Forum Commoner
Posts: 90
Joined: Tue Sep 16, 2003 8:39 am

Post by Linkjames »

I'm of the school of thought that the best way to learn somthing is to set yourself a goal, then figure out how to achive it. So I am building a CMS. For ease of use I am having a page in which you enter the details of your MySql server, which are written to config.php, which is then used for all connections to the database and so forth. The setup page is going to create the tables needed on the fly, which is why I am not using PHPMyAdmin.

To those of you that belive I am insane, your absolutlly right. :)
Post Reply