php - Pull data from MySQL into JSONArray (with PDO!) -
i reading on internet , many people it's recommended switch pdo old mysql (and mysqli) extensions.
i'm new pdo, learned it. problem tried search stackoverflow, google, etc, didn't helped me.
original script mysqli :
$data = mysqli_query($con, "select x1, x2, x3, x4 table1 x1 = $variable1"); $row = mysqli_fetch_row($data); $result_data = array( 'data1' => $row[0], 'data2' => $row[1] ); echo json_encode($result_data);
this code outputs :
{"data1", 1, "data2", 2}
i tried change pdo code :
$sth = $dbh->prepare("select x1, x2, x3, x4 table1 x1 = ?"); $sth->bindparam(1, $variable1); $sth->execute(); $row = $sth->fetchall(); $result_data = array( 'data1' => $row[0], 'data2' => $row[1], ); echo json_encode($result_data);
this me strange
{"data1", 1, 2, 1, "data2", 1, "data2"}, data1 null, data2 null
it should original mysqli script...
i tried multiple fetch modes assoc, num, column, found on internet, result similar , error :
notice:undefined offset: 1 in
what can problem, how fix it?
$sth = $dbh->prepare("select x1 data1, x2 data2 table1 x1 = ?"); $sth->execute([$variable1]); echo json_encode($sth->fetchall(pdo::fetch_assoc));
Comments
Post a Comment