escapovani dotazu Database?
- vlna
- Člen | 34
Ahoj
pro nasledujici priklad kodu:
$source = $this->database->table($w)->select("
if(
isnull({$w}.image_path) or ({$w}.image_path = '/media/pict/foo_0.png'),
0,
1
) AS has_image
");
vede k nekolika chybam:
- i kdyz je v retezci – ‚/media/pict/foo0.png‘ – kuli tecce se snazi Database vyhodnotit relace (reseni jsem nasel pres escape \.)
- po prechroupani Databasi, pred odeslanim do db vznikne:
...
isnull(`xxx`.`image_path`) `or` (`xxx`.`image_path` = '/`media`/`pict`/`foo_0`\.`png`'),
...
u cehoz vadi a) escapovani or operatoru, b) rozsekani filesystemceesty
Jak tohle resit ?
Diky.
Nette Framework (version 2.0.2 released on 2012–03–31, https://nette.org)
PHP 5.3.5
Edit: oprava preklepu
Editoval vlna (16. 8. 2012 17:37)
- David Matějka
- Moderator | 6445
zkus napsat klicovy slova velkyma pismenama
a k te ceste.. musi se to tam dostat nejak pres parametr.. takze zkus
image_path = ?
a ted je problem, ze parametry se do nette database nechaji narvat jen pres
where, melo by se to nechat obejit pomoci hacku ->where(‚1‘,
‚/media/pict/foo0.png‘)
cely kod tedy asi:
<?php
$source = $this->database->table($w)->select("
IF(
ISNULL({$w}.image_path) OR ({$w}.image_path = ?),
0,
1
) AS has_image
")->where('1', '/media/pict/foo_0.png');
?>
Editoval matej21 (16. 8. 2012 18:15)
- vlna
- Člen | 34
matej21 napsal(a):
zkus napsat klicovy slova velkyma pismenama
a k te ceste.. musi se to tam dostat nejak pres parametr.. takze zkus image_path = ?
a ted je problem, ze parametry se do nette database nechaji narvat jen pres where, melo by se to nechat obejit pomoci hacku ->where(‚1‘, ‚/media/pict/foo0.png‘)
Dobryyy :-). Nenapadlo me, ze by velikost klicovych slov mohla mit vliv → pomohlo to k neuvozovkovani OR. Hack s pridanim parametru pres podminku mi nefunguje, ale je to aspon o krok dal.
- David Matějka
- Moderator | 6445
tak jeste uprava where, tohle uz by melo fungovat :)
<?php
$source = $this->context->database->table($w)->select("
IF(
ISNULL({$w}.image_path) OR ({$w}.image_path = ?),
0,
1
) AS has_image
")->where('?', 1, '/media/pict/foo_0.png');
?>
- vlna
- Člen | 34
matej21 napsal(a):
tak jeste uprava where, tohle uz by melo fungovat :)
<?php $source = $this->context->database->table($w)->select(" IF( ISNULL({$w}.image_path) OR ({$w}.image_path = ?), 0, 1 ) AS has_image ")->where('?', 1, '/media/pict/foo_0.png'); ?>
Perfektni ! Je potreba jeste prehodit poradi tech dvou parametru, ale
kazdopadne funguje skvele.
Diky za pomoc pane ;-).