none
Connecting MySQL to PHP script

    Question

  • Hello,

    I try to create a database for my website.  My Webhosting has already installed and set up MySQL and PHP.  Now I have to establish a connection to MySQL from inside a PHP script.  I'm still working with EW1 which has no PHP, is that right?  Do I have to install PHP on my Computer?  What do I have to do to display the data on my webpage?
    Thank you.

    Superotto

     
    Thursday, September 11, 2008 8:37 PM

Answers

  • You don't need PHP installed to write a PHP script because in the end, the PHP script will run on a web server. 

    Having PHP installed allows you to run the scripts on your local machine with a web server installed (like IIS, since EW1 doesn't support PHP with its development server).  EW2's internal development server has PHP support but you still need to install the PHP interpreter for scripts to run locally.

    First for connecting to a MySQL database, you need to create the database and database user information (usually you can do this through your hosting control panel, such as CPanel, commonly used with Linux Hosting) or your host support can set up the db's for you  Then you need to declare these variables (such as those below) in a php file called "config.php" or similar - these are in a separate file for the sake of simplicity and ease of updating.

    Your db connection variables would look something like this:


    /** MySQL settings **/
    define('DB_NAME', 'your_database_name'); //The name of your database.
    define('DB_USER', 'database_username'); //Your database username
    define('DB_PASSWORD', 'password'); //Your database users password
    define('DB_HOST', 'localhost'); //The hostname for your database normally localhost


    On doing a brief Google search I found this site with info about using PHP with MySQL.

    http://www.databasejournal.com/features/mysql/article.php/1469211

    Also the Google results for "connect to mySQL with PHP":

    http://www.google.com.au/search?q=connect+to+mysql+in+php&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a

    You might also do a google search on "displaying mySQL data in a php page"

    http://www.veign.com has some good examples of PHP scripts, it's worth a look.

    I'm sure some of the experts in PHP here can point you in the right direction as well, and it might be worth visiting a specifc PHP newsgroup/forum for advice on the specifics of what you're trying to do such as alt.php and alt.comp.lang.php and alt.comp.databases.mySQL.

    Hope this is of some assistance.




    Andrew M
    Thursday, September 11, 2008 10:27 PM
  • Easiest way to setup a local development machine is to use WAMPWAMP will setup Apache, MySQL and PHP for you so you can practice and test locally.  You always want to test locally before publishing live.

    Connecting to MySQL from PHP is pretty simple.  Could be as simple as;
    $conn = mysql_connect('localhost', 'root', 'password') or die ('Error connecting to mysql'); 
    mysql_select_db('MyDatabase'); 


    --
    Chris Hanscom - Microsoft MVP
    Resource Center | Veign's Blog | Web Development Help
    Thursday, September 11, 2008 10:45 PM

All replies

  • Make sure you have a characterset on every php page other than utf-8 since php can't handle it.

    You will need to install IIS or Apache and PHP then set your pages to preview from that url under Site > Site Settings.

    Go to http://php.net for instructions on how to install it.
    MS MVP Expression http://by-expression.com
    Thursday, September 11, 2008 9:55 PM
  • You don't need PHP installed to write a PHP script because in the end, the PHP script will run on a web server. 

    Having PHP installed allows you to run the scripts on your local machine with a web server installed (like IIS, since EW1 doesn't support PHP with its development server).  EW2's internal development server has PHP support but you still need to install the PHP interpreter for scripts to run locally.

    First for connecting to a MySQL database, you need to create the database and database user information (usually you can do this through your hosting control panel, such as CPanel, commonly used with Linux Hosting) or your host support can set up the db's for you  Then you need to declare these variables (such as those below) in a php file called "config.php" or similar - these are in a separate file for the sake of simplicity and ease of updating.

    Your db connection variables would look something like this:


    /** MySQL settings **/
    define('DB_NAME', 'your_database_name'); //The name of your database.
    define('DB_USER', 'database_username'); //Your database username
    define('DB_PASSWORD', 'password'); //Your database users password
    define('DB_HOST', 'localhost'); //The hostname for your database normally localhost


    On doing a brief Google search I found this site with info about using PHP with MySQL.

    http://www.databasejournal.com/features/mysql/article.php/1469211

    Also the Google results for "connect to mySQL with PHP":

    http://www.google.com.au/search?q=connect+to+mysql+in+php&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a

    You might also do a google search on "displaying mySQL data in a php page"

    http://www.veign.com has some good examples of PHP scripts, it's worth a look.

    I'm sure some of the experts in PHP here can point you in the right direction as well, and it might be worth visiting a specifc PHP newsgroup/forum for advice on the specifics of what you're trying to do such as alt.php and alt.comp.lang.php and alt.comp.databases.mySQL.

    Hope this is of some assistance.




    Andrew M
    Thursday, September 11, 2008 10:27 PM
  • Easiest way to setup a local development machine is to use WAMPWAMP will setup Apache, MySQL and PHP for you so you can practice and test locally.  You always want to test locally before publishing live.

    Connecting to MySQL from PHP is pretty simple.  Could be as simple as;
    $conn = mysql_connect('localhost', 'root', 'password') or die ('Error connecting to mysql'); 
    mysql_select_db('MyDatabase'); 


    --
    Chris Hanscom - Microsoft MVP
    Resource Center | Veign's Blog | Web Development Help
    Thursday, September 11, 2008 10:45 PM