Display results from an MDB file based on a selection using PHP

I hope someone can help me with this?

I have a Joomla installation running and the website looks and works great.

Here’s the problem, the website is for a car dealership, which means they need to display a list of their stock on the floor.

They are using a custom system to manage their stock and this system saves the data to a MS Access database.

I got it to work to a point where I can display a table from the database. (http://www.autodeal.co.za/newsite/used-car-sales-south-africa).

Now when someone clicks on the model, which is a link that will take them to another page which displays only the information relevant to the selected model.

That’s what I can’t figure out. The link works fine and it takes me to the page that I want, but it doesn’t display the data like it’s supposed to.

Please see the code below for connecting to the database and displaying the results:

<?php $dbName = "F:/Domains/autodeal/autodeal.co.za/wwwroot/newsite/db/savvyautoweb.mdb"; // Throws an error if the database cannot be found if (!file_exists($dbName)) { die("Could not find database file."); } // Connects to the database // Assumes there is no username or password $conn = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$dbName", '', ''); // This is the query // You have to have each column you select in the format tableName.[ColumnName] $sql = "SELECT Make, Model, Year, Price, SpecialPrice, Branch, StockNO FROM Vehicle ORDER BY Make"; // Runs the query above in the table $rs = odbc_exec($conn, $sql); echo "\t" . "\n"; echo "\t" . "Make Model Year Price Special Price Location Stock Number" . "\n"; while (odbc_fetch_row($rs)) { $make = odbc_result($rs, Make); $model = odbc_result($rs, Model); $year = odbc_result($rs, Year); $price = odbc_result($rs, Price); $specialPrice = odbc_result($rs, SpecialPrice); $branch = odbc_result($rs, Branch); $stockNo = odbc_result($rs, StockNO); echo "\t" . "\n"; echo "\t\t" . "" . $make . " " . $model . " " . $year . " " . $price . " " . $specialPrice . " " . $branch . " " . $stockNo . "\n"; echo "\t" . "\n"; } odbc_free_result($rs); odbc_close($conn); // This message is displayed if the query has an error in it if (!$rs) { exit("There is an error in the SQL!"); } ?>

Please see the code below to display a specific vehicle information from the table based on a selection made from the above script.

<?php $dbName = "F:/Domains/autodeal/autodeal.co.za/wwwroot/newsite/db/savvyautoweb.mdb"; // Throws an error if the database cannot be found if (!file_exists($dbName)) { die("Could not find database file."); } // Connects to the database // Assumes there is no username or password $conn = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$dbName", '', ''); // This is the query // You have to have each column you select in the format tableName.[ColumnName] $selected_id = intval($_GET['Id']); $sql = "SELECT Id, Make, Model, Year, Price, SpecialPrice, Branch, StockNO, MainPic FROM Vehicle WHERE Id = Id"; // Runs the query above in the table $rs = odbc_exec($conn, $sql); $id = odbc_result($rs, Id); $make = odbc_result($rs, Make); $model = odbc_result($rs, Model); echo $make; echo $model; $image_path_main = ""; echo "this is a test"; odbc_free_result($rs); odbc_close($conn); // This message is displayed if the query has an error in it if (!$rs) { exit("There is an error in the SQL!"); } ?>

EDIT So I’ve updated the above code based an answer received, but the individual records aren’t displayed. I printed a test line and that works fine, so that tells me that there’s an issue with the query? The thing is, the query works fine to display all the records in a table, but I need to display a single record when that record has been clicked.

Furthermore, the $mainPic variable above is referencing the image name from the database. The actual image isn’t saved in the database; it’s in a different location. I assume I need to create a variable with the actual path of the image and use the reference variables above to display the image, but it’s not working.

So to recap, I need some help displaying all information from the database based on a selection.

For example: in the table, I select 323i. On a new page, I need to display all the information that’s in the database about the 323i on a new page.

Is that doable and if so, could anyone please assist me in this matter.

Thank you very much in advance.

I am pretty Anti-Microsoft when it comes to web related application uses so I wont be any help with your current problem. Should the powers that be ever decide to go with a more web friendly setup, I would be happy to assist. It would take very little time for me to build your current system in a LAMP setup.

My biggest problem with an MS setup is it requires MS stuff to work. A LAMP app will run on anything, including MS servers.

Since you are from a car dealership, I suspect that system is from back in the day when Access was a common database to do things.

Talk to the GM about upgrading to 2014. There are a lot of great features we can add such as videos, auto responses to customer inquirys, live help, ebay intergration, craigslist integration, reports and charts, facebook and twitter integration…and the list goes on.

Sponsor our Newsletter | Privacy Policy | Terms of Service