How to use mysql_field_len in PHP 7?

2019-10-03 05:05发布

问题:

I'm new with PHP 7 and updating the code into PHP 7. I got error below when trying to get length as mysql_field_len() as it is not exist in mysqli, so how to solved this?

My problem is in this line >>> $panjang = mysql_field_len($query,0);

Warning: mysql_field_len() expects parameter 1 to be resource, object given in

function WriteCode($tabel, $inisial){
    $server = "localhost";
    $username = "user_db";
    $password   = "password_db";
    $database   = "name_db";
    $connection = mysqli_connect($server,$username,$password);
    if (!$connection) {
        die("Database connection failed: " . mysqli_error($connection));
    }
    $db_select = mysqli_select_db($connection, $database);
    if (!$db_select) {
        die("Database selection failed: " . mysqli_error($connection));
    }
    $query  = mysqli_query($connection,"SELECT * FROM $tabel");
    $panjang = mysql_field_len($query,0);
    $qry = mysqli_query($connection,"SELECT MAX('.$property.') FROM ".$tabel);
    if (!$qry) {
        printf("Error: %s\n", mysqli_error($connection));
        exit();
    }
    $row = mysqli_fetch_array($qry, MYSQLI_NUM);
    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;

Thank You

回答1:

Since mysql_field_len is removed in PHP 7, try the alternatives, mentioned in the docs: http://php.net/manual/en/function.mysql-field-len.php

If you want the same named function, try to implement this one:

function mysqli_field_len($result, $field_offset) {
    $properties = mysqli_fetch_field_direct($result, $field_offset);
    return is_object($properties) ? $properties->length : null;
}


标签: php mysqli