mysql PHP PDO executing 2 queries on a single POST request -
i'm using standard apache2 lamp
configuration mysql
, php5
, thing changed character encoding onto utf8
.
i'm making ajax
call upon clicking anchor (the article title), , want run 2 queries. 1 gets article, , other comments related article.
i found this post while browsing around, far haven't been able implement he's talking about. message: fatal error: call member function nextrowset() on non-object in /home/shooshte/dropbox/ptc_php/db_queries/articles.php on line 45
. have no idea if right way it.
the problem first select statement returns single row (one article), while second select statement returns multiple rows (all comments on article), can't join them 1 statement.
anyway here's code (i commented out irrelevant parts):
<?php error_reporting(e_all); ini_set('display_errors', 1); try { $hostname = "localhost"; $username = "topdecka_admin"; $password = ""; $db = new pdo("mysql:host=$hostname;dbname=topdecka_ptc;charset=utf8",$username, $password); $db->setattribute(pdo::attr_errmode, pdo::errmode_exception); /*if (!empty($_post["searchword"])) { $searchword = $_post["searchword"]; $query = $db->prepare( 'select articles.title, articles.posted, articles.extract, authors.name, group_concat(categories.cat_name) cat_name articles, authors, categories, article_categories articles.author_id = authors.id , articles.id = article_categories.article_id , article_categories.category_id = categories.id , ((title :searchword) or (extract :searchword) or (body :searchword) or (name :searchword) or (cat_name :searchword))' ); //end db query $query->execute(array(":searchword" => "%" . $searchword . "%")); $result = $query->fetchall(); //turns timestamp integer for($i = 0; $i < count($result); ++$i) { $result[$i]['posted'] = strtotime($result[$i]['posted']); } echo json_encode($result); die(); */ } else if (!empty($_post["title"])) { $title = $_post["title"]; $query = $db->prepare( "select articles.title, articles.posted, articles.body, authors.name, authors.img, authors.bio, group_concat(categories.cat_name) cat_name articles inner join authors on articles.author_id = authors.id inner join article_categories on articles.id = article_categories.article_id inner join categories on article_categories.category_id = categories.id title :title; select comment.user_id, comment.text, comment.article_id articles right join comment on articles.id = comment.article_id title :title; select comment.user_id, comment.text, comment.article_id articles right join comment on articles.id = comment.article_id;" ); //end db query $query->execute(array(":title" => $title)); $result = $query->fetchall(); $result->nextrowset(); $result[0]['posted'] = strtotime($result[0]['posted']); echo json_encode($result); die(); } /*else { $query = $db->prepare( 'select articles.title, articles.posted, articles.extract, authors.name, group_concat(categories.cat_name) cat_name articles, authors, categories, article_categories articles.author_id = authors.id , articles.id = article_categories.article_id , article_categories.category_id = categories.id' ); //end db query $query->execute(); $result = $query->fetchall(); //turns timestamp integer for($i = 0; $i < count($result); ++$i) { $result[$i]['posted'] = strtotime($result[$i]['posted']); } echo json_encode($result); die(); } */ } catch (pdoexception $e) { echo "error!: " . $e->getmessage() . "<br/>"; die(); } ?>
and here's jquery makes post
request:
$(".article-box h1 a").click(function(e) { e.preventdefault(); var title = "title="+ $(this).text(); $.post( "db_queries/articles.php", title, function(data) { console.log(data); var response = json.parse(data); // formats article body var articlebody = response[0].body.trim(); articlebody = articlebody.replace(/(\r\n|\n|\r)/gm,"<br>"); // formats date var posted = new date((response[0].posted * 1000)); $("#articles-feed").empty(); $("#articles-feed").append( '<div class="article-box"><h1>' +response[0].title+ '</h1><h3>' + posted.tolocaledatestring()+ '</h3><p>' + articlebody + '</p><button>back articles</button></div>' ); //end article append var authorhtml = ( '<img id="author-img" src="' + response[0].img + '"><h2 id="author-name">' + response[0].name + '</h2><p>' + response[0].bio + '</p>'); $("#author").append(authorhtml); //back articles button $("#articles-feed button").click(function(e) { e.preventdefault(); window.location.assign('articles'); }) } //end response function ); //end post request }) //end article title click function
there 2 problems have now: calling nextrowset()
on array contains data , not getting data second row set.
you need like:
$result = $query->fetchall(); // $result contains row data // next row set query $query->nextrowset(); ^^^^^^ // results of second row set $result2 = $query->fetchall(); // have see how want send both result sets... echo json_encode( array('article' => $result, 'comments' => $result2) ); die();
note have adapt javascript accommodate data structure.
Comments
Post a Comment