Jump to content

Archived

This topic is now archived and is closed to further replies.

Avalanche

How do I connect? lol

Recommended Posts

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.

Share this post


Link to post
Share on other sites
$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. ;)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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]

Share this post


Link to post
Share on other sites

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 6

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

Share this post


Link to post
Share on other sites

doh :o .. 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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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 6

error: 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

Share this post


Link to post
Share on other sites
<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>

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.