Avalanche Posted April 1, 2003 Share Posted April 1, 2003 Well, after doing lots of PHP, I want to try to mix in MySQL. The only thing is, I can\'t figure out specifically how to connect (I want to connect to a MySQL database on my website, not on my PC). Let\'s say the dbname was was \"teh_db\", the dbusername was \"teh_user\", and the password for \"teh_user\" was \"teh_pass\". Now how would I connect to the database \"teh_db\" using \"teh_user\"\'s information? Sorry for the n00bish question, but thanks. Quote Link to comment https://forums.phpfreaks.com/topic/288-how-do-i-connect-lol/ Share on other sites More sharing options...
metalblend Posted April 1, 2003 Share Posted April 1, 2003 $conn = mysql_connect("{HOST}",$teh_user,$teh_pass); mysql_select_db($teh_db,$conn); You are now connected and have selected the database under $teh_db. Hope this is what you wanted to know. Some error checking would be nice. Quote Link to comment https://forums.phpfreaks.com/topic/288-how-do-i-connect-lol/#findComment-878 Share on other sites More sharing options...
Avalanche Posted April 1, 2003 Author Share Posted April 1, 2003 Okay, I get a parse error on line 6 (past your connect code): [php:1:a74af47b08]<?php $conn = mysql_connect(\"{HOST}\",$burnttoa_teh_user,$teh_pass); mysql_select_db($burnttoa_teh_db,$conn); CREATE TABLE roleplayers ( id tinyint(4) DEFAULT \'0\' NOT NULL AUTO_INCREMENT, first varchar(20), last varchar(20), age varchar(3), race varchar(20), PRIMARY KEY (id), UNIQUE id (id)); INSERT INTO roleplayers VALUES (1,\'Marth\',\'Kujo\',\'23\',\'Elven\'); INSERT INTO roleplayers VALUES (2,\'Flick\',\'Flick\'s last name is unknown to me.\',\'No Clue!\',\'I think he\'s a little bit of everything...\'); INSERT INTO roleplayers VALUES (3,\'Mister\',\'Fallende\',\'Unknown... no one knows... not even himself.\',\'That\'s a secret. Bwarhar!\'); ?>[/php:1:a74af47b08] I made the databases on the site (it automatically puts burnttoa_ before the dbnames and usernames), so I think it\'s connecting right, just something wrong with the tables. PS: Was I supposed to replace {HOST} with the host name (I\'m pretty sure it\'s localhost) or does it automatically find it? Sorry for the n00bish questions, but thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/288-how-do-i-connect-lol/#findComment-879 Share on other sites More sharing options...
metalblend Posted April 1, 2003 Share Posted April 1, 2003 yes in the connection, {HOST} should have been the host address.. \"localhost\" works in most cases. your queries are not PHP, you\'ll need to use PHP\'s query functions to query MySQL:[php:1:9e7885fd9b]$conn = mysql_connect(\"localhost\",$burnttoa_teh_user,$teh_pass); mysql_select_db($burnttoa_teh_db,$conn); $q1 = mysql_query(\"CREATE TABLE roleplayers ( id tinyint(4) DEFAULT \'0\' NOT NULL AUTO_INCREMENT, first varchar(20), last varchar(20), age varchar(3), race varchar(20), PRIMARY KEY (id), UNIQUE id (id))\",$conn); if ($q1==FALSE) { print \"<code><b>error:</b> query1 failed.<br></code>\"; } else { print \"<code>query1 was successful.<br></code>\"; } $q2 = mysql_query(\"INSERT INTO roleplayers VALUES (1,\'Marth\',\'Kujo\',\'23\',\'Elven\')\",$conn); if ($q2==FALSE) { print \"<code><b>error:</b> query2 failed.<br></code>\"; } else { print \"<code>query2 was successful.<br></code>\"; } $q3 = mysql_query(\"INSERT INTO roleplayers VALUES (2,\'Flick\',\'Flick\'s last name is unknown to me.\',\'No Clue!\',\'I think he\'s a little bit of everything...\')\",$conn); if ($q3==FALSE) { print \"<code><b>error:</b> query3 failed.<br></code>\"; } else { print \"<code>query3 was successful.<br></code>\"; } $q4 = mysql_query(\"INSERT INTO roleplayers VALUES (3,\'Mister\',\'Fallende\',\'Unknown... no one knows... not even himself.\',\'That\'s a secret. Bwarhar!\')\",$conn); if ($q4==FALSE) { print \"<code><b>error:</b> query4 failed.<br></code>\"; } else { print \"<code>query4 was successful.<br></code>\"; } mysql_close($conn);[/php:1:9e7885fd9b] Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/288-how-do-i-connect-lol/#findComment-880 Share on other sites More sharing options...
Avalanche Posted April 1, 2003 Author Share Posted April 1, 2003 Queries 1-4 all fail for some reason. Here is the code: [php:1:238570100d]<?php $conn = mysql_connect(\"localhost\",$burnttoa_teh_user,$teh_pass); mysql_select_db($burnttoa_teh_db,$conn); $q1 = mysql_query(\"CREATE TABLE roleplayers ( id tinyint(4) DEFAULT \'0\' NOT NULL AUTO_INCREMENT, first varchar(20), last varchar(20), age varchar(3), race varchar(20), PRIMARY KEY (id), UNIQUE id (id))\",$conn); if ($q1==FALSE) { print \"<code><b>error:</b> query1 failed.<br></code>\"; } else { print \"<code>query1 was successful.<br></code>\"; } $q2 = mysql_query(\"INSERT INTO roleplayers VALUES (1,\'Marth\',\'Kujo\',\'23\',\'Elven\')\",$conn); if ($q2==FALSE) { print \"<code><b>error:</b> query2 failed.<br></code>\"; } else { print \"<code>query2 was successful.<br></code>\"; } $q3 = mysql_query(\"INSERT INTO roleplayers VALUES (2,\'Flick\',\'Flick\'s last name is unknown to me.\',\'No Clue!\',\'I think he\'s a little bit of everything...\')\",$conn); if ($q3==FALSE) { print \"<code><b>error:</b> query3 failed.<br></code>\"; } else { print \"<code>query3 was successful.<br></code>\"; } $q4 = mysql_query(\"INSERT INTO roleplayers VALUES (3,\'Mister\',\'Fallende\',\'Unknown... no one knows... not even himself.\',\'That\'s a secret. Bwarhar!\')\",$conn); if ($q4==FALSE) { print \"<code><b>error:</b> query4 failed.<br></code>\"; } else { print \"<code>query4 was successful.<br></code>\"; } mysql_close($conn); ?>[/php:1:238570100d] Quote Link to comment https://forums.phpfreaks.com/topic/288-how-do-i-connect-lol/#findComment-881 Share on other sites More sharing options...
Avalanche Posted April 1, 2003 Author Share Posted April 1, 2003 Okay, last edit (I hope...). Here is my code: [php:1:54b2d9f1da]<?php $dbh=mysql_connect (\"localhost\", \"burnttoa_tehuser\", \"tehpass\") or die (\'I cannot connect to the database because: \' . mysql_error()); mysql_select_db (\"burnttoa_tehdb\"); $q1 = mysql_query(\"CREATE TABLE roleplayers ( id tinyint(4) DEFAULT \'0\' NOT NULL AUTO_INCREMENT, first varchar(20), last varchar(20), age varchar(3), race varchar(20), PRIMARY KEY (id), UNIQUE id (id))\",$conn); if ($q1==FALSE) { print \"<code><b>error:</b> query1 failed.<br></code>\"; } else { print \"<code>query1 was successful.<br></code>\"; } $q2 = mysql_query(\"INSERT INTO roleplayers VALUES (1,\'Marth\',\'Kujo\',\'23\',\'Elven\')\",$conn); if ($q2==FALSE) { print \"<code><b>error:</b> query2 failed.<br></code>\"; } else { print \"<code>query2 was successful.<br></code>\"; } $q3 = mysql_query(\"INSERT INTO roleplayers VALUES (2,\'Flick\',\'Flick\'s last name is unknown to me.\',\'No Clue!\',\'I think he\'s a little bit of everything...\')\",$conn); if ($q3==FALSE) { print \"<code><b>error:</b> query3 failed.<br></code>\"; } else { print \"<code>query3 was successful.<br></code>\"; } $q4 = mysql_query(\"INSERT INTO roleplayers VALUES (3,\'Mister\',\'Fallende\',\'Unknown... no one knows... not even himself.\',\'That\'s a secret. Bwarhar!\')\",$conn); if ($q4==FALSE) { print \"<code><b>error:</b> query4 failed.<br></code>\"; } else { print \"<code>query4 was successful.<br></code>\"; } mysql_close($dbh); ?>[/php:1:54b2d9f1da] (Apparently the thing gives me a string to use to connect, lol) And here are the errors I get: Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/burnttoa/public_html/MySQL.php on line 6error: query1 failed. Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/burnttoa/public_html/MySQL.php on line 13 error: query2 failed. Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/burnttoa/public_html/MySQL.php on line 20 error: query3 failed. Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/burnttoa/public_html/MySQL.php on line 27 error: query4 failed. Quote Link to comment https://forums.phpfreaks.com/topic/288-how-do-i-connect-lol/#findComment-882 Share on other sites More sharing options...
shivabharat Posted April 1, 2003 Share Posted April 1, 2003 used $dbh not $conn in mysql_query or leave it blank Quote Link to comment https://forums.phpfreaks.com/topic/288-how-do-i-connect-lol/#findComment-883 Share on other sites More sharing options...
metalblend Posted April 1, 2003 Share Posted April 1, 2003 doh .. yea, i missed that. i\'m used to my $conn connection link. Use this:[php:1:24b28da994]<?php $conn=mysql_connect (\"localhost\", \"burnttoa_tehuser\", \"tehpass\") or die (\'I cannot connect to the database because: \' . mysql_error()); mysql_select_db (\"burnttoa_tehdb\",$conn); $q1 = mysql_query(\"CREATE TABLE roleplayers ( id tinyint(4) DEFAULT \'0\' NOT NULL AUTO_INCREMENT, first varchar(20), last varchar(20), age varchar(3), race varchar(20), PRIMARY KEY (id), UNIQUE id (id))\",$conn); if ($q1==FALSE) { print \"<code><b>error:</b> query1 failed.<br></code>\"; } else { print \"<code>query1 was successful.<br></code>\"; } $q2 = mysql_query(\"INSERT INTO roleplayers VALUES (1,\'Marth\',\'Kujo\',\'23\',\'Elven\')\",$conn); if ($q2==FALSE) { print \"<code><b>error:</b> query2 failed.<br></code>\"; } else { print \"<code>query2 was successful.<br></code>\"; } $q3 = mysql_query(\"INSERT INTO roleplayers VALUES (2,\'Flick\',\'Flick\'s last name is unknown to me.\',\'No Clue!\',\'I think he\'s a little bit of everything...\')\",$conn); if ($q3==FALSE) { print \"<code><b>error:</b> query3 failed.<br></code>\"; } else { print \"<code>query3 was successful.<br></code>\"; } $q4 = mysql_query(\"INSERT INTO roleplayers VALUES (3,\'Mister\',\'Fallende\',\'Unknown... no one knows... not even himself.\',\'That\'s a secret. Bwarhar!\')\",$conn); if ($q4==FALSE) { print \"<code><b>error:</b> query4 failed.<br></code>\"; } else { print \"<code>query4 was successful.<br></code>\"; } mysql_close($conn); ?>[/php:1:24b28da994] or the longer way would be to change all $conn\'s to $dbh\'s Quote Link to comment https://forums.phpfreaks.com/topic/288-how-do-i-connect-lol/#findComment-884 Share on other sites More sharing options...
Avalanche Posted April 1, 2003 Author Share Posted April 1, 2003 Yay, thanks, guys. I wasn\'t paying enough attention to realize the $con variable was used later in the script besides the connecting and disconnecting. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/288-how-do-i-connect-lol/#findComment-885 Share on other sites More sharing options...
Avalanche Posted April 1, 2003 Author Share Posted April 1, 2003 Okay, apparently this one MySQL tutorial I am reading is complete fubar and none of the things work... so: 1) Any good MySQL tutorials? and 2) [php:1:7bc750c6e8]<?php $conn=mysql_connect (\"localhost\", \"burnttoa_tehuser\", \"tehpass\") or die (\'I cannot connect to the database because: \' . mysql_error()); mysql_select_db (\"burnttoa_tehdb\",$conn); $q1 = mysql_query(\"SELECT * FROM roleplayers\",$burnttoa_tehdb); if ($q1==FALSE) { print \"<code><b>error:</b> query1 failed.<br></code>\"; } else { print \"<code>query1 was successful.<br></code>\"; } $q2 = mysql_result($result,1,\"first\"); if ($q2==FALSE) { print \"<code><b>error:</b> query2 failed.<br></code>\"; } else { print \"<code>query2 was successful.<br></code>\"; } mysql_close($conn); ?>[/php:1:7bc750c6e8] I get this error: Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/burnttoa/public_html/MySQL2.php on line 6error: query1 failed. Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/burnttoa/public_html/MySQL2.php on line 14 error: query2 failed. What\'s wrong now? lol Quote Link to comment https://forums.phpfreaks.com/topic/288-how-do-i-connect-lol/#findComment-886 Share on other sites More sharing options...
shivabharat Posted April 1, 2003 Share Posted April 1, 2003 <html> <body> <?php $conn=mysql_connect ("localhost", "burnttoa_tehuser", "tehpass") or die (\'I cannot connect to the database because: \' . mysql_error()); mysql_select_db ("burnttoa_tehdb",$conn); $result = mysql_query("SELECT * FROM roleplayers",$conn); printf(" Name: %s<br>n", mysql_result($result,0,"first")); mysql_close($conn); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/288-how-do-i-connect-lol/#findComment-887 Share on other sites More sharing options...
Avalanche Posted April 1, 2003 Author Share Posted April 1, 2003 Any good MySQL tutorials? The only ones that I find are if you are hosting it on your own PC, not on a web server. Dang Linux users... PS: Thanks, it works. Quote Link to comment https://forums.phpfreaks.com/topic/288-how-do-i-connect-lol/#findComment-888 Share on other sites More sharing options...
shivabharat Posted April 2, 2003 Share Posted April 2, 2003 Download the manual Nothing can help u better than that http://www.phpfreaks.com/articles/75/0.php Now if u are looking for tutorial try our tutorila section!! Quote Link to comment https://forums.phpfreaks.com/topic/288-how-do-i-connect-lol/#findComment-889 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.