autocode function error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mfuada
Forum Newbie
Posts: 3
Joined: Fri May 01, 2009 2:16 am

autocode function error

Post by mfuada »

Hi...
I try to implement this code below to build a function that will get me an autocode such as :SM001,SM002,SM003, and so on, for my textbox, but when i run my script i've got tons of list of warning and errors, i'm using php 5, and microsoft sql server, this is my code:

Code: Select all

 
function kdauto($tabel, $inisial){
    $conn_kd    = odbc_connect("server","usroffc","password");  
    $sql_kd     = ("SELECT * FROM $tabel");
    $struktur = odbc_exec($conn_kd,$sql_kd);
    $field    = odbc_field_name($struktur,0);
    $panjang  = odbc_field_len($struktur,0);
    
    $conn_lib  = odbc_connect("server","usroffc","password");   
    $sql_lib = ("SELECT max(".$field.") FROM ".$tabel); 
    $qry_lib = odbc_exec($conn_lib,$sql_lib);
    $row     = odbc_fetch_array($qry_lib);
    
    if ($row[0] =="") {
    $angka=0;
    }
    else {
        $angka = substr($row[0], strlen($inisial));
        }
    $angka++;
    $angka = strval($angka);
    $tmp = "";
    for($i=1; $i<=($panjang-strlen($inisial) - strlen($angka)); $i++) {
        $tmp=$tmp."0";
        }
        return $inisial.$tmp.$angka;
    }
 
could anybody tell me where i've done wrong??

this is the error message:
<br /><b>Warning</b>: odbc_field_name() [<a href='function.odbc-field-name'>function.odbc-field-name</a>]: Field numbering starts at 1 in <b>C:\xampp\htdocs\TokoBuku\inc.librari.php</b> on line <b>6</b><br />
<br /><b>Warning</b>: odbc_field_len() [<a href='function.odbc-field-len'>function.odbc-field-len</a>]: Field numbering starts at 1 in <b>C:\xampp\htdocs\TokoBuku\inc.librari.php</b> on line <b>7</b><br /><br /><b>Warning</b>: odbc_exec() [<a href='function.odbc-exec'>function.odbc-exec</a>]: SQL error: [Microsoft][ODBC SQL Server Driver][SQL Server]The max function requires 1 arguments., SQL state 37000 in SQLExecDirect in <b>C:\xampp\htdocs\TokoBuku\inc.librari.php</b> on line <b>11</b><br /><br /><b>Warning</b>: odbc_fetch_array(): supplied argument is not a valid ODBC result resource in <b>C:\xampp\htdocs\TokoBuku\inc.librari.php</b> on line <b>12</b><br />SM1
Last edited by Benjamin on Wed May 20, 2009 7:55 am, edited 1 time in total.
Reason: Added [code=php], [quote] tags.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: autocode function error

Post by Darhazer »

Read the error message and the documentation of the functions 8)

Code: Select all

$field    = odbc_field_name($struktur,0);
$panjang  = odbc_field_len($struktur,0);
Should be:

Code: Select all

$field    = odbc_field_name($struktur,1);
$panjang  = odbc_field_len($struktur,1);
Post Reply