0

My code is very simple:

 $con1 = new PDO("mysql:host=localhost;port=3306;dbname=users_db", "root", "");
 $resp = $con1 -> query('SELECT * FROM records');

The response I get inside $resp is the query string 'SELECT * FROM records' no clue why. The same query is working using mysqli.

I tried debugging and I see a strange value {POD}[0] in $con1 after creating the PDO instance. What am I doing wrong here I followed this phpro.org.

0

1 Answer 1

2

Try this code:

<?php

// Connect to MySQL via PDO
try {
$con1 = new PDO("mysql:host=localhost;port=3306;dbname=users_db", "root", "");
$con1->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}

try {
$resp = $con1 -> query('SELECT * FROM records');
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}

You will get a Exception when the connection fails or when the query fails.

Sign up to request clarification or add additional context in comments.

6 Comments

I did that still no exception and no result I get inside $resp the value 'SELECT * FROM records' after execution of line $resp = $con1 -> query('SELECT * FROM records'); and no exceptions at all
Add also this line: ini_set('display_errors', true); now there should be a error.
Still noting after I add this line, I don't think there is an error
Didn't help I think the problem is starting when I start the connection in the debug I see {POD}[0] inside $con1
+1 For the record this works correctly, you may want to add in the loop example such as foreach($resp as $row) { var_dump($row); }
|

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.