Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > PHP database connections

PHP database connections
Thread Tools
derbs
Senior User
Join Date: May 2001
Location: Nottingham, UK
Status: Offline
Reply With Quote
Nov 27, 2003, 10:19 AM
 
Alright guys
is there a way to connect to an access database using PHP? And how do you go about connecting to any database with PHP? Is there a nice simple connection string? I don't want to arse around with ODBC

I need your help mundy and zilla!!!
     
philzilla
Occasionally Useful
Join Date: Jun 2001
Location: Liverpool, UK
Status: Offline
Reply With Quote
Nov 27, 2003, 12:16 PM
 
to simply check you can connect, you can run something like this: [php]<?php
$DBName = "database";
$DBUser = "mysql";
$DBPass = "password";
$DBHost = "localhost";
$link = mysql_connect ( $DBHost, $DBUser, $DBPass )
or die ( "Could notconnect<br><br>" );
print ( "Connected to mysql server successfully<br><br>" );
mysql_select_db ( $DBName )
or die ( "Could not select database<br><br>" );
print ( "Connected to database successfully<br><br>" );
mysql_close ( $link );
?>[/php]you should check out the PHP site, it's well helpful.
there's a ton of info on the mysql site, like this, and some Access info too.
"Have sharp knives. Be creative. Cook to music" ~ maxelson
     
Simon Mundy
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status: Offline
Reply With Quote
Nov 27, 2003, 06:40 PM
 
I'm pretty taken with the PEAR DB library to access databases now - it's all done with some very thorough classes, you can create 'generic' database calls so your code will be fairly portable if you change databases, the support is OK and it comes pre-installed with PHP 4.3.x (IIRC)

An example of how to initialise it:-

[php]

require_once('DB.php');
$dsn = 'mysql://user : password@localhost/your_database';
$sql = DB::connect($dsn);
if (DB::isError($sql)) { die($sql->getMessage()); }

[/php]

Then to retrieve an associative array of all your records from a query would be as simple as...

[php]
$data = $sql->getAssoc("SELECT * FROM yourtable",false,null,DB_FETCHMODE_ASSOC)
[/php]

... where each element of $data would be the key (usually your database record ID) and an associated array (fieldname -> data).

It's lovely and straightforward to use, you can even set up auto-inserts/updates and it has support for transactions (I use it on InnoDB tables in MySQL and it works a treat).

Have a quick browse of the docs in http://pear.php.net/

I don't know if it specifically supports access, but the list of DB's it DOES support is pretty good.
Computer thez nohhh...
     
   
 
Forum Links
Forum 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
Top
Privacy Policy
All times are GMT -4. The time now is 05:52 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,