Jump to content


Photo

How do I connect? lol


  • Please log in to reply
12 replies to this topic

#1 Avalanche

Avalanche
  • Members
  • PipPipPip
  • Advanced Member
  • 73 posts

Posted 01 April 2003 - 03:56 AM

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.
DEFINE("YOU","Spending Too Much Time On The Computer");

#2 metalblend

metalblend
  • Members
  • PipPipPip
  • Advanced Member
  • 89 posts
  • LocationCalifornia, USA

Posted 01 April 2003 - 05:56 AM

$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. ;)

#3 Avalanche

Avalanche
  • Members
  • PipPipPip
  • Advanced Member
  • 73 posts

Posted 01 April 2003 - 08:07 PM

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.
DEFINE("YOU","Spending Too Much Time On The Computer");

#4 metalblend

metalblend
  • Members
  • PipPipPip
  • Advanced Member
  • 89 posts
  • LocationCalifornia, USA

Posted 01 April 2003 - 09:37 PM

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.

#5 Avalanche

Avalanche
  • Members
  • PipPipPip
  • Advanced Member
  • 73 posts

Posted 01 April 2003 - 09:43 PM

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]
DEFINE("YOU","Spending Too Much Time On The Computer");

#6 Avalanche

Avalanche
  • Members
  • PipPipPip
  • Advanced Member
  • 73 posts

Posted 01 April 2003 - 09:47 PM

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.


DEFINE("YOU","Spending Too Much Time On The Computer");

#7 shivabharat

shivabharat
  • Members
  • PipPipPip
  • Advanced Member
  • 371 posts
  • LocationChennai, India

Posted 01 April 2003 - 09:52 PM

used $dbh not $conn in mysql_query or leave it blank
Knowledge --- Reading Enriches Mind But Sharing Enhances It.[br][br]Note: Before you request help enusre that you have had a look at the tutorials @phpfreaks

#8 metalblend

metalblend
  • Members
  • PipPipPip
  • Advanced Member
  • 89 posts
  • LocationCalifornia, USA

Posted 01 April 2003 - 10:43 PM

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

#9 Avalanche

Avalanche
  • Members
  • PipPipPip
  • Advanced Member
  • 73 posts

Posted 01 April 2003 - 11:07 PM

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.
DEFINE("YOU","Spending Too Much Time On The Computer");

#10 Avalanche

Avalanche
  • Members
  • PipPipPip
  • Advanced Member
  • 73 posts

Posted 01 April 2003 - 11:23 PM

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
DEFINE("YOU","Spending Too Much Time On The Computer");

#11 shivabharat

shivabharat
  • Members
  • PipPipPip
  • Advanced Member
  • 371 posts
  • LocationChennai, India

Posted 01 April 2003 - 11:32 PM

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

Knowledge --- Reading Enriches Mind But Sharing Enhances It.[br][br]Note: Before you request help enusre that you have had a look at the tutorials @phpfreaks

#12 Avalanche

Avalanche
  • Members
  • PipPipPip
  • Advanced Member
  • 73 posts

Posted 01 April 2003 - 11:35 PM

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.
DEFINE("YOU","Spending Too Much Time On The Computer");

#13 shivabharat

shivabharat
  • Members
  • PipPipPip
  • Advanced Member
  • 371 posts
  • LocationChennai, India

Posted 02 April 2003 - 12:02 AM

Download the manual

Nothing can help u better than that :)

http://www.phpfreaks...ticles/75/0.php

Now if u are looking for tutorial try our tutorila section!!
Knowledge --- Reading Enriches Mind But Sharing Enhances It.[br][br]Note: Before you request help enusre that you have had a look at the tutorials @phpfreaks




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users