How get all values in a column using PHP?

前端 未结 6 1296
悲&欢浪女
悲&欢浪女 2020-11-28 09:10

I\'ve been searching for this everywhere, but still can\'t find a solution: How do I get all the values from a mySQL column and store them in an array?

For eg: Tab

6条回答
  •  一个人的身影
    2020-11-28 10:05

    Here is a simple way to do this using either PDO or mysqli

    $stmt = $pdo->prepare("SELECT Column FROM foo");
    // careful, without a LIMIT this can take long if your table is huge
    $stmt->execute();
    $array = $stmt->fetchAll(PDO::FETCH_COLUMN);
    print_r($array);
    

    or, using mysqli

    $stmt = $mysqli->prepare("SELECT Column FROM foo");
    $stmt->execute();
    $array = [];
    foreach ($stmt->get_result() as $row)
    {
        $array[] = $row['column'];
    }
    print_r($array);
    

    Array
    (
        [0] => 7960
        [1] => 7972
        [2] => 8028
        [3] => 8082
        [4] => 8233
    )
    

提交回复
热议问题