--- bryan_is_south <[EMAIL PROTECTED]> wrote:

> I just installed MySQL and could access it on the command line, but
> I'm having trouble connecting through PHP.
> 
> In the setup, there was nowhere where I had to choose a username or
> where I was given a username, so that might be the problem.
> 
> Also, would it be worth it for me to try using phpMyAdmin instead of
> MySQL?
> 
> Any help is much appreciated.
> 
> Thanks in advance
> 
> -bryan


Some of this depends on which operating system you are using.  For example, the
recommended installation is LAMP (Linux-Apache-MySQL-PHP).  On an RPM-based
Linux distribution (eg Red Hat, Fedora, CentOS) you may be able to use the yum
command to install some needed parts.  For example, sometimes you have MySQL,
PHP and Apache all working but PHP can't connect to MySQL because it is missing
the module needed for that.

sudo yum install php-mysql

It will ask you for your ordinary, but privileged, user account password.  That
is, the user hast to be in /etc/sudoers with permissions to run sudo commands.

If you have used a big package installer like xampp to install the programs,
this part may have been done for you.

The basics of a MySQL connection look like the code below.  Note that when you
first install MySQL, the only user which is created is called 'root' and it has
no password.  Do not leave it this way!

<?php
# localhost is the location of the server if it is on the same machine as
Apache
# root is a MySQL user name
# the third argument holds the password.  Initially "root" has no password.
$db = mysql_connect("localhost", "root", "");

# the database known as mysql is used by the server to keep track of tables,
etc
# it is OK for this experiment
mysql_select_db("mysql");

# define the query
$q  = "SELECT * FROM user";

# send the query to the server with some handy error reporting
$r  = mysql_query($q, $db) or die("$q<hr>" . mysql_error());

# loop through any results we find
while ($d = mysql_fetch_assoc($r))
{
 # display results in a primitive way
 printf("<pre>%s</pre><hr>\n", print_r($d, true));
}
?>

Running this program should tell you if you can connect to the database.  You
may need to use the user and password provided to you if you don't admin the
server.

phpMyAdmin is an OK program which can do some things very well.  Some people
lean on it as a crutch too much and don't take the time to understand MySQL
queries very well.  It's a little like people who expect DreamWeaver to write
their PHP code for them; they never gain a deep and useful understanding of
PHP.

James

Reply via email to