# phpMyAdmin and mySQL Connection



## Akumos (Apr 10, 2012)

Hi All

I have a mySQL DB and have edited it using phpMyAdmin and woul dnow like to access it on my website... Is this the right connection code?


```
$connection = mysql_connect('localhost', 'admin', 'password')  or exit('Could not connect (' . mysql_errno() . '): ' . mysql_error());
```

Is there a way to export this code from phpMyAdmin control panel?

Thanks


----------



## W1zzard (Apr 10, 2012)

it's easiest if you create a php function like 

```
function connect_db($dbname)
{
	$dbuser = "techpowerup";
  $dbpass = ""; 
  $dbhost = "dbint.techpowerup.com"; 
  
  $db = mysql_connect($dbhost, $dbuser, $dbpass) or die("Couldn't connect to Server");
  mysql_select_db($dbname, $db) or die("Couldn't select database ".$dbname);
  return $db;
}
```

and then call that whenever you need to use the database


----------



## Akumos (Apr 10, 2012)

Thanks wizzard... Do I need to setup $dbname in a variable outside the function?


----------



## FordGT90Concept (Apr 10, 2012)

No, you could just make a connect() function with everything defined inside.  The way he set it up allows him to use the same function to access many databases.


----------



## Akumos (Apr 10, 2012)

Thank you 

May have more questions later!


----------



## Aquinus (Apr 10, 2012)

adodb makes a good DB abstraction layer.
http://adodb.sourceforge.net/


```
include('/path/to/adodb.inc.php');
$DB = NewADOConnection('mysql');
$DB->Connect($server, $user, $pwd, $db);
```

Then if you're working with multiple databases, just make an array or object of NewADOConnection members. It might be wise to make $DB global as well.


```
include('/path/to/adodb.inc.php');
global $DB;
$DB = new stdClass;
$DB->foo = NewADOConnection('mysql');
$DB->bar = NewADOConnection('mysql');
$DB->foo->Connect($server, $user, $pwd, $db);
$DB->bar->Connect($server, $user, $pwd, $someotherdb);
```


----------



## digibucc (Apr 10, 2012)

i like that aquinus, will be using it shortly


----------



## Akumos (Apr 18, 2012)

Thanks Digi


----------



## Aquinus (Apr 18, 2012)

I might add that phpMyAdmin isn't exactly the most secure web service. I wouldn't host it where anyone can get a hold of it. I'm assuming you're working locally on your own machine or in a virtual machine with no access to your web server externally.


----------



## Akumos (Apr 18, 2012)

It's nothing important... The only wasy I know how to test is save it from dreamweaver, upload it using filezilla, then navigate to the url!

In the end, I went for this code... Can anyone tell me (using the same SIMPLE ways i'm using) where and how I would get the password submitted from part1.html and compare it to the username?

Thanks


```
<?
    $dbServer=mysql_connect ("localhost","admin","pw");
    if (!$dbServer) {echo "Failed to connect to MySQL"; exit; }
    
    mysql_select_db("a3484399_ps",$dbServer);
    
    $sql ="SELECT * FROM users";
    $sql.=" WHERE fName=\"".$_POST["searchName"]."\"";  // the space before the WHERE is critical
	
    
    $queryResult=mysql_query($sql);
    
    if (mysql_error())
    {
      echo "Problem with Query<BR>";
      echo "The following error message was returned from MySQL:<BR>";
      echo mysql_error();
      exit;
    }
    
    if (mysql_num_rows($queryResult)==0)
    {
      echo "No users with that name.";
    }
    else
    {
      while ($dbRecord=mysql_fetch_array($queryResult))
      {
        echo "Welcome ".$dbRecord["fName"]."<BR>";
				
		
      }
    }
	
	
	
 ?>
```


----------



## Aquinus (Apr 18, 2012)

A: There is an edit button, use it instead of posting multiple times.

B: What does the Users table look like?

C: You would add it to the SQL query.

D: Why in the world are you using Dreamweaver? If you want to write a web application the right way, I would get virtualbox and install a basic Ubuntu Server with a LAMP stack and edit everything using VIM.

This would help with configuration if you're not familiar with debian based servers.
https://help.ubuntu.com/community/ApacheMySQLPHP

What is your knowledge level as far as PHP and SQL is concerned? I'm assuming you're new to both because of the comment next to the query saying to remember to put a space before the where.

Personally, when I have to put SQL into a variable, I do it like this.


```
$sql = "
    SELECT t.*
    FROM table AS t
        JOIN some_other_table AS o
            ON (t.sharedfield = o.sharedfield)
    WHERE t.field1 = 'foobar'
";

// Or like this just plugging it right into an adodb connection.
$db->Execute("
    SELECT t.*
    FROM table AS t
        JOIN some_other_table AS o
            ON (t.sharedfield = o.sharedfield)
    WHERE t.field1 = 'foobar'
");
```


----------



## Disparia (Apr 18, 2012)

Akumos said:


> It's nothing important... The only wasy I know how to test is save it from dreamweaver, upload it using filezilla, then navigate to the url!



Set up your Remote Server in Dreamweaver and check "Automatically upload files to server on save". Correctly set up, you'll just need to Ctrl+S and refresh your browser. Or if the browser was closed, F12 for Preview in Browser. Saves time and keeps your mind on the code!


----------

