Skip to content

OUT_BIN

Jurek Muszyński edited this page Mar 28, 2024 · 7 revisions

void OUT_BIN(const char *data, long len)

Description

Writes binary data to output buffer. Typical usage would be to serve an image from the database.

Initially output buffers are of OUT_BUFSIZE size (memory model-dependent) and they may or may not be resized if necessary, depending on OUTxxx compilation switch.

Returns

None

Example

int show_image(int user_id, int img_id)
{
    int             ret=OK;
    char            sql[1024];
    MYSQL_RES       *result;
    MYSQL_ROW       row;
    unsigned long   *lengths;

    DBG("show_image");

    sprintf(sql, "SELECT fname, bcontent FROM images WHERE user_id=%d AND img_id=%d", user_id, img_id);
    DBG("sql: %s", sql);
    mysql_query(G_dbconn, sql);
    result = mysql_store_result(G_dbconn);
    if ( !result )
    {
        ERR("%u: %s", mysql_errno(G_dbconn), mysql_error(G_dbconn));
        return ERR_INT_SERVER_ERROR;
    }

    row = mysql_fetch_row(result);

    if ( !row )
    {
        mysql_free_result(result);
        WAR("Record not found");
        return ERR_NOT_FOUND;
    }

    lengths = mysql_fetch_lengths(result);

    OUT_BIN(row[1], lengths[1]);

    RES_CONTENT_TYPE_FROM_FILE_EXTENSION(row[0]);

    DBG("File: [%s], size = %ul", row[0], lengths[1]);

    mysql_free_result(result);

    return OK;
}
Clone this wiki locally