Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As others have mentioned:</p> <pre><code>use strict; use warnings; </code></pre> <p>Also, every time you do a DBI call, you should be doing this:</p> <pre><code>if ($sth-&gt;err()) { die "ERROR: " . $sth-&gt;errstr() . "\n"; } </code></pre> <p>Even after a fetch. This will catch lots of the issues you might have.</p> <p>I simplified your program a wee bit:</p> <pre><code>use strict; use warnings; use DBI; my $connect = DBI-&gt;connect("$connectString", "$user", "$id"); if (not $connect) { die qq(connection error\n); } my $table = "session"; my $uniqueSessions = "SELECT DISTINCT SESSION FROM $table"; print qq(\$uniqueSessions = "$uniqueSessions"\n); my $queryUniques = $connect-&gt;prepare($uniqueSessions); if ($queryUniques-&gt;err()) { die "ERROR: " . $queryUniques-&gt;errstr() . "\n"; } $queryUniques-&gt;execute(); if ($queryUniques-&gt;err()) { die "ERROR: " . $queryUniques-&gt;errstr() . "\n"; } my $session; $queryUniques-&gt;bind_columns(\$session); my $counter = 1; my $sessionString; while(my $hashref = $queryUniques-&gt;fetch()) { print "Fetching Row\n"; if($counter == 1) { $sessionString = "'" . $session . "'"; } else { $sessionString = $sessionString . ", '" . $session . "'"; } $counter++; } if ($queryUniques-&gt;err()) { print "ERROR = " . $queryUniques-&gt;errstr . "\n"; } print "$sessionString\n"; </code></pre> <p>I basically took out the second query and fixed a few things here and there. The last line prints out the <code>$sessionString</code> which is a quote separated list of all of your sessions. This part worked.</p> <p>The second part is where things get weird. You are taking <code>$sessionString</code> and passing it as a SQL statement. Unless there's something I am not seeing, <code>$sessionString</code> is simply a list of sessions and not a SQL statement in itself.</p> <p>As I said, check for errors with each DBI call, and see if you're erroring out somewhere.</p> <p>You can also add a line like this:</p> <pre><code>print qq(DEBUG: SQL Query = '$sqlStatement'\n); </code></pre> <p>before you run a <code>$sth-&gt;prepare($sqlStatement)</code> and that way, you can see what your SQL statement is.</p> <hr> <h1>The Problem</h1> <p>It looks like you're preparing the second query before you figure out what <code>(?)</code> should be (which I assume is your group of $sessionString).</p> <p>You need to substitute the sessions with the <code>(?)</code> <strong>BEFORE</strong> you do your DBI->prepare().</p> <p>Something like this (<em>not tested</em>):</p> <pre><code>(my $sessionQuery = $query) =~ s/\(\?\)/$sessionString/; my $querySth = $connect-&gt;prepare($sessionQuery); $querySth-&gt;execute(); my @test = $queryPrep-&gt;fetchall_arrayref(); </code></pre> <p>Remember that <code>qq()</code> quoting syntax is your friend. I use this a lot:</p> <pre><code>print qq(DEBUG: \$foo = "$foo"\n); </code></pre> <p>And sometimes, I simply copy and paste a statement and then quote it:</p> <pre><code>print qq(DEBUG: while (my $foo = foobar($bar)) };\n); </code></pre> <p>It's amazing how many errors I can catch that way. I can then search for <code>DEBUG:</code> in my program and delete those lines.</p> <p>Remember write your programs a bit at a time, see if you can get <code>$sessionString</code> working. Then try to see if you can substitute that into your <code>$query</code>, then try executing the calculated query.</p> <p>Perl is a nice and powerful language, but the syntax can be a bit crufty at times -- especially if you're not use to object oriented Perl.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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