Jump to content

Switching from ASP to PHP...Need Help connecting to Access Database


Recommended Posts

So i built a website in ASP a while ago, and used connection strings to connect to a database in Microsoft Access.

 

My connection string looked like this in the page where i would display data from the table:

 

<%

' Name of the Accessdb being read

accessdb="database/pricelist2.mdb"

 

 

' Connect to the db with a DSN-less connection

cn="DRIVER={Microsoft Access Driver (*.mdb)};"

cn=cn & "DBQ=" & server.mappath(accessdb)

 

' Create a server recordset object

Set rs = Server.CreateObject("ADODB.Recordset")

 

' Select specific fields from the table the_bird

sql = "SELECT Coin1, Coin2, Coin3, Coin4, Coin5, Coin6, Coin7, Coin8, Coin9 FROM contact;"

 

' Execute the sql

rs.Open sql, cn

%>

 

But now i just need some help on how to connect to the Access database using PHP and Linux Based Hosting.

 

Can anyone help?

Thanks!  So you are recommending using a mysql database?

 

I would second Thorpe's suggestion. But if you are stuck on ASP stuff, SQLServer is way better then access as well. Access is just...well its own little demon. If you are switching to Linux / PHP, definitely switch to MySQL as well.

 

And yea, I used to develop in ASP / SQLServer / VB C# too. But my boss used SQLServer, which was way better then access. I still hated coding in VB when I had to though. :)

Thanks guys, so i have decided to switch over to mysql based on your recommendations.  I created a simple table with two fields, coin and price.  But i'm trying to just connect to the database (using godaddy hosting) and nothing is showing up.  Maybe someone can help with my string or page to see if something looks off??

 

Here's my page:

 

<?php

//Connect To Database

$hostname='getliquid.db.4760604.hostedresource.com';

$username='getliquid';

$password='********';

$dbname='getliquid';

$usertable='contact';

$yourfield = 'coin';

 

mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');

mysql_select_db($dbname);

 

$query = 'SELECT * FROM $usertable';

$result = mysql_query($query); 

if($result) {

    while($row = mysql_fetch_array($result)){

        $name = $row['$yourfield'];

        echo 'Name: '.$name;

    }

}

?>

 

Here's the page  (As you can see nothing is showing up);

 

http://startuplab.net/wordpress/contact_test.php

 

Thanks in advance!

$query = 'SELECT * FROM $usertable';

 

your $usertable variable (or any variable for that matter) will not parse when within single-quotes.  two options:

 

1. [concatenation]

$query = 'SELECT * FROM ' . $usertable;

 

2.

 (most common practice)
[code]$query = "SELECT * FROM $usertable";[/code]

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.