Connecting To My Database

liunx

Guest
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:<br /><br /><?php<br /><br />$connection = mysql_connect("localhost","username","password","database name")<br />or die("message");<br /><br /><br /> INSERT INTO game (cust_id, score, email, name) <br /> VALUES ('','test','if','works'); <br /><br /><br /><br />?><br /><br />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<br /> <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/wallbash.gif" style="vertical-align:middle" emoid=":wallbash:" border="0" alt="wallbash.gif" /><!--content-->
Yes, these types of connections are possible.<br /><br />Did you create the database?<br />Did you create a user?<br />Did you add the user to the database?<br /><br />You access the database like this.<br /><br />dbname - cpanelname_databasenane<br />dbuser - cpanel_user<br />password - what you created<!--content-->
What kind of errors are you getting?<br /><br />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.<br /><br />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.<br /><br />Here is what it should look like (I think):<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--><?php<br /><br />$connection = mysql_connect("localhost","username","password") or die("message");<br />mysql_select_db ("database") or die ("message");<br /><br />$query = "INSERT INTO game (score, email, name) VALUES ('test','if','works')";<br />mysql_query($query);<br /><br />?><!--c2--></div><!--ec2--><!--content-->
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. <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><br />//------------------------------------------------------------------------------------------------------------<br /><br />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.<br /><br />on the accesing the database descriptions: <br /><br />"dbname - cpanelname_databasenane<br />dbuser - cpanel_user<br />password - what you created"<br /><br />what do mean by the "cpanlename", and the "cpanle_user"<br /><br />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.<!--content-->
The bit before the underscore "_" is the cpanel username. ie, your username that you login to your cpanel with.<br /><br />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.<!--content-->
This is my quick check list for database's<br /><br /><br />Be sure that in MySql you <br /><br />1 - created the database<br />2 - created a user<br />3 -added the user to the database (go back to MySql and find the Add user<br />select the user and database, and then <b>Add user to database</b>)<br /><br /><br />Then to connect to the database in a script use:<br /><br />host: localhost<br />database: <b>yourCpanelName_databaseName</b><br />user: <b>yourCpanelName_databaseUser</b><br />password: <b>mypassword</b><br /><br />* Note password is just as the password, <br />do not add the CpanelName to it.<!--content-->
I'm sorry the cpanel_user was a typo on my part.<br /><br />That should have been cpanelname_user for the dbuser.<br /><br />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.<!--content-->
Thanks Andy, it's SLOWLY starting to making sence.<!--content-->
Thanks very much Don and Bruce, after trying it out on the script that D.Slatk gave me it worked a treat, thanks again you all have been a MASSIVE help. Daz <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/tchrocks!.gif" style="vertical-align:middle" emoid=":tchrocks!:" border="0" alt="tchrocks!.gif" /><!--content-->
You are welcome<br />thats what our family is for.<br /><br />Now have fun <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><!--content-->
Glad it's working <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><!--content-->
Cool, grats on getting it working! <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><!--content-->
 
Top