MYSQL Highest Field Value
#1

Hello Everyone,

I want to get the highest value of a field in a MYSQL table.

I have this custom function to return the highest value of a field from the SQLite table. It works perfectly, Here is the function in SQLite:

pawn Код:
stock sqlite_highest_field(DB:db, fieldname[], tablename[])
{
    new querys[128], DBResult:qresult, id = -1, queryresult[128];
    format(querys, sizeof(querys), "SELECT max( `%s` ) FROM `%s`", fieldname, tablename);
    qresult = db_query(db, querys);
    db_get_field(qresult,0,queryresult,128);
    id = strval(queryresult);
    db_free_result(qresult);
    return id;
}
My friend 'Ronaldo_raul™' helped me to code a new function for MYSQL. But it always return -1.

Is there anyone who can fix this function? so that it will return the Highest value of a field in a table??

pawn Код:
stock name ( fieldname [] , tablename [] )
{
    new qry [ 128 ] , val = -1 ;
    format( qry , 128 , "SELECT max( `%s` ) FROM `%s`" , fieldname , tablename ) ;
    mysql_query ( qry ) ;
    mysql_store_result ( ) ;
    new rows = mysql_num_rows ( ) ;
    mysql_free_result ( ) ;
    if(!rows) return 0 ;
    else return val ;
}
Thank You
Ballu Miaa
Reply
#2

You forgot to fetch the field into the integer.
pawn Код:
stock name ( fieldname [] , tablename [] )
{
    new qry [ 128 ] , val = -1 ;
    format( qry , 128 , "SELECT max( `%s` ) FROM `%s`" , fieldname , tablename ) ;
    mysql_query ( qry ) ;
    mysql_store_result ( ) ;
    new rows = mysql_num_rows ( ) ;
    mysql_free_result ( ) ;
    if(!rows) return -1 ;
    val = mysql_fetch_int ( ) ;
    return val;
}
Reply
#3

Quote:
Originally Posted by VincentDunn
Посмотреть сообщение
You forgot to fetch the field into the integer.
pawn Код:
stock name ( fieldname [] , tablename [] )
{
    new qry [ 128 ] , val = -1 ;
    format( qry , 128 , "SELECT max( `%s` ) FROM `%s`" , fieldname , tablename ) ;
    mysql_query ( qry ) ;
    mysql_store_result ( ) ;
    new rows = mysql_num_rows ( ) ;
    mysql_free_result ( ) ;
    if(!rows) return -1 ;
    val = mysql_fetch_int ( ) ;
    return val;
}
Okay thanks for that Vinci but there was one more fault, it worked like:

pawn Код:
stock name ( fieldname [] , tablename [] )
{
    new qry [ 128 ] , val = -1 ;
    format( qry , 128 , "SELECT max( `%s` ) FROM `%s`" , fieldname , tablename ) ;
    mysql_query ( qry ) ;
    mysql_store_result ( ) ;
    new rows = mysql_num_rows ( ) ;
    if(!rows) return -1 ;
    val = mysql_fetch_int ( ) ;
    mysql_free_result ( ) ;
    return val;
}
Reply
#4

Quote:
Originally Posted by VincentDunn
Посмотреть сообщение
You forgot to fetch the field into the integer.
pawn Код:
stock name ( fieldname [] , tablename [] )
{
    new qry [ 128 ] , val = -1 ;
    format( qry , 128 , "SELECT max( `%s` ) FROM `%s`" , fieldname , tablename ) ;
    mysql_query ( qry ) ;
    mysql_store_result ( ) ;
    new rows = mysql_num_rows ( ) ;
    mysql_free_result ( ) ;
    if(!rows) return -1 ;
    val = mysql_fetch_int ( ) ;
    return val;
}
Damn, I forgot the main part

@Ballu - You must have told me it returned -1

e.e
Reply
#5

Quote:
Originally Posted by Ronaldo_raul™
Посмотреть сообщение
Damn, I forgot the main part

@Ballu - You must have told me it returned -1

e.e
Uhh My bad bro! you did half of the work for me. I appreciate it. Just dont want to burden you with this.

Anyways thanks for helping me in LoadingBusiness also.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)