0

I am having an issue with my prepared SELECT result array not holding the actual column values. I know how to get data from an sql database using Java, however I am having issues with PHP PDO. I am connected to a remote AWS database that I know works. Here is the code:

        $conn = new PDO($dsn, $username, $password);
        //echo "Test";

Here I want to get all the column's data

        $sth = $conn->prepare("SELECT 'id', 'short-des', 'full-des', 'image-urls', 'date-created' FROM projects");
        $sth->execute();

I have placed PDO::FETCH_BOTH, PDO::FETCH_ASSOC, PDO::FETCH_COLUMN. The result at the end of this post is a result of me leaving the fetchall() blank. When I insert any of the PDO::styles no values are returned. I have done other scenarios such as using var_dump along with the styles in the fetch statement which provide actual results, BUT no actual values that the columns contain.

        $row = $sth->fetchAll();

        echo "<br>" . $row[0] . "<br>";
        echo "<br>" . $row[1] . "<br>";

        $column1 = $row[0];
        $column2 = $row[1];

        for($i = 0; $i < 5; $i++){
            echo " " . $column1[$i];
            $var = $column1[$i];
            echo " = " . $var . " , ";

        }
        for($i = 0; $i < 5; $i++){
            echo " " . $column2[$i];
            $var = $column2[$i];
            echo " = " . $var . " , ";
        }

This is the resulting text:

64-bitPDO is available

Array

Array id = id , short-des = short-des , full-des = full-des , image-urls = image-urls , date-created = date-created , id = id , short-des = short-des , full-des = full-des , image-urls = image-urls , date-created = date-created , Connected successfully

I'm guessing its some stupidly simple reason that it is not showing actual values. I am not familiar with the "fetch" concept since I'm coming from JDBC, however I have been spending the past few days trying to get it to work to no avail. I have looked at similar posts such as this post, and this post, and this post. These seem to handle slightly different issues. Unfortunately I want to use a prepared statement instead of an actual query.

Community
  • 1
  • 1
Josiah L.
  • 302
  • 1
  • 4
  • 21

1 Answers1

1

I believe the problem is your single-quotes. Try this:

$sth = $conn->prepare("SELECT `id`, `short-des`, `full-des`, `image-urls`, `date-created` FROM projects");

Note: backticks ` are not the same as single-quotes '.

MSS
  • 3,520
  • 24
  • 29
imtheman
  • 4,713
  • 1
  • 30
  • 30
  • Unfortunately that does not resolve the issue. I used single quotes because I saw somewhere that you can use them if your column names have key phrases in them. The word 'fill' , and 'date' gets highlighted. $sth = $conn->prepare("SELECT id, short-des, full-des, image-urls, date-created FROM projects"); – Josiah L. Jul 07 '16 at 23:33
  • @JosiahL. What does `var_dump($row);` output with this change? Actually, you use the backtick ` not '. Try it with the backtick now. – imtheman Jul 07 '16 at 23:36
  • It fails to output anything. However this is what it outputs with the single quotes: array(2) { [0]=> array(10) { ["id"]=> string(2) "id" [0]=> string(2) "id" ["short-des"]=> string(9) "short-des" [1]=> string(9) "short-des" ["full-des"]=> string(8) "full-des" [2]=> string(8) "full-des" ["image-urls"]=> string(10) "image-urls" [3]=> string(10) "image-urls" ["date-created"]=> string(12) "date-created" [4]=> string(12) "date-created" } [1]=> array(10) { ["id"]=> string(2) "id" [0]=> string(2) "id" ["short-des"]=> string(9) "short-des" [1]=> string(9) "short-des" ["full-des"]=> string(8) – Josiah L. Jul 07 '16 at 23:43
  • "full-des" [2]=> string(8) "full-des" ["image-urls"]=> string(10) "image-urls" [3]=> string(10) "image-urls" ["date-created"]=> string(12) "date-created" [4]=> string(12) "date-created" } } – Josiah L. Jul 07 '16 at 23:43
  • 1
    Correction I just noticed that you said backticks. It displays data through the var_dump, and the route I have in the post. – Josiah L. Jul 07 '16 at 23:48