PDO mysql and PHP: Undefined index and Trying to get property of non-object -
anyone please me fix php code here. im working php , pdo , have been switching codes around nothing works. appreciated.
here code index page:
<?php require_once 'init.php'; $supportquery = $conn -> query(" select id, title, content table"); while ($row=$supportquery ->fetchobjecy()) { $support[]=$row; } ?> <?php if(!empty($support)): ?> <?php foreach($support $supp): ?> <a href="page.php?id=<?php echo $supp->id; ?>&title=<?php echo $supp->title; ?>"> <?php echo $supp->title; ?></a> <?php endforeach; ?> <?php else: ?> no items found. <?php endif; ?> this code works way work when run code , click link, getting errors.
here php code page.php:
<?php require_once 'init.php'; $articlequery = $conn -> prepare(" select id, title, content table id='$id' , title='$title'"); while ($row=$articlequery ->fetchobject()) { $support[]=$row; } $articlequery->execute(); ?> <?php if(!$_session($support)): ?> <?php echo $support->id; ?> <?php echo $support->title; ?><br/> <?php echo $support->content; ?> <?php else: ?> <p>are inventing titles? sorry page doesnt exist nowhere.</p> <?php endif; ?> i need please. getting following error messages:
notice: undefined index: title , id in /page.php notice: undefined variable: support in /page.php notice: trying property of non-object in /page.php ♡nbgmalimit
ok, start...
1) in 2nd code block, need execute statement before trying fetch results.
2) should binding values $id , $title prior executing avoid sql injection.
look @ examples in manual see how both of these... http://php.net/manual/en/pdostatement.execute.php
3) you're fetching row object , adding resulting object value in $support array.
4) assuming id , title unique, not need loop , add array, should 1 row returned. use fetch. using pdo::fetch_assoc fetch style return array. using pdo::fetch_obj fetch style return object. http://php.net/manual/en/pdostatement.fetch.php
try this...
$row = null; $stmt = $conn->prepare("select id, title, content table id = :id , title = :title"); $stmt->bindparam( ":id", $id, pdo::param_int ); $stmt->bindparam( ":title", $title, pdo::param_str ); $result = $stmt->execute(); if ($result) { $row = $stmt->fetch(pdo::fetch_obj); } else { // handle error } ?> <?php if(!empty($row)): ?> <?php echo $row->id; ?> <?php echo $row->title; ?><br/> <?php echo $row->content; ?> <?php else: ?> <p>are inventing titles? sorry page doesnt exist nowhere.</p> <?php endif; ?>
Comments
Post a Comment