odbc_exec Error

Discussion in 'HTML / PHP / JavaScript / CSS' started by jling, Sep 4, 2008.

  1. I am trying to use PHP to read a file and insert contents into a table in MSServer 2005.I created the table using MS SQL Server Management Studio Express. PHP can open the file, read the line, populate variables,but it got this error in insertion [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'database_name.table_name'., SQL state S0002 in SQLExecDirect in E:\web\my_site_name\htdocs\test2_updates.php on line 37. Can anyone point out what I am missing? Thanks.
     
  2. Bruce

    Bruce DiscountASP.NET Staff

    Based on the error, you are trying to write to a table that doesn't exists.


    can you post your code?





    Bruce

    DiscountASP.NET
    www.DiscountASP.NET
     
  3. Bruce, here is the code. I have verified that my connection to the database was OK since I could query and display the contents of a existing table.
    =================================
    <html>

    <table>
    <?php


    $ConnString="DRIVER={SQL Server};SERVER=sql2k504.discountasp.net;DATABASE=SQL2005_277395_lifttruck";
    $DB_User="SQL2005_277395_lifttruck_user";
    $DB_Passwd="XXXXXXXXXXXX";
    $Connect = odbc_connect( $ConnString, $DB_User, $DB_Passwd ) or die ("Unable to connect to database");

    $file=fopen("trkprpage.txt","r")
    or exit("Unable to open trkprpage.txt!");

    while(!feof($file))
    {
    //echo fgets($file). "";

    $line = fgets($file);

    //$res="";

    //echo $line. "";

    list($doc_no, $family, $family_desc, $truck_type) = explode("|", $line);

    echo "<tr><td>". $doc_no."<td>". $family. "<td>". $family_desc. "<td>". $truck_type. "</tr>";
    $sql = "INSERT INTO $DB_User.trkprpage (doc_no, family, family_desc, truck_type)
    VALUES ($doc_no, '$family', '$family_desc', '$truck_type')";


    $res = odbc_exec( $Connect, $sql );

    if (!$res)
    {
    echo "<tr><td>SQL statement failed with error</td><td></td></tr>";

    }
    else
    {
    echo "<tr><td>One data row inserted</td></tr>";

    }

    }


    fclose($file);
    ?>

    </body>
    </html>
    =========================
     
  4. Bruce

    Bruce DiscountASP.NET Staff

    This line is the problem
    $sql = "INSERT INTO $DB_User.trkprpage (doc_no, family, family_desc, truck_type)
    VALUES ($doc_no, '$family', '$family_desc', '$truck_type')";

    Change it to

    $sql = "INSERT INTO trkprpage (doc_no, family, family_desc, truck_type)
    VALUES ($doc_no, '$family', '$family_desc', '$truck_type')";

    Bruce

    DiscountASP.NET
    www.DiscountASP.NET
     
  5. Thank you, Bruce. But I still get the same error.
     
  6. Bruce

    Bruce DiscountASP.NET Staff

  7. No. I just confirmed that my SQL Server 2005 Studio Express have the same user name.
     

Share This Page