Page 1 of 2

Connecting to mysql with variables

Posted: Sat Nov 15, 2003 8:13 am
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

Posted: Sat Nov 15, 2003 8:20 am
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.

Posted: Sat Nov 15, 2003 8:23 am
by Linkjames
Yeah, thanks for that, but unfortunatly it does not help.

Posted: Sat Nov 15, 2003 8:30 am
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?

Posted: Sat Nov 15, 2003 8:38 am
by Weirdan
Try to echo variables $uname,$pass,$host and check if it's defined properly. Then try to var_dump($set).

Posted: Sat Nov 15, 2003 9:58 am
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.

Posted: Sat Nov 15, 2003 10:41 am
by Weirdan
Without any error messages?

Posted: Sat Nov 15, 2003 10:56 am
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());
?

Posted: Sun Nov 16, 2003 6:43 am
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

Posted: Sun Nov 16, 2003 11:36 pm
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...

Posted: Mon Nov 17, 2003 12:02 am
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?

Posted: Mon Nov 17, 2003 12:51 am
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:

Posted: Mon Nov 17, 2003 1:00 am
by Paddy
Linkjames, how come you aren't using phpmyadmin anyway?

Posted: Mon Nov 17, 2003 1:10 am
by infolock
yeah, you could put phpadmin in your administration page. much easier ( http://www.phpmyadmin.net )

Posted: Mon Nov 17, 2003 5:38 pm
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. :)