0

I have a simple SQL query that is not working. I have tried everything I know to fix this query, but to no avail.

$Data = $connection->prepare("SELECT * FROM EXAMPLE"); 

The table on the database exists, and the connection to the database is set properly.

I have also tried this:

$Data = $connection->prepare("SELECT ID FROM EXAMPLE WHERE EXAMPLE1=:EXAMPLE1 "); $Data->execute(array( ':EXAMPLE1' => $EXAMPLE1, )); 
8
  • Are you sure you have "tried everything"? Seems unlikely when the code you supplied is probably actually working as expected Commented Sep 7, 2017 at 18:01
  • What are you doing with $Data later? Commented Sep 7, 2017 at 18:02
  • @GrumpyCrouton I am trying to fix this problem since 5 hours ago. Commented Sep 7, 2017 at 18:02
  • Check out this documentation phpdelusions.net/pdo Commented Sep 7, 2017 at 18:02
  • 1
    Don't use rowcount for selects. php.net/manual/en/pdostatement.rowcount.php If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications. Commented Sep 7, 2017 at 18:03

1 Answer 1

0

If your connection is fine..

$Data=$connection->prepare("SELECT ID FROM EXAMPLE WHERE EXAMPLE1 = :EXAMPLE1"); $Data->execute(array( ':EXAMPLE1' => $EXAMPLE1, )); $result = $Data->fetchAll(); 

Or

$result = array(); $sql = "SELECT ID FROM EXAMPLE WHERE EXAMPLE1 = {$EXAMPLE1};"; foreach ($connection->query($sql) as $row) { $result[] = $row; } 
Sign up to request clarification or add additional context in comments.

9 Comments

mm check your connection php.net/manual/es/pdo.construct.php And then execute your query in phpmyadmin or administration interface or use console sql and execute your query.
User privileges?
So, if not exist error in the logs, the connection it's fine and the permissions of user are correct, database, table and registers are exists.. i don't know bro that can be the error :( but try to use errorInfo after prepare() - php.net/manual/es/pdo.errorinfo.php
I would remove the second snippet. Using a prepared statement with bound variables is always better. What if it's a string, then you need to quote it. What if the string has a quote in it?
@Cuchu Never surrender!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.