PHP SQL query error

前端 未结 6 2031
忘了有多久
忘了有多久 2021-01-26 07:26

I have some problem with type conversation in this code (working with Facebook PHP SDK 3.0.1):

$page_id = 192485754113829;
$post_limit = 2;

$query = \"select po         


        
相关标签:
6条回答
  • 2021-01-26 07:53

    It has nothing to do with type-conversion, it's just your quotes that are wrong. The code should look like this:

    $page_id = 192485754113829;
    $post_limit = 2;
    
    $query = "select post_id, source_id, actor_id, target_id, created_time, likes, message, attachment, comments from stream where source_id = ".$page_id." LIMIT ".$post_limit."";
    
    0 讨论(0)
  • 2021-01-26 08:06

    You think you do string concatenation, but you don't. The dots are inside the double quoted string and hence have no special meaning. They are not valid in a SQL statement.

    As variables are parsed in double quoted strings you can do:

    "select (...) where source_id = $page_id LIMIT $post_limit";
    

    If you want to use string concatenation, you have the terminate the string properly:

    'select (...) where source_id = ' . $page_id . ' LIMIT ' . $post_limit;
    

    Read more about strings.

    0 讨论(0)
  • 2021-01-26 08:08
    $query = "select post_id, source_id, actor_id, target_id, created_time, likes, message, attachment, comments from stream where source_id = '$page_id' LIMIT $post_limit";
    

    or

    $query = "select post_id, source_id, actor_id, target_id, created_time, likes, message, attachment, comments from stream where source_id = '".$page_id."' LIMIT ".$post_limit."";
    

    (if page_id is an integer you can write it without any quotes)

    the problem is in quotes.

    0 讨论(0)
  • 2021-01-26 08:08

    This is a syntax error. It won't work on any host.

    0 讨论(0)
  • 2021-01-26 08:14

    You are basically generating this query (I've reformatted it for better display):

    select post_id, source_id, actor_id, target_id, created_time, likes,
        message, attachment, comments
    from stream
    where source_id = '.1.9248575411383E+14.' LIMIT '.2.'
    

    This is by no means valid SQL. To begin with, you have to get rid of the additional dots.

    Apart from your invalid SQL, 192485754113829 is very large integer. The maximum size of integers depends on the platform but in my computer the limit is 2^32 which is smaller than your number. That means that PHP will handle it as a floating point number and you will lose precision. Given that it's an ID, I suggest you use PHP strings instead.

    0 讨论(0)
  • 2021-01-26 08:20
    $query = "select post_id, source_id, actor_id, target_id,
    created_time, likes, message, attachment, comments
    from stream where source_id = '$page_id' LIMIT $post_limit ";
    

    would work.

    Or if you want to use concatenation :

    $query = "select post_id, source_id, actor_id, target_id,
    created_time, likes, message, attachment, comments
    from stream where source_id =  ".$page_id." LIMIT ".$post_limit;
    
    0 讨论(0)
提交回复
热议问题