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,
));
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
U. MJG
  • 47
  • 1
  • 9
  • Are you sure you have "tried everything"? Seems unlikely when the code you supplied is probably actually working as expected – GrumpyCrouton Sep 07 '17 at 18:01
  • What are you doing with `$Data` later? – chris85 Sep 07 '17 at 18:02
  • @GrumpyCrouton I am trying to fix this problem since 5 hours ago. – U. MJG Sep 07 '17 at 18:02
  • Check out this documentation https://phpdelusions.net/pdo – GrumpyCrouton Sep 07 '17 at 18:02
  • Are you getting any errors? If not, you can run an `if` check: `if ($Data=$connection->prepare("SELECT * FROM ADS")) { ... }` – Sam Sep 07 '17 at 18:02
  • @chris85 I have this if ($Data->rowCount()>0){ ...... } – U. MJG Sep 07 '17 at 18:02
  • 1
    Don't use `rowcount` for selects. http://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.` – chris85 Sep 07 '17 at 18:03
  • @Samuel It give no errors, also with the error cathchers this is the strange thing. – U. MJG Sep 07 '17 at 18:04

1 Answers1

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;
}
Maxi Schvindt
  • 1,432
  • 1
  • 11
  • 20
  • mm check your connection http://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. – Maxi Schvindt Sep 07 '17 at 18:13
  • User privileges? – Maxi Schvindt Sep 07 '17 at 18:16
  • 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() - http://php.net/manual/es/pdo.errorinfo.php – Maxi Schvindt Sep 07 '17 at 18:25
  • 2
    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? – Qirel Sep 07 '17 at 18:31
  • @Cuchu Never surrender! – U. MJG Sep 07 '17 at 18:32
  • @Cuchu After about 8 hours of search i discovered that cloudflare was the problem... i refreshed the cache and it worked!! – U. MJG Sep 07 '17 at 19:20
  • @U.MJG You really need to apply proper validations and exception handlings. See [my answer](https://stackoverflow.com/questions/46012971/pdo-fetching-content-from-two-tables-foreach-statement/46019824#46019824) and you'll be surprised to find out that you would have saved about 7 hours of 8 :-) Hopefully. Good luck! –  Sep 07 '17 at 22:25
  • @aendeerei nice answer!! – U. MJG Sep 08 '17 at 13:28
  • @U.MJG Thanks. If you're interested in a complete solution, here's my [db adaper](https://stackoverflow.com/questions/46014772/return-multiple-response-data-in-one-response/46018999#46018999) (the "EDIT" part), so that you can make use of the whole data access operations in a much easier way. Bye. P.S: the PDO instance, e.g the connection, should be created outside and passed as constructor argument into db adapter - that's the perfect OOP implementation. –  Sep 08 '17 at 15:58