0

I'm new to OOP and just trying to migrate from mysql_, but I'm running into some issues while migrating.

$konek = new PDO('mysql:host=localhost;dbname=mydatabase', root, root);$hasil = $konek->query("SELECT * FROM `tabel`");
while ($data = $hasil->fetch_object()) {
echo '<td>'.$data->something.'</td>';}

It doesn't work or show an error, but when I replace the first line with mysqli it works:

konek = new mysqli('localhost','root','root','mydatabase');

Any help would be really appreciated.

1
  • I suspect you aren't getting errors because: 1) You haven't configured PHP in general to do so. 2) You don't have any PDO specific error checking code. Commented Jun 28, 2014 at 15:25

3 Answers 3

2

I assume in your code, it is coded as string: (or is root defined?)

$konek = new PDO('mysql:host=localhost;dbname=mydatabase', root, root);

Clearly, in your example, if it's a type change it to:

$konek = new PDO('mysql:host=localhost;dbname=mydatabase', 'root', 'root', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));

Refactor your code into this:

error_reporting(E_ALL);
ini_set("display_errors", 1);

try {
    $konek = new PDO('mysql:host=localhost;dbname=mydatabase', 'root', 'root', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

$hasil = $konek->query("SELECT * FROM `tabel`");
echo '<table>';
while ($row = $hasil->fetch(PDO::FETCH_OBJ)) {
    echo '<tr>';
        echo '<td>' . $row->column_name . '</td>';
    echo '</tr>';
}
echo '</table>';
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the help , now every error is displayed , i use MAMP and i think error is hidden by default
@user3181160 yes even in other environment, its usually off as default you really have to set it first, glad it helped
0

1)User name and password should be quoted string

2) use try catch

3) use prepare and execute the query

4) then fetch

try like this:

try {
    $konek = new PDO('mysql:host=localhost;dbname=mydatabase', 'root', 'root');
    $konek->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    die( 'ERROR: ' . $e->getMessage());
}

try {
    $hasil = $konek->prepare("SELECT * FROM `tabel`");
    $hasil->execute();
    while ($data = $hasil->fetch(PDO::FETCH_OBJ)) {
    echo '<td>'.$data->something.'</td>';
    }

}
catch(PDOException $e) {
        die( 'ERROR: ' . $e->getMessage());
    }

2 Comments

try { $konek = new PDO('mysql:host=localhost;dbname=jwcrm2', 'root', 'root'); $konek->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $e) { die( 'ERROR: ' . $e->getMessage()); } try{} $hasil = $konek->query("SELECT * FROM kontak"); while ($data = $hasil->fetch_object()) { echo '<td>'.$data->nomer.'</td>'; } catch(PDOException $e) { die( 'ERROR: ' . $e->getMessage()); }
Ok thanks this is works :) , i just try to edit the tabel that doesnt exist but it doesnt show error , is it supposed to be that way ?
-1

Please provide an error message it echoes.

MY best guess:

http://www.php.net/manual/en/pdostatement.fetchall.php

Try replacing

while ($data = $hasil->fetch_object()) {
echo '<td>'.$data->something.'</td>';}

with

while ($data = $hasil->fetchAll()) {
echo '<td>'.$data->something.'</td>';}

2 Comments

Please try putting error_reporting into E_ALL mode php.net/manual/en/function.error-reporting.php
Also please do var_dump on the $data variable and put the error here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.