Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL WHERE IN returning one row
    primarykey
    data
    text
    <p>So I've got what I think is a pretty simple SELECT query. The query itself looks like this:</p> <pre><code>"SELECT id FROM files WHERE name IN ("me_me-about.jpg", "me_me-blog.jpg", "me_me-.jpg") </code></pre> <p>Where I want the ids of those three files in my database. They're in their table. They have unique IDs. (I'm doing this so I can get the ids to insert into a junction table to associate them with user profiles, in case you're wondering.) I created the statement out of a $filenames array, that's just a simple, one-dimensional array of filenames:</p> <pre><code>$stmt = '('; for ($i = 0; $i &lt; sizeof($filenames); $i++) { $stmt .= '"' . $filenames[$i] . '", '; } $stmt = substr($stmt, 0, -2); $stmt .= ')'; $query2 = $this-&gt;db-&gt;prepare("SELECT id FROM files WHERE name IN " . $stmt); </code></pre> <p>Query returns one row, the last one. From everything I've seen of the WHERE... IN syntax, that shouldn't be? Is something haywire that I just can't see?</p> <p>CSV table dump of "files" from PMA (id, name, year [I'm coming back to that once this is working]):</p> <pre><code>"20";"me_me-about.jpg";;"0" "21";"me_me-blog.jpg";;"0" "22";"me_me-.jpg";;"0" </code></pre> <p>print_r of $filenames:</p> <pre><code>Array ( [0] =&gt; me_me-about.jpg [1] =&gt; me_me-blog.jpg [2] =&gt; me_me-.jpg ) </code></pre> <p>print_r of $stmt in its original form (with my code above):</p> <pre><code>("me_me-about.jpg", "me_me-blog.jpg", "me_me-.jpg") </code></pre> <p>Try-catch to execute. Output the $rows to $_SESSION so I can print_r it on the relevant page:</p> <pre><code>try { $query2-&gt;execute(); $rows = $query2-&gt;fetch(); $_SESSION['stmt'] = $rows; } catch (PDOException $e) { die($e-&gt;getMessage()); } </code></pre> <p>print_r of $_SESSION['stmt']:</p> <pre><code>Array ( [id] =&gt; 22 [0] =&gt; 22 ) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload