Monday, January 21, 2013

How to get row count from MySQL with PHP/PDO

PDOStatement::rowCount() does not work well with MySQL, so here is the workaround code:
$pdo = new PDO('mysql:dbname=database;host=localhost;charset=UTF-8', 'username', 'password',
    array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
...
$total = $pdo->query('SELECT COUNT(*) FROM database')->fetchColumn();

2 comments:

  1. prepare($sql);

    $stmt->execute();

    $stmt->bind_result($P_id, $product_name, $product_price);

    while($stmt->fetch()){

    $temp = [

    'P_id'=>$P_id,

    'product_name'=>$product_name,

    'product_price'=>$product_price

    ];

    array_push($android, $temp);

    }

    echo json_encode($android);

    }



    else{

    die("Connection failed: " . $conn->connect_error);

    }



    ?>


    how to count row from table and print using jeson

    ReplyDelete
  2. prepare($sql);

    $stmt->execute();

    $stmt->bind_result($P_id, $product_name, $product_price);

    while($stmt->fetch()){

    $temp = [

    'P_id'=>$P_id,

    'product_name'=>$product_name,

    'product_price'=>$product_price

    ];

    array_push($android, $temp);

    }

    echo json_encode($android);

    }



    else{

    die("Connection failed: " . $conn->connect_error);

    }



    ?>


    how to count row from and print it

    ReplyDelete