Solved

PHP MS Access, populate drop-down/list box help.

Posted on 2004-09-01
10
665 Views
Last Modified: 2013-12-12
I think this is a easy question, but im still learning php so don't laugh! Im using php to connect to a microsoft access database and i want to populate a drop-down box with the product numbers of the items in a table. I have seen many help pages on how to do this with my_sql but none for ms access as i need to find the equivelent of the "mysql_fetch_array()". I will post the code i have tried but not working below, it just takes ages to load and doesnt populate anything.

form.php

<?php
                         
if (!$conn = new COM("ADODB.Connection"))
        exit("Unable to create an ADODB connection<br>");
    $strConn = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath("databasefile");
    $conn->open($strConn);
      
    $strSQL = "SELECT Part_No FROM products";
      $rs = $conn->execute($strSQL);
      $prodno = $rs->fields['Part_No'];
                              
 ?>
                  <form name="update" method="post" action="deleted.php">
                    <select name="partno" size="1">
                      <?php
                               while (!$rs->EOF)
                               {
   
        print "<option value=\"$prodno->value\"></option>";
            
                               }
                               ?>
                    </select>
                    <input type="submit" name="Submit" value="Delete">
                  </form>
<?php
 }

  ?>
0
Comment
Question by:meclive
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 4

Expert Comment

by:chipple
ID: 11960864
To use a MS Access DB with PHP, you should use ODBC.
Set up an ODBC data source name, and then use PHP's ODBC functions (odbc_*) to access the database. They work pretty much the same as the mysql_* functions.

Doc.: http://www.php.net/manual/en/ref.uodbc.php

Good luck!
0
 
LVL 26

Expert Comment

by:Umesh
ID: 11961330
Hi,

Check out this article, which explained & shown how PHP interact with MS Access

http://www.phpbuilder.com/columns/siddarth20000228.php3


Hope This Helps!
0
 
LVL 26

Expert Comment

by:Umesh
ID: 11961416
try this..

$sql = "SELECT Part_No FROM products";
$rs = $conn->execute($strSQL);



 while (!$rs->EOF) {

    print "<option value=\"$rs->fields[0]\">$rs->fields[0]</option>";

    $rs->MoveNext();  //  Moves to the next row
  }  
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:meclive
ID: 12033917
I have tried the above mentioned code and i just get "Object[0]" in the drop-down box. Also i dont think my hosting allows a ODBC connection so is there any way of doing this? I have upped the points to 250 as i relly need an answer thanks.
0
 

Author Comment

by:meclive
ID: 12034592
points upped to 335, anybody got an idea?
0
 
LVL 26

Expert Comment

by:Umesh
ID: 12034786
Hi,

replace this   c:/test.mdb with your actual database..



It's working fine for me..

<?php

if (!$conn = new COM("ADODB.Connection"))
exit("Unable to create an ADODB connection<br>");
$strConn = "DRIVER={Microsoft access Driver (*.mdb)}; DBQ=" . realpath("c:/test.mdb");
$conn->open($strConn);

$strSQL = "SELECT Part_No FROM products";
     $rs = $conn->execute($strSQL);
     $prodno = $rs->fields["Part_No"];


 ?>
                  <form name="update" method="post" action="deleted.php">
                    <select name="partno" size="1">
                      <?php
                          while (!$rs->EOF)
                          {

        print "<option value=\"$prodno->value\">$prodno->value</option>";
                          $rs->MoveNext();
                          }
                          ?>
                    </select>
                    <input type="submit" name="Submit" value="Delete">
                  </form>


0
 

Author Comment

by:meclive
ID: 12034853
Thanks that works like a charm :0) you definatly get the points. The only problem i have is that i wish the first option in the drop-down box to either be blank or say "select from list".
0
 

Author Comment

by:meclive
ID: 12034866
Also is there anyway i can order the drop down list box to put the results in order as they seem to be in a random order, thanks.
0
 
LVL 26

Accepted Solution

by:
Umesh earned 335 total points
ID: 12034970
try this..


<?php

if (!$conn = new COM("ADODB.Connection"))
exit("Unable to create an ADODB connection<br>");
$strConn = "DRIVER={Microsoft access Driver (*.mdb)}; DBQ=" . realpath("c:/test.mdb");
$conn->open($strConn);

$strSQL = "SELECT Part_No FROM products order by Part_No ASC";
     $rs = $conn->execute($strSQL);
     $prodno = $rs->fields["Part_No"];


 ?>
                  <form name="update" method="post" action="deleted.php">
                    <select name="partno" size="1">
                      <?php
        print "<option value=\"select from list\">select from list</option>";

                          while (!$rs->EOF)
                          {

        print "<option value=\"$prodno->value\">$prodno->value</option>";
                          $rs->MoveNext();
                          }
                          ?>
                    </select>
                    <input type="submit" name="Submit" value="Delete">
                  </form>
0
 

Author Comment

by:meclive
ID: 12035039
Works perfectly, thanks for all your help its much appreciated :0)
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP URL Parameter to simple URL for single folder 16 48
PHP substring 3 50
Code planning methods/tools? 5 56
php time 12 25
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question