Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql PDO doesnt work

Tags:

php

mysql

pdo

I've a little problem here..

I am using jquery easy UI to get data from mysql and then write a table in my php application..

nah.. the problem is.. my code or my query doesn't work..

When I am using my old mysql_connect, my code works well... but when I try to write it to mysql, I get nothing...

Here is my old code:

$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
    $rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
    $offset = ($page-1)*$rows;

    $result = array();

    $conn = mysql_connect('127.0.0.1','root','');
    mysql_select_db('colosus',$conn);

    $rs = mysql_query("select count(*) from user");
    $row = mysql_fetch_row($rs);
    $result["total"] = $row[0];

    $rs = mysql_query("select * from user limit $offset,$rows");

    $rows = array();
    while($row = mysql_fetch_object($rs)){
        array_push($rows, $row);
    }
    $result["rows"] = $rows;

    echo json_encode($result);

and its the PDO query code...

$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
    $rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
    $offset = ($page-1)*$rows;

    $result = array();



    $rs = $db->query("select count(*) from user");
    $row = $rs->fetch(PDO::FETCH_NUM);
    $result["total"] = $row[0];

    $rs = $db->query("select * from user as u, leveluser as l where u.idLevelUser = l.idLevelUser limit $offset,$rows");

    $rows = array();
    while($row = $rs->fetch(PDO::FETCH_OBJ)){
        array_push($rows, $row);
    }
    $result["rows"] = $rows;

    echo json_encode($result);

The last query (PDO) doesn't work.. I dont know why.. anybody can help me???

like image 484
Wawan Brutalx Avatar asked Sep 23 '26 23:09

Wawan Brutalx


1 Answers

Looks like you forget to instanciate your $db object.

Try to put this before $result = array();

$db = new PDO("mysql:host=127.0.0.1;dbname=colosus", 'root', '');
like image 147
Alain Tiemblo Avatar answered Sep 26 '26 11:09

Alain Tiemblo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!