1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

phpMyAdmin and mySQL Connection

Discussion in 'Programming & Webmastering' started by Akumos, Apr 10, 2012.

  1. Akumos

    Akumos

    Joined:
    Nov 30, 2008
    Messages:
    548 (0.25/day)
    Thanks Received:
    28
    Location:
    Birmingham, England...
    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?

    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
     
  2. W1zzard

    W1zzard Administrator Staff Member

    Joined:
    May 14, 2004
    Messages:
    15,008 (3.90/day)
    Thanks Received:
    11,877
    it's easiest if you create a php function like
    PHP:
    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 says thanks.
  3. Akumos

    Akumos

    Joined:
    Nov 30, 2008
    Messages:
    548 (0.25/day)
    Thanks Received:
    28
    Location:
    Birmingham, England...
    Thanks wizzard... Do I need to setup $dbname in a variable outside the function?
     
  4. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    13,961 (6.25/day)
    Thanks Received:
    3,803
    Location:
    IA, USA
    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 says thanks.
    Crunching for Team TPU
  5. Akumos

    Akumos

    Joined:
    Nov 30, 2008
    Messages:
    548 (0.25/day)
    Thanks Received:
    28
    Location:
    Birmingham, England...
    Thank you :)

    May have more questions later!
     
  6. Aquinus

    Aquinus Resident Wat-man

    Joined:
    Jan 28, 2012
    Messages:
    6,668 (6.46/day)
    Thanks Received:
    2,325
    Location:
    Concord, NH
    adodb makes a good DB abstraction layer.
    http://adodb.sourceforge.net/

    PHP:
    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.

    PHP:
    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);
     
    Last edited: Apr 10, 2012
    Akumos, Millennium and digibucc say thanks.
  7. digibucc

    digibucc

    Joined:
    May 21, 2009
    Messages:
    4,937 (2.45/day)
    Thanks Received:
    1,502
    i like that aquinus, will be using it shortly :)
     
    Aquinus says thanks.
  8. Akumos

    Akumos

    Joined:
    Nov 30, 2008
    Messages:
    548 (0.25/day)
    Thanks Received:
    28
    Location:
    Birmingham, England...
    Thanks Digi
     
  9. Aquinus

    Aquinus Resident Wat-man

    Joined:
    Jan 28, 2012
    Messages:
    6,668 (6.46/day)
    Thanks Received:
    2,325
    Location:
    Concord, NH
    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.
     
  10. Akumos

    Akumos

    Joined:
    Nov 30, 2008
    Messages:
    548 (0.25/day)
    Thanks Received:
    28
    Location:
    Birmingham, England...
    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

    PHP:
    <?
        
    $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>";
                    
            
          }
        }
        
        
        
     
    ?>
     
    Last edited by a moderator: Apr 18, 2012
  11. Aquinus

    Aquinus Resident Wat-man

    Joined:
    Jan 28, 2012
    Messages:
    6,668 (6.46/day)
    Thanks Received:
    2,325
    Location:
    Concord, NH
    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.

    PHP:
    $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'
    "
    );
     
  12. Jizzler

    Jizzler

    Joined:
    Aug 10, 2007
    Messages:
    3,454 (1.30/day)
    Thanks Received:
    645
    Location:
    Geneva, FL, USA
    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! :D
     

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page