这是什么数据类型要保存在mysql 1-​​4.9 [英] what datatype is this to save on mysql 1 - 4.9

查看:89
本文介绍了这是什么数据类型要保存在mysql 1-​​4.9的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在MySQL数据库上保存范围为1-4.9的样式评级,请问我应该使用哪种数据类型?当我将其另存为十进制(11,0)

时,我一直收到此错误 helpers.php第747行中的

ErrorException: 方法App \ Wasamar \ Rating :: __ toString()必须返回字符串值

我的密码

$allInputs = Input::all();
    $catalogueStyleId = Input::get('catalogueStyleId');
    $userId = Crypt::decrypt( Input::get('user') );
    $rating = Input::get('rating');


    $countUserStyleRating = count( Rating::where('catalogue_style_id',$catalogueStyleId)->where('user_id','=', $userId)->first() );

    if ( $countUserStyleRating == 0 ) {
        # add new rating data

        $rating = new Rating;
        $rating->user_id = $userId;
        $rating->catalogue_style_id = $catalogueStyleId;
        $rating->catalogue_style_rating = $rating;
        $rating->save();

        echo "Your rating has been noted thank you";
    }

解决方案

问题是您的十进制定义没有指定小数点. DECIMAL(11,0)表示您要存储小数点左边的11位数字,之后不存储任何数字.

如果要存储0.1到4.9,则需要将小数定义为DECIMAL(2,1). 2表示要存储两个数字,而1表示要在小数点右边存储这些数字中的1个.

从文档中:

DECIMAL 列的声明语法为 DECIMAL(M,D).这 MySQL 5.7中参数的取值范围如下:

M 是最大位数(精度).它具有一系列 1至65.

D 是小数点( 规模).范围是0到30,并且不能大于 M .

MySQL文档

I want to save style ratings that ranges from 1 - 4.9 on my mysql database please what datatype should i use? and i keep getting this error when i save it as a decimal(11,0)

ErrorException in helpers.php line 747: Method App\Wasamar\Rating::__toString() must return a string value

MY CODE

$allInputs = Input::all();
    $catalogueStyleId = Input::get('catalogueStyleId');
    $userId = Crypt::decrypt( Input::get('user') );
    $rating = Input::get('rating');


    $countUserStyleRating = count( Rating::where('catalogue_style_id',$catalogueStyleId)->where('user_id','=', $userId)->first() );

    if ( $countUserStyleRating == 0 ) {
        # add new rating data

        $rating = new Rating;
        $rating->user_id = $userId;
        $rating->catalogue_style_id = $catalogueStyleId;
        $rating->catalogue_style_rating = $rating;
        $rating->save();

        echo "Your rating has been noted thank you";
    }

解决方案

The problem is that your decimal definition specifies no decimal points. DECIMAL(11,0) means you want to store 11 digits left of the decimal point and none after.

You need to define your decimal as DECIMAL(2,1) if you want to store 0.1 to 4.9. The 2 means you want to store two total digits, and the 1 means you want 1 of those digits right of the decimal point.

From the documentation:

The declaration syntax for a DECIMAL column is DECIMAL(M,D). The ranges of values for the arguments in MySQL 5.7 are as follows:

M is the maximum number of digits (the precision). It has a range of 1 to 65.

D is the number of digits to the right of the decimal point (the scale). It has a range of 0 to 30 and must be no larger than M.

MySQL Documentation

这篇关于这是什么数据类型要保存在mysql 1-​​4.9的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆