Jump to content

Connecting To My Database


dazmond

Recommended Posts

Hello again, resently i have been trying to conect to my database so i can insert data via a php and mysql script in to a selected table, after reading through a number of books and useing some code that has prevoiusly worked on university server i still seem to keep getting some kind of error message, the script i'm uesing is:

 

<?php

 

$connection = mysql_connect("localhost","username","password","database name")

or die("message");

 

 

INSERT INTO game (cust_id, score, email, name)

VALUES ('','test','if','works');

 

 

 

?>

 

do tch allow this type of accsess and if so can you see what i'm doing wrong as it getting quite fustrating to be honest, php isn't my strong point as you can probly tell, thanking you in advance Darren

:thumbup1:

Link to comment
Share on other sites

Yes, these types of connections are possible.

 

Did you create the database?

Did you create a user?

Did you add the user to the database?

 

You access the database like this.

 

dbname - cpanelname_databasenane

dbuser - cpanel_user

password - what you created

Link to comment
Share on other sites

What kind of errors are you getting?

 

I don't think you can set the database by putting it into mysql_connect as a fourth parameter, so use the mysql_select_db function instead. And also you can't put the queries directly into the PHP as well, you have to use the mysql_query function.

 

Also with the mysql, I'm presuming you're have cust_id set to auto increment since you give it an empty string as a value? If so, just exclude it from the query all together and myself will set it for you as you insert it.

 

Here is what it should look like (I think):

 

><?php

$connection = mysql_connect("localhost","username","password") or die("message");
mysql_select_db ("database") or die ("message");

$query = "INSERT INTO game (score, email, name) VALUES ('test','if','works')";
mysql_query($query);

?>

Edited by D.Slatk
Link to comment
Share on other sites

D.Slatk: Hi thanks a lot that is a big help, i haven't tryed just yet, work keeps getting in the way. The types of error messages that i recive range from the error message that follows the "die()" statment or just says i have autarisation to connect, playing around with it a little it chaged to being a "parse error, unexpected T_STRING" ... "on line 12" which is were the insert data sql statment was. i'll give your vertion a try, fingers crossed, Thanks againg for the help. :group:

//------------------------------------------------------------------------------------------------------------

 

TCH-Bruce: Hi i belive i did create a database, and the user that i'm useing is the one that i use for accessing my cpanle, not too sure what you mean about adding the user to the database to be honest.

 

on the accesing the database descriptions:

 

"dbname - cpanelname_databasenane

dbuser - cpanel_user

password - what you created"

 

what do mean by the "cpanlename", and the "cpanle_user"

 

sorry to be a pain about this but i'm prity much a begginer at this. any help would be gratly apreciated. P.s. sorry about the spellings.

Link to comment
Share on other sites

The bit before the underscore "_" is the cpanel username. ie, your username that you login to your cpanel with.

 

The bit after the underscore is the name you gave to the database when you created it in cpanel, or the name for the user you created (again in cpanel) and assigned to the database.

Link to comment
Share on other sites

This is my quick check list for database's

 

 

Be sure that in MySql you

 

1 - created the database

2 - created a user

3 -added the user to the database (go back to MySql and find the Add user

select the user and database, and then Add user to database)

 

 

Then to connect to the database in a script use:

 

host: localhost

database: yourCpanelName_databaseName

user: yourCpanelName_databaseUser

password: mypassword

 

* Note password is just as the password,

do not add the CpanelName to it.

Link to comment
Share on other sites

I'm sorry the cpanel_user was a typo on my part.

 

That should have been cpanelname_user for the dbuser.

 

Where cpanelname is what you use to log in to cpanel and user is the database user that you create and add to access the database. And underscore separates these two values.

Link to comment
Share on other sites

Cool, grats on getting it working! :group:

Link to comment
Share on other sites

Join the conversation

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

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

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