0

I want to store all items from a field into a PHP Array.

  • The database is an ".accdb" file
  • The connection with the database is through PHP's ODBC functions

This is the table I've been using for my test code (its name is "Posts1"):
enter image description here

This is the latest version of my PHP code, it only returns one result.

<?php
    $dbName = $_SERVER["DOCUMENT_ROOT"] . "/Project6/phptest/SampleUsers.accdb";
    $connection = odbc_pconnect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=" .$dbName, ";", ";");
    if ($connection != true){ exit; };
    $query_string = "SELECT Body FROM Posts1";
    $results = odbc_exec ( $connection , $query_string);
    $output = odbc_result($results, 1);
    odbc_close ( $connection );
    echo $output;
?>

If I change the index of the result I'm trying to access, the code won't work.
Ex: $output = odbc_result($results, 2);
If that did work, I planned to create a loop to transfer the items to an array.

Before this, I also tried using fetch_array but could not make it work. It was especially difficult because I was using JSON to communicate the array between the client and server, which I have no experience with.

The web side I got from here, it was literally copy and paste. Basically the website is just a link and a div: when the link is clicked, some javascript sends off a request for the PHP. The div is updated based on what the PHP echos.

I'm hoping for some guidance, I'm completely new to web development.

EDIT
I've made a working version now, it makes use of odbc_fetch_array instead of odbc_result. Any other answers are still appreciated, if they offer an improvement I'll accept them.

$output = array();
for ($value = array_pop(odbc_fetch_array($results)), $i = 1; 
    $value != false;
    $value = array_pop(odbc_fetch_array($results)), $i++)
{
    $output[$i] = $value;
}
Community
  • 1
  • 1
Alter
  • 3,332
  • 4
  • 31
  • 56
  • Since you're completely new to web development, why don't you drop ODBC and join us at MySQL where cool kids are? – Richie Aug 18 '14 at 23:39
  • Sounds fun, but it looks like it might take some work to transfer over the existing databases (I got stuck with a really large project in an area I know nothing about haha). Would it be easier to solve this type of problem/are there any other reasons you'd suggest it? – Alter Aug 19 '14 at 01:08
  • Well, I have no idea how ODBC works but since I have worked with MySQL for over 5 years now, I can tell that there's no database driver that beats it. You can get lots of resources about MySQL and most developers actually recommend that. Maybe you should wind up that project in ODBC and start learning MySQL for new projects. – Richie Aug 19 '14 at 04:50
  • Alright, I can switch over anytime actually, it's only the solution that matters to me. If you can offer any pointers it would be appreciated. – Alter Aug 19 '14 at 05:05
  • 1
    I'll be glad to help. I believe you have heard about [MySQLi (i stands for improved)](http://php.net/manual/en/book.mysqli.php) and [PHP Data Objects (PDO)](http://php.net/manual/en/book.pdo.php) right? Pick any of the two and start learning it. – Richie Aug 19 '14 at 05:11

0 Answers0