Thread [DBI] Syntaxfehler DBD::mysql::st execute failed und DBD::mysql::db selectrow_array failed (5 answers)
Opened by saschaw at 2017-01-24 15:41

GwenDragon
 2017-01-24 16:15
#185922 #185922
User since
2005-01-17
14533 Artikel
Admin1
[Homepage]
user image
Du hast kein Problem mit Perl/CGI sondern deiner Datenbankprogrammierung. Ich verschiebe mal.

Kannst du bitte mal dein generiertes SQL in $sql in eine Logdatei oder die Shell ausgeben lassen?


Quoting im SQL-Code??!? Uhhh, da macht man nette fehler!
Und Feldbezeichner können sich mit internen Befehlen beißen, deswegen gibt es das bei MySQL in `....` zu setzen.
Und wozu gibt es prepared Statements in DBI?

Statt:
Code: (dl )
1
2
3
4
5
6
$sql="SELECT $qdbcols FROM $table WHERE stationid = $stationId AND sensid=$sensorId";
#$sql.=" ORDER by datetime DESC,id DESC LIMIT 2";
$sql.=" ORDER by datetime DESC LIMIT 2";
# Query database for min, max and average of sensor
$sth = $dbh->prepare($sql);
$sth->execute;

eher so:
Code: (dl )
1
2
3
4
5
6
$sql="SELECT ? FROM ? WHERE `stationid` = ? AND `sensid`=?";
#$sql.=" ORDER by `datetime` DESC,id DESC LIMIT 2";
$sql.=" ORDER by `datetime` DESC LIMIT 2";
# Query database for min, max and average of sensor
$sth = $dbh->prepare($sql);
$sth->execute($qdbcols,$table,$stationId,$sensorId );



Und bei
Code: (dl )
1
2
3
 $sql="SELECT SUM($dbColName) AS \"SUM\" FROM $table WHERE stationid = $stationId AND".
" datetime>=\"$midnightDateGMT $midnightTimeGMT\" AND".
" LEFT(datetime,10) <=\"$midnightDate $dayEndTime\" AND sensid=$sensorId";
Finde ich gruselig!

Code: (dl )
1
2
3
$sql=qq{SELECT SUM(?) AS `SUM` FROM ? WHERE `stationid`=? AND `datetime` >= "? ?" AND LEFT(`datetime`,10) <= "? ?" AND `sensid`=?};
$sth = $dbh->prepare( $sql );
$sth->execute($dbColName, $table, $stationId, $midnightDateGMT, $midnightTimeGMT, $midnightDate, $dayEndTime, $sensorId);

Schau mal rein: http://www.perl.com/pub/1999/10/DBI.html

PS: CamelCase bei Variablennamen ist auch eher unlesbar, das ist aber meine Geschmacksasche.
Last edited: 2017-01-25 12:56:07 +0100 (CET)
die Drachin, Gwendolyn


Unterschiedliche Perl-Versionen auf Windows (fast wie perlbrew) • Meine Perl-Artikel

View full thread [DBI] Syntaxfehler DBD::mysql::st execute failed und DBD::mysql::db selectrow_array failed