By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,017 Members | 1,169 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,017 IT Pros & Developers. It's quick & easy.

How to connect to MySQL

P: n/a
AP
I have not seen any good examples on how to connect to a mySQL db. My
website uses phpMyAdmin to administer the SQL databases. What confuses
me is where are the databases that I create located? For example, if my
website is www.mywebsite.com and I have installed myphpadmin in the
folder tkd, now using this I have created two databases, one named
'acct' with a single table named 'persons', and another database named
'terms' with a single table named accounts. How would I specify in php
that I wanted to connect to the acct database and pull from the persons
table?

Thanks

Oct 27 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Phpmyadmin is a web base mysql databse administration tool, it doesn't
matter where u install it under your webroot. It helps you to
adminstrate mysql db using a web browser insted of using command line
mysql> tool.

When you create a db using phpmyadmin or in any other way, mysql stores
the data files in a directory (Data Directory) which is specified in
my.ini file.

After adding a user with appropriate privileges you can connect to
mysql localy or remotely.

If you are going to use only one table you can select that db after
connecting to mysql like,

//copy paste from php manual
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Not connected : ' . mysql_error());
}

// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}

you can use mysql_db_query() function to send a query to terms database
while acct is selected.

You may want to look at php manuel...

Oct 27 '05 #2

P: n/a
yes you can use mysql_db_query function as,
............
mysql_connect($hostname, $user, $passwd);

$result = mysql_db_query($db1, $query1);
$row1 = mysql_fetch_object($result);

$result = mysql_db_query($db2, $query2);
$row2 = mysql_fetch_object($result);
.............

Oct 27 '05 #3

P: n/a
gd
mysql_connect($hostname, $user, $passwd);

$result = mysql_db_query($db1, $query1);
$row1 = mysql_fetch_object($result);
$result = mysql_db_query($db2, $query2);
$row2 = mysql_fetch_object($result);

Oct 27 '05 #4

P: n/a
Botan Guner,

I have a similar problem and I use exactly your code

//copy paste from php manual
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Not connected : ' . mysql_error());
}
<

and I get
Parse error: parse error, unexpected T_IF in
my program (or your program or the manual's program)
on line
'if (!$link) {'

Something must be wrong in my setup. Can you give me a hint?
Lennart
Oct 28 '05 #5

P: n/a
Lennart Björk wrote:
Botan Guner,

I have a similar problem and I use exactly your code
>

//copy paste from php manual
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Not connected : ' . mysql_error());
}
<

and I get
Parse error: parse error, unexpected T_IF in
my program (or your program or the manual's program)
on line
'if (!$link) {'

Something must be wrong in my setup. Can you give me a hint?
Lennart


Lennart,

I don't see an error there. I suspect it's someplace earlier in your
code. Probably mismatched single quotes.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Oct 28 '05 #6

P: n/a
Jerry,
I suspect it's someplace earlier in your code


You are right and the error was mine. I forgot a semicolon.

Lennart
Oct 31 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.