DiigIT | IT Community
No Profile Image
Welcome Guest
New User? Register | Login

Error in connection to MYSQL server through PHP

By: Shashank | 05 Sep 2008 1:32 pm

Hello,

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

Comments

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!

# 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" . mysql_error( ));

# loop through any results we find
while ($d = mysql_fetch_ assoc($r) )
{
# display results in a primitive way
printf("
 %s
< 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.
By: PHP Guru | 05 Sep 2008
Here is a PHP function that I wrote that handles everything,
(1)..database open/connect
(2)..database query
(3)..database close
============ ========= ========= ========= ========= ========= ====

function thesqlquery( $sqlquery)
{
include "./files-includes/ config.inc. php";
//echo "
Trying to connect to MySQL";
//echo '
Hostname:(' . $host . ') UserID: (' . $user . ') Password:(' .
$passwd . ')';
if ( !$dbh = mysql_connect( $host, $user, $passwd) )
{
$errmsg = mysql_errno( $dbh) . ": " . mysql_error( $dbh);
echo "
" . $errmsg;
exit();
}
//echo '
DatabaseName: (' . $dbname . ')';
$db_selected = mysql_select_ db($dbname, $dbh);
if (!$db_selected) {
$errmsg = mysql_errno( $dbh) . ": " . mysql_error( $dbh);
echo "
" . $errmsg;
exit();
}
//echo "
SQLQUERY: " . $sqlquery;
if ( !$sqlresult = mysql_query( $sqlquery, $dbh) )
{
echo "
SQLQUERY: " . $sqlquery;
$errmsg = mysql_errno( $dbh) . ": " . mysql_error( $dbh);
echo "
" . $errmsg;
exit();
}
$numrows = mysql_num_rows( $sqlresult) ;
//echo "
NumberOfRows: " . $numrows;
mysql_close( $dbh);
return $sqlresult;
}

============ ========= ========= ========= ========= ========= ====

------------ --------- --------- --------- --------- --------- -
The above mentioned config.inc.php file contains the following global
variables.
------------ --------- --------- --------- --------- --------- -
$host="localhost" ;
$user="your mysql userid/username" ;
$passwd="yourmysql password";
$dbname="yourmysql database name";
?>

------------ --------- --------- --------- --------- --------- -
By: PHP Guru | 05 Sep 2008

Leave a comment

Enter the text in the image
img
Can't read?
Type the characters you see in the picture below.


Close Move