techPowerUp! Forums

Go Back   techPowerUp! Forums > Software > Programming & Webmastering

Reply
 
Thread Tools
Old Apr 10, 2012, 08:10 AM   #1
Akumos
200 Posts
 
Akumos's Avatar
 
Join Date: Nov 2008
Location: Birmingham, England...
Posts: 472 (0.29/day)
Thanks: 72
Thanked 27 Times in 22 Posts

System Specs

phpMyAdmin and mySQL Connection

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
__________________
Akumos is offline  
Reply With Quote
Old Apr 10, 2012, 08:16 AM   #2
W1zzard
Benevolent Dictator
 
W1zzard's Avatar
 
Join Date: May 2004
Location: Stuttgart, Germany
Posts: 13,764 (4.18/day)
Thanks: 184
Thanked 10,209 Times in 3,157 Posts
Send a message via ICQ to W1zzard Send a message via AIM to W1zzard Send a message via MSN to W1zzard

System Specs

it's easiest if you create a php function like
PHP Code:
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
W1zzard is offline  
Reply With Quote
The Following User Says Thank You to W1zzard For This Useful Post:
Old Apr 10, 2012, 09:01 AM   #3
Akumos
200 Posts
 
Akumos's Avatar
 
Join Date: Nov 2008
Location: Birmingham, England...
Posts: 472 (0.29/day)
Thanks: 72
Thanked 27 Times in 22 Posts

System Specs

Thanks wizzard... Do I need to setup $dbname in a variable outside the function?
__________________
Akumos is offline  
Reply With Quote
Old Apr 10, 2012, 10:20 AM   #4
FordGT90Concept
"I go fast!1!11!1!"
 
FordGT90Concept's Avatar
 
Join Date: Oct 2008
Location: IA, USA
Posts: 10,574 (6.29/day)
Thanks: 1,752
Thanked 2,596 Times in 1,960 Posts

System Specs

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.
__________________
Golden Rule of Programming: Never assume.

try { SteamDownload(); }
catch (Steamception ex) { RageQuit(); }
FordGT90Concept is offline  
Crunching for Team TPU
Reply With Quote
The Following User Says Thank You to FordGT90Concept For This Useful Post:
Old Apr 10, 2012, 10:23 AM   #5
Akumos
200 Posts
 
Akumos's Avatar
 
Join Date: Nov 2008
Location: Birmingham, England...
Posts: 472 (0.29/day)
Thanks: 72
Thanked 27 Times in 22 Posts

System Specs

Thank you

May have more questions later!
__________________
Akumos is offline  
Reply With Quote
Old Apr 10, 2012, 10:46 AM   #6
Aquinus
3500 Posts
 
Aquinus's Avatar
 
Join Date: Jan 2012
Location: Dover, New Hampshire, USA
Posts: 4,242 (8.86/day)
Thanks: 1,265
Thanked 1,322 Times in 981 Posts

System Specs

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

PHP Code:
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 Code:
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); 
__________________
MyHeat

Last edited by Aquinus; Apr 10, 2012 at 12:03 PM.
Aquinus is online now  
Crunching for Team TPU
Reply With Quote
The Following 3 Users Say Thank You to Aquinus For This Useful Post:
Old Apr 10, 2012, 12:01 PM   #7
digibucc
3500 Posts
 
digibucc's Avatar
 
Join Date: May 2009
Location: In the mountains :) Adirondacks in NY (US)
Posts: 3,705 (2.54/day)
Thanks: 4,534
Thanked 1,442 Times in 1,038 Posts

System Specs

i like that aquinus, will be using it shortly
__________________

Donate to TPU TeamSpeak Server

TPU TS: ts21.gameservers.com:9207

PSN / XBL / Steam = digibucc | Origin / BF3 = digibuc
digibucc is offline  
Reply With Quote
The Following User Says Thank You to digibucc For This Useful Post:
Old Apr 18, 2012, 08:46 AM   #8
Akumos
200 Posts
 
Akumos's Avatar
 
Join Date: Nov 2008
Location: Birmingham, England...
Posts: 472 (0.29/day)
Thanks: 72
Thanked 27 Times in 22 Posts

System Specs

Thanks Digi
__________________
Akumos is offline  
Reply With Quote
Old Apr 18, 2012, 10:16 AM   #9
Aquinus
3500 Posts
 
Aquinus's Avatar
 
Join Date: Jan 2012
Location: Dover, New Hampshire, USA
Posts: 4,242 (8.86/day)
Thanks: 1,265
Thanked 1,322 Times in 981 Posts

System Specs

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.
__________________
MyHeat
Aquinus is online now  
Crunching for Team TPU
Reply With Quote
Old Apr 18, 2012, 11:35 AM   #10
Akumos
200 Posts
 
Akumos's Avatar
 
Join Date: Nov 2008
Location: Birmingham, England...
Posts: 472 (0.29/day)
Thanks: 72
Thanked 27 Times in 22 Posts

System Specs

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 Code:
<?
    $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 Tatty_One; Apr 18, 2012 at 12:16 PM. Reason: Triple post madness! Keep it tidy
Akumos is offline  
Reply With Quote
Old Apr 18, 2012, 12:00 PM   #11
Aquinus
3500 Posts
 
Aquinus's Avatar
 
Join Date: Jan 2012
Location: Dover, New Hampshire, USA
Posts: 4,242 (8.86/day)
Thanks: 1,265
Thanked 1,322 Times in 981 Posts

System Specs

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 Code:
$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'
"
); 
__________________
MyHeat
Aquinus is online now  
Crunching for Team TPU
Reply With Quote
Old Apr 18, 2012, 02:26 PM   #12
Jizzler
2000 Posts
 
Jizzler's Avatar
 
Join Date: Aug 2007
Location: Geneva, FL, USA
Posts: 3,010 (1.43/day)
Thanks: 567
Thanked 606 Times in 487 Posts

System Specs

Quote:
Originally Posted by Akumos View Post
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!
Jizzler is offline  
Reply With Quote
Reply


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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Wired LAN connection and USB wireless connection? squelchy451 Networking & Security 18 Feb 3, 2012 03:31 PM
Unidentified wireless network connection on laptop(local only connection) Rado D Networking & Security 5 Jun 2, 2011 11:22 AM
Setting up WordPress using phpMyAdmin on 1and1 ChiSox Programming & Webmastering 3 Feb 24, 2010 05:16 AM
some html php and mysql mixed question r9 Programming & Webmastering 2 Aug 30, 2009 04:24 PM
help needed with phpmyadmin r9 Linux / BSD / Mac OS X 9 Mar 14, 2009 06:33 PM


All times are GMT. The time now is 10:56 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
no new posts