PDA

View Full Version : building first script to create tables in mysql


Crowds
10-03-2005, 10:32 PM
Hi,
I have been going through some online tutorials for using php to create a new table in mysql. I am running sokkit on my home pc with mysql installed there so i am testing the scripts at http://localhost/
I keep getting this error
Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host '$dbhost' (11001) in C:\sokkit\site\phptestarea\install.php on line 10
I kinda know what area of the script im going wrong in but i dont know why.
I have built a form to input data for the conection to mysql. (index.php)
And my form action asks for (install.php)
Here is the form
<form action="install.php" method="get" name="databaseinfo">
<label>
<div align="right">Database Host:
<input type="text" name="dbhost" value="dbhost"/>
</div>
</label>


<label>
<div align="right">Database Name:
<input type="text" name="dbname" value="dbname"/>
</div>
</label>

<label>
<div align="right">Database Username:
<input type="text" name="dbuser" value="dbuser"/>
</div>
</label>

<label>
<div align="right">Database Password
<input type="password" name="dbpass" value="dbpass"/>
</div>
</label>

<label>
<div align="right">Submit
<input type="submit" name="Submit" value="Submit" />
</div>
Not sure if i should be using a $ before dbpass and dbuser etc
Here is the install.php
<html>
<body>


<?php
// install.php: seting up you database details and creating a new table

// (1) connect to the server
// enter your database details: below in 'localhost' 'username' and 'password'
if( mysql_connect( '$dbhost', '$dbuser', '$dbpass' ) )
echo( "Connected to database.<br>\n" );
else
die( "Error! Could not connect to server: " . mysql_error() );

// (2) select the database: Enter the name of your database below 'database_name'
if( mysql_select_db( '$dbname' ) )
echo( "Selected the database.<br>" );
else
die( "Error! Could not select the database.<br>Please Check your connection details: " . mysql_error() );

// (3) create the table: If you have connected to server succsesfully then the Table below
// Will Be created With table name as 'gallery_bio' Art/photo title 'title'
// submitters name and age 'name' 'age' email address 'email' and their Biography 'bio'
// and website 'www'
// the VARCHAR() is the number of charecters available for each field. This is why 'age'
// only 3
$create_query = "CREATE TABLE `gallery_bio` ( `title` VARCHAR(30), name VARCHAR(40),
`age` VARCHAR(3), `email` VARCHAR(40), `bio` VARCHAR(100) `www` VARCHAR (40),
`id` INT AUTO_INCREMENT, UNIQUE (`id`) )";

if( mysql_query( $create_query ) )
echo( "Table created successfully.<br>" );
else
die( "Error! Could not create table: " . mysql_error() );


// put in dummy values
$insert_query = "INSERT INTO `gallery_bio`
( `title`, `name` , `age`, `email` , `bio`) VALUES
( 'S-C Admin' , '---', '---','admin@surviving-chesterfield.co.uk', 'Administrator For surviving Chesterfield' ) ";

if( mysql_query( $insert_query ) )
echo( "Values inserted successfully." );
else
die( "Error! Could not insert values: " . mysql_error() );

?>



</body>
</html>


My ultimate aim is to create a data base of biogs for artists/photographers that submit there work to an online gallery and to call the correct biog depending on what image is shown. But one step at a time... i need to get my head around php and mysql connections first. Can anyone see where i am going wrong. I think i am messing up with variables somewhere :(

Nightfire
10-03-2005, 10:42 PM
Variables in single quotes don't get parsed. So, you've got 2 choices.

if( mysql_connect( "$dbhost", "$dbuser", "$dbpass" )

Or the preferred way

if( mysql_connect( $dbhost, $dbuser, $dbpass)


Good town to do a site on ;) Not often I see people on here within the same town as me

Crowds
10-03-2005, 10:52 PM
Cheers Nightfire,
Now i am getting this

Notice: Undefined variable: dbhost in C:\sokkit\site\phptestarea\install.php on line 10

Notice: Undefined variable: dbuser in C:\sokkit\site\phptestarea\install.php on line 10

Notice: Undefined variable: dbpass in C:\sokkit\site\phptestarea\install.php on line 10
Connected to database.

Notice: Undefined variable: dbname in C:\sokkit\site\phptestarea\install.php on line 16
Error! Could not select the database.
Please Check your connection details: No Database Selected

line 10 and 16 are

if( mysql_connect( $dbhost, $dbuser, $dbpass ) )

and

if( mysql_select_db( $dbname ) )
am i doing something wrong in my form?
or maybe i should rename elements of my form from

<input type="text" name="dbhost" value="dbhost"/>

to <input type="text" name="database_host" value="database_host"/>
and include something along the lines of

$dbhost = ['database_host'];

in my install.php ?

Nightfire
10-03-2005, 11:02 PM
Add this to your script. If you're sending the form via GET, then replace POST for GET

<?php


$dbhost = mysql_real_escape_string($_POST['dbhost']);
$dbuser = mysql_real_escape_string($_POST['dbuser']);
$dbpass = mysql_real_escape_string($_POST['dbpass']);
$dbname = mysql_real_escape_string($_POST['dbname']);

// install.php: seting up you database details and creating a new table

// (1) connect to the server
// enter your database details: below in 'localhost' 'username' and 'password'

marek_mar
10-03-2005, 11:16 PM
Good town to do a site on ;) Not often I see people on here within the same town as me
Localhost is the biggest web hosting company out there. The best part is that it's free!

Crowds
10-03-2005, 11:17 PM
Cheers Nightfire,
that seemed to get me a little closer. but still no joy. As you can see from below i got an access denied but it did accept the password.

Notice: Undefined index: dbuser in C:\sokkit\site\phptestarea\install.php on line 8

Warning: mysql_connect() [function.mysql-connect]: Access denied for user: 'ODBC@127.0.0.1' (Using password: YES) in C:\sokkit\site\phptestarea\install.php on line 16
Error! Could not connect to server:

Lines 8 and 16


$dbuser = mysql_real_escape_string($_GET['dbuser']);

if( mysql_connect( $dbhost, $dbuser, $dbpass ) )

This would seem to suggest its not happy with my user name. But sokkit defaults to root as username on install and i have kept it as that as i am running it from my own pc. And i have no idea how tochange the database username once its installed. I cannot find anything in phpmyadmin.
:confused:

Crowds
10-03-2005, 11:22 PM
Nightfire i missed the part of your post about you being in the same town as me. :eek:
So do you think surviving chesterfield is a good idea once its up and running ?
:)
Or do you think i am likely to be chased out of town be torch weilding locals
:D

Nightfire
10-04-2005, 12:00 AM
Says you're using the username ODBC according to that error? Try hard coding the username into the db connection

mysql_connect('127.0.0.1','root','INSERT PASSWORD');

Site is a good idea, been meaning to do a site on town myself as the 'main' site ( http://www.chesterfieldweb.co.uk ) is badly made etc, but I've never got around to doing it

Crowds
10-04-2005, 12:27 AM
Nearly there :)
I got this
Connected to database.
Selected the database.
Error! Could not create table: 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 'www` VARCHAR (40), `id` INT AUTO_INCREMENT, UNIQUE (`id
Something to do with the creation of fresh id's for every field added..

Bah Humbug

Christ on a bike i have never seen that site before. It looks like it should have cobwebs on it :) I thought visit chesterfield (http://www.visitchesterfield.info/) was the main contender which scared me sh#tless because everybody loves a site were all the content is crammed onto the left off the screen :D
shouldn't laugh my artcell area is giving me 3 types of sh#t in a 1 sh#t bag.
just dont ever view it in IE :rolleyes:
If you have any ideas for areas that the site could do with i am all ears and would love the input... just dont ask me to include a spireites blog.. :D

Nightfire
10-04-2005, 12:35 AM
Missing comma here

`bio` VARCHAR(100) ,`www` VARCHAR (40),


Will give ideas on your forums as I don't think the mods will really be wanting us to go off topic :D

Crowds
10-04-2005, 12:57 AM
Thanks Nightfire Thats worked a dream table and test fields all in place :thumbsup:

No I agree, I certainly dont want booting out these forums... as you can see i need them too much :)
Thanks again
Crowds