• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 699
  • Last Modified:

PHPMyAdmin - connect to MySQL

Hi,

I am learning how PHPMyAdmin works, but am struggling a bit.  I managed to get Xampp installed which gives me PHPMyAdmin and Apache server.  Now I need to connect to MySQL with the constants given below.  Can anyone give me instructions on how to do this?
<?php
// MySQL server
define('DB_HOST', '');

// Database name
define('DB_NAME', '');

// MySQL username
define('DB_USER', '');

// MySQL password
define('DB_PASS', '');
?>

Open in new window

0
NerishaB
Asked:
NerishaB
1 Solution
 
Dave BaldwinFixer of ProblemsCommented:
Here is the mysql-connect page http://us3.php.net/manual/en/function.mysql-connect.php .  After you complete the definitions for the constants, you use them in place of the literals frequently used.  Examples of both below.
Literals
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

Constants
<?php
$link = mysql_connect(DB_HOST, DB_USER, DB_PASS);
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

Open in new window

0
 
Dave BaldwinFixer of ProblemsCommented:
After thinking for a second, I'll add this.  You do have to create thru phpmyadmin a database of your own with tables for your data and a user with permissions to use that database.  MySQL connections are based on the user permissions.  If you don't have a user that is allowed to connect to the server and database, your connection won't work.  http://dev.mysql.com/doc/refman/5.1/en/adding-users.html

Note that users can have 'localhost', 'anyhost', or a specific host connection.  It is common to create the same user and password with both 'localhost' and 'anyhost' permissions.  It is Not recommended to use root to access your database.  If you were to put your database on shared hosting somewhere, you would not be allowed to use 'root' at all.
<?php
// MySQL server
define('DB_HOST', 'localhost');

// Database name
define('DB_NAME', 'yourDB');

// MySQL username
define('DB_USER', 'yourUser');

// MySQL password
define('DB_PASS', 'yourPWD');
?>

Open in new window

0
 
ropennerCommented:
This should give enough hints to figure it out:

Once Xampp is installed you should be able to use your web browser (IE, firefox, safari, chrome) to view a website located at http://localhost/phpmyadmin.

http://localhost/phpmyadmin

or for example if you used another port as the default apache port

http://localhost:81/phpmyadmin/

From there you can setup your databases: create new, add tables etc.
______________________________________
for WAMP there is a config file that gives what all the local settings are for host and port

    C:\wamp\apps\phpmyadmin3.3.9\config.inc.php

it looks like this

<?php

/* Servers configuration */
$i = 0;

/* Server: localhost [1] */
$i++;
$cfg['Servers'][$i]['verbose'] = 'localhost';
$cfg['Servers'][$i]['host'] = 'localhost';
$cfg['Servers'][$i]['port'] = '';
$cfg['Servers'][$i]['socket'] = '';
$cfg['Servers'][$i]['connect_type'] = 'tcp';
$cfg['Servers'][$i]['extension'] = 'mysqli';
$cfg['Servers'][$i]['auth_type'] = 'config';
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = '????????'; // there will be an actual password in here not question marks
$cfg['Servers'][$i]['AllowNoPassword'] = false;
________________________________________

In PHP now to access those databases and tables

<?php
// fill in these five with your settings
$host='';
$user='';
$password='';
$databasename='';
$table='';


$database = new mysqli();

$database->connect($host, $user,$password,$databasename);
$sql = "SELECT * FROM $table";
$statement_handle = $database->query($sql);
while ($value = $statement_handle->fetch_row()) {
      for ($i=0; $i < count($value); $i++) {

            print "\t{$value[$i]}";

      }
}

$database->close();
0
 
Lukasz ChmielewskiCommented:
Usually - if you did nothing, the setup would be like this:
host: localhost
user: root
password: (empty)
0
 
NerishaBAuthor Commented:
Thanks alot
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Tackle projects and never again get stuck behind a technical roadblock.
Join Now