Reputation: 49
I have tryed many things, but I just cant workout why my left dosent work.
this dos not work, only give me 5 (5 post) blank lines
$sql = 'SELECT work_id, LEFT(job_art, 15) FROM rum where work_id = :work_id';
this works
$sql = 'SELECT work_id, job_art FROM rum where work_id = :work_id';
this dos not work (
$sql = 'SELECT LEFT(job_art, 15), work_id FROM rum where work_id = :work_id';
and my sql looks like this
$work_id = $_GET['work_id'];
$job_art = $_GET['job_art'];
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = 'SELECT work_id, LEFT(job_art, 15) FROM rum where work_id = :work_id';
$q = $pdo->prepare($sql);
$q->bindValue(':work_id', $work_id);
$q->execute();
$q->setFetchMode(PDO::FETCH_ASSOC);
?>
<?php while ($r = $q->fetch()): ?>
<div class="liste"><p class="padding"><?php echo htmlspecialchars($r['job_art'])?></p></div>
Upvotes: 2
Views: 1560
Reputation: 20899
You're attempting to read the columns by name (PDO::FETCH_ASSOC
), but you haven't given a name to the result of LEFT(job_art, 15)
, so attempting to access $r['job_art']
won't contain anything.
To fix this, you'll need to alias the value of your expression. Your new query should look like:
SELECT work_id, LEFT(job_art, 15) AS job_art FROM rum WHERE work_id = :work_id
Upvotes: 4