How to retrieve data from a SQL Server

Discussion in 'PHP' started by omnii, Jun 15, 2007.

  1. #1
    Can anyone tell me the odbc functions to retrieve and display data from an existing SQL server?

    I tried using odbc_exec, odbc_fetch_row, but that ain't working. The page just comes back blank.

    This is one example of what I tried that didn't work:

    <html>
    <head>

    </head>

    <body>

    <?php


    $conn = odbc_connect('xyz.com','login','pass');

    if ($conn <=0) {
    echo "Error in connection<BR>";
    exit;
    }
    else {
    echo "<P>Connection successful\n";
    };

    $query = 'SELECT * from XYZ_ITEM');

    $result = odbc_exec($conn, $query);

    while(odbc_fetch_row($result)){
    $ITEM=odbc_result($result, "ITEM");
    echo "$ITEM";

    }

    ?>

    </body>
    </html>
     
    omnii, Jun 15, 2007 IP
  2. ProgrammersTalk

    ProgrammersTalk Peon

    Messages:
    684
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    isn't that suppose to be mysql_connect(); :-/
     
    ProgrammersTalk, Jun 15, 2007 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    ^^ He's using ODBC, so why would he use mysql_connect() ?

    Have a look at php.net's odbc_fetch_row() manual. There are some examples that may help.

    Also try putting these lines at the top of your code to make sure all possible errors are displayed:
    
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    
    PHP:
     
    nico_swd, Jun 16, 2007 IP
  4. omnii

    omnii Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I added your code to display errors, but the page still comes back blank. If I just leave the code to connect and return "connection successful" or "error in connection" one of those strings is returned, but nothing else is being displayed when I add anything else.:confused:
     
    omnii, Jun 16, 2007 IP
  5. dankenpo

    dankenpo Guest

    Messages:
    30
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I found the combination of the following lines very useful, as the
    fieldnames will be simply available as variables by their names.

    
    $query="select fieldname from table";
    ...
    $line=mysql_fetch_assoc($query_result);
    extract($line);
    echo $fieldname;
    
    Code (markup):
     
    dankenpo, Jun 16, 2007 IP