How to run multiple queries in one php page? - php

I have a select query and then another select query but for another table. I need to run the first query, then if it finds something, show a table with a foreach loop. And then, a second query run and select another table with a foreach loop too.
This is my DB class to run my DB :
class DB {
private static $_instance = null;
private $_pdo,
$_query,
$_error = false,
$_count = 0;
public $_results;
private function __construct() {
try {
$this->_pdo = new PDO(...);
} catch(PDOException $e) {
die($e->getMessage());
}
}
public static function getInstance() {
if(!isset(self::$_instance)) {
self::$_instance = new DB();
}
return self::$_instance;
}
public function query($sql, $params = array()) {
$this->_error = false;
if($this->_query = $this->_pdo->prepare($sql)) {
$x = 1;
if(count($params)) {
foreach($params as $param) {
$this->_query->bindValue($x, $param);
$x++;
}
}
if($this->_query->execute()) {
$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
$this->_count = $this->_query->rowCount();
} else {
$this->_error = true;
}
}
return $this;
}
Then, on a page I have this following :
<?php if ($st = DB::getInstance()->query("SELECT * FROM resum WHERE form_id = ? ORDER by date DESC", array(Input::get('formID')))) {
if ($st->count()) { ?>
<div id="topSeparateurClient">
<div>Date</div>
<div>Concessionnaire</div>
<div>Client</div>
<div>Voiture</div>
<div>Prix</div>
<div>Statut</div>
</div>
<div style="clear:both;"></div>
<?php
foreach($st->_results as $result) {
echo "<div class=\"clientGenInfos\">
<div><b>".substr($result->date, 0, 10)."</b> / ".substr($result->date, 10)."</div>
<div>".$result->concessionnaire."</div>
<div>".$result->client."</div>
<div>".$result->voiture."</div>
<div>".$result->prix."</div>";
if ($result->statut == 'En Attente') {
echo '<div class="enAttente"><span>En Attente</span></div>';
} else if ($result->statut == 'Accepter') {
echo '<div class="accepter"><span>Accepter</span></div>';
} else if ($result->statut == 'Refuser') {
echo '<div class="refuser"><span>Refuser</span></div>';
}
echo " </div>
";
}
} else {
echo '<div class="aucuneDemande">Nothing from now.</div>';
}
}
?>
Then a second block, almost identical but with another table name in his query. The problem now is that my second table have the same values as the first one..I am stuck here, reading stuff on the net and nothing about this situation. I am still new to PDO object! please help!
EDIT ---
This is my second block..
<?php
if ($st = DB::getInstance()->query("SELECT * FROM users WHERE users.group = 3 ORDER by date DESC")) {
if ($st->count()) { ?>
<div id="topSeparateurClientConc">
<div>Prénom et Nom</div>
<div>Adresse</div>
<div>Nom d'utilisateur</div>
<div>Date d'inscription</div>
<div>Demandes reçues</div>
</div>
<div style="clear:both;"></div>
<?php
foreach($st->_results as $result2) {
echo "<div class=\"clientGenInfosConc\">
<div>".$result2->name."</div>
<div>".$result2->adresse."</div>
<div>".$result2->username."</div>
<div><b>".substr($result2->joined, 0, 10)."</b> / ".substr($result2->joined, 10)."</div>
<div>".$result2->concessionnaire."</div>
</div>";
}
} else {
echo '<div class="aucuneDemande">Aucune demande transférable en ce moment</div>';
}
}
?>

Do not write your own PDO wrapper. Period.
PDO is already a wrapper. Written by the professionals. It has some drawbacks, but at least it has no harmful features which newbie programmers introduce in hundreds.
If you want static method to get instance - all right, have it. But leave the rest for raw PDO. Saving yourself one function call doesn't worth struggling against fallacies of your own wrapper.
Do not save on calls at all! Look what are you doing:
<?php if ($st = DB::getInstance()->query("SELECT * FROM resum WHERE form_id = ? ORDER by date DESC", array(Input::get('formID'))))
It cannot be even read without scrolling. Are you fined for every extra line or what?
This one single line contains almost dozen operators!
This is called "write-only" style.
You are saving yourself a linefeed to write faster, but when it come to reading, you'll run here, crying "read my code for me!".
Always code as if the person who ends up maintaining your code is a violent psychopath who knows where you live.
Especially if it's you who have to maintain. Look:
<?php
$sql = "SELECT * FROM resum WHERE form_id = ? ORDER by date DESC";
$st = DB::getInstance()->query($sql, array(Input::get('formID')));
if ($st)
One can read and comprehend it. And noone died for splitting this call in four lines. Now add a couple:
$sql = "SELECT * FROM resum WHERE form_id = ? ORDER by date DESC";
$st = DB::getInstance()->prepare($sql);
$st->execute(array(Input::get('formID')));
$data = $st->fetchAll();
if ($data)
assuming getInstance() returns raw PDO instance. And your code will be perfectly fine working.
All right, I can understand a programmer's desire to avoid repetitions. Here is a solution that solves this problem without drawbacks:
$sql = "SELECT * FROM resum WHERE form_id = ? ORDER by date DESC";
$data = DB::prepare($sql)->execute(array(Input::get('formID')))->fetchAll();
And quit that idea of writing SQL calls right in the template. Fetch all your data first and then include a file with markup.
<?php if ($data): ?>
<div id="topSeparateurClientConc">
<div>Prénom et Nom</div>
<div>Adresse</div>
<div>Nom d'utilisateur</div>
<div>Date d'inscription</div>
<div>Demandes reçues</div>
</div>
<div style="clear:both;"></div>
<?php foreach($data as $result2): ?>
<div class="clientGenInfosConc">

Related

php query doesnt work

Hello I was building my Query class which I saw on YouTube, but I'm stuck. My Query function that allows you to use advanced SQL queries like SELECT * FROM market LIMIT ? OFFSET ? It binds values so I can't find any solution. Anybody help? What should I do?
My Query.php class contains
public function query($sql, $params = array())
{
$this->_error = false;
if ($this->_query = $this->db->prepare($sql))
{
$x = 1;
if (count($params))
{
foreach ($params as $param)
{
$this->_query->bindValue($x, $param);
$x++;
}
}
if ($this->_query->execute())
{
$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
$this->_count = $this->_query->rowCount();
} else {
$this->_error = true;
}
}
return $this;
}
Here I tried to select items from db like it's in my Query bellow
$i = 3;
$x = 100;
$sql = Query::getInstance()->query("SELECT * FROM market LIMIT ? OFFSET ?", array($i, $x));
var_dump($sql);
I didn't put here full source code, I think there is a problem in query function but I'm not able to find it.
ERROR IMAGE
Thanks nogad,
All i do is set this at the beginning of query function:
$this->db->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );

How do I set a DB2_SCROLLABLE with hold in PHP?

I've been working on creating an object oriented database call class for practice and I've been trying to figure out how to incorporate a scrollable cursor properly. I would like to be able to call this function in order to grab the next 10 records or a different function for the previous 10 records. I've gotten to the point where I can specify an offset for the records through the scrollable cursor but I cannot call the same function again to get the next set without passing in an incremented offset value.
How would I go about being able to specify a number of records per page and simply call getNextSet() or getPrevSet() in order to get the results I want.
My current line of thinking is setting a static variable somewhere and incrementing/deincrementing or resetting it accordingly then use that as the offset value in the functions. However, I believe I heard there was an issue with using static variables in instantiated classes.
Here's the code so far:
<?php
$i5DBconn = db2_connect("*LOCAL", "", "");
if (!$i5DBconn) {
echo "Database Connection failed. SQL Err:";
echo db2_conn_error();
echo "<br>";
echo db2_conn_errormsg();
die();
}
class queryDB{
var $query;
var $outputFunction;
var $parameters;
function performDBCall(){
global $i5DBconn;
$dbStatement = db2_prepare($i5DBconn, $this->query, array('cursor' => DB2_SCROLLABLE));
$i = 1;
foreach ($this->parameters as $param) {
db2_bind_param($dbStatement, $i, 'param', DB2_PARAM_IN);
$i++;
}
$result = db2_execute($dbStatement);
if (!$result){
echo "$this->query failed!. ";
echo 'SQLSTATE value: ' . db2_stmt_error();
echo ' Message: ' . db2_stmt_errormsg();
}
return $dbStatement;
}
}
class invoiceCall extends queryDB {
function testQueryFormatter($dbResultSet, $dataOffset){
$zebraStriping = 1;
?>
//snipped header row formatting
<?php
$rowLimit = 10;
while(($rowData = db2_fetch_array($dbResultSet, $dataOffset)) && ($zebraStriping < $rowLimit)){
list($dataNames) = $rowData;
?>
//snipped data formatting
<?php
$dataOffset++;
}
}
}
$offsetVal = 5;
$testQuery = new invoiceCall();
$testQuery->query = "select invoice#, status, amountpaid, amountbilled, invoicedate, paydate, custname, termofpay, balance from TESTSCHEMA.SAMPLE_INVOICE_TABLE";
//$testQuery->parameters = [8000001];
$resultSet = $testQuery->performDBCall();
$testQuery->testQueryFormatter($resultSet, $offsetVal);
?>
If there are also some coding practices I could improve on, let me know as I am starting my first real journey into Object Oriented PHP.

Recursive function for comment and reply PHP application

I am having difficulty conceptualizing a recursive function for appending replies to comments, replies to replies, replies to replies to replies, etc.
This is my comments table:
Which SHOULD look something like this when rendered:
As it stands I can render each comment associated with the article_id (excluding those that are NOT NULL, of course):
$comments = $commentClass->fetch_article_comments($article_id);
foreach($comments as $comment) {
$comment_id = $comment['comment_id'];
$member_id = $comment['member_id'];
$comment_text = $comment['comment_text'];
$comment_timestamp = timeAgo($comment['comment_timestamp']); //get time ago
//render comment using above variables
}
Now I'm assuming I need to add a recursive function at the end of the above foreach statement, but I have yet to come up with anything remotely successful in appending the comments replies and the replies to each reply.
Can anyone help me accomplish this or perhaps point me in the right direction. I am not entirely familiar with recursive functions. I have done some scanning of the internet and on stackoverflow looking for something that helps, but haven't found anything helpful yet.
I did come across this post which recommended using a hierarchical database system, but I think I would prefer to use a php recursive function for this.
Thanks.
edit
By using the below answers I am only returning the results first comment and then it iterates and displays that one comment indefinitely. I can't figure out why?
My php:
function display_comments($article_id, $parent_id=0, $level=0) {
global $comment;
global $member;
global $signed_in;
global $username;
$comments = $comment->fetch_article_comments($article_id, $parent_id);
foreach($comments as $data) {
$comment_id = $data['comment_id'];
$c_member_id = $data['member_id'];
$comment_text = $data['comment_text'];
$comment_timestamp = timeAgo($data['comment_timestamp']); //get time ago
$member_data = $member->fetch_member_data($c_member_id);
$c_member_username = $member_data['member_username'];
$c_member_avatar = $member_data['member_avatar'];
//render HTML here
$parent = $data['comment_parent'];
display_comments($article_id, $parent, $level+1);
}//end foreach
}//end display_comments()
and my PDO query:
public function fetch_article_comments($article_id, $parent_id) { //$page = (int)(!isset($_GET['page'])) ? 0 : $_GET['page'];
if ($parent_id > 0) {
$parent_id = "= $parent_id";
} else {
$parent_id = "IS NULL";
}
$query = $this->db->prepare("SELECT * FROM `comments2` WHERE `article_id` = $article_id AND `comment_parent` $parent_id ORDER BY `comment_timestamp` DESC");
try{
$query->execute();
$comments = $query->fetchAll(); //returns an array
$query->closeCursor();
return $comments;
} catch(PDOException $e){
die($e->getMessage());
}
}
The core of the problem is this:
$comments = $commentClass->fetch_article_comments($article_id);
I assume, this function somwhere creates and runs SQL, that is similar to SELECT ... WHERE comments.article_id=$article_id. This is not sufficient - you need something like
$comments = $commentClass->fetch_article_comments($article_id, $parent_id);
that translates into SQL similar to SELECT ... WHERE comments.article_id=$article_id AND comments.comment_parent ($parent_id>0)?"=$parent_id":"IS NULL"
Now you can write your PHP function:
function display_comments ($article_id, $parent_id=0, $level=0) {
$comments = $commentClass->fetch_article_comments($article_id, $parent_id);
foreach($comments as $comment) {
$comment_id = $comment['comment_id'];
$member_id = $comment['member_id'];
$comment_text = $comment['comment_text'];
$comment_timestamp = timeAgo($comment['comment_timestamp']); //get time ago
//render comment using above variables
//Use $level to render the intendation level
//Recurse
display_comments ($article_id, $comment_id, $level+1);
}
}
And finally call it with display_comments ($article_id);
You could do something like:
function getCommentsForParent($parent="") {
echo "<div class=\"comment\">";
$db = get("select your db logics where parent = $parent");
foreach ($db as $comment) {
echo "print your comment in a box and make a div around the hole comments"
getCommentsForParent($comment['parent_id']);
}
echo "</div>";
}
invoke function with getCommentsForParent()
With your code it should be something like:
function getCommentForParent($parent="") {
global $commentsClass, $article_id;
$comments = $commentClass->fetch_article_comments($article_id,$parent);
foreach($comments as $comment) {
$comment_id = $comment['comment_id'];
$member_id = $comment['member_id'];
$comment_text = $comment['comment_text'];
$comment_timestamp = timeAgo($comment['comment_timestamp']); //get time ago
getCommentForParent($comment['parent']);
}
}
Look closely, ive adjusted your fetch article_comments. It now has a listener for $parent. Make sure that when your $parent = "", the class will only select the comments with empty parent.
invoke with getCommentsForParent()
It will automatically loop through the parent. Ofcourse this is highly conceptual but given your code you should get the hang of it.

Memory/Time Efficient Database Entry

I have a foreach loop that iterates through an array.
In each instance, I organise the array into a query string and use MySQLi to add it to the database.
function storeProperties($data, $db) {
foreach ($data['property'] as $property) {
$query_string = "INSERT INTO table VALUES(..., ..., ...,)"
$db->query($query_string);
echo $db->error;
}
}
Is there a better way I should be doing this?
Obviously, this method uses n database queries one after another so this is memory intensive and time intensive.
Is there a better way to do this?
Should I be concatenating each query into a single string and running it all outside the for loop?
The following method is from my PDO workhorse, used for bulk insertions. It creates a single INSERT statement with multiple VALUES entries.
Use it as
$db->bulkinsert(
'tbl_my_tablename',
array('fieldname_1','fieldname_2', 'fieldname_3'),
array(
/* rec1 */ array('r1f1', 'r1f2', 'r1f3'),
/* rec2 */ array('r2f1', 'r2f2', 'r2f3'),
/* rec3 */ array('r3f1', 'r3f2', 'r3f3')
));
Please note that the method is an snip from a complex class definition, some methods used here are not contained in the code snippet, especially $this->connect() (connects to PDO),$this->begin() (starts transaction), $this->commit()and $this->rollback(), and the static Log class for Logging similar to Apache Commons ;-)
But I'm sure this is what you might need.
/**
* Performs fast bulk insertion
* Parameters:
* $tablename
* $datafields - non-assiciative array of fieldnames
* or propertynames if $data is an array of objects
* $data - array of either non-associative arrays (in the correct order)
* or array of objects with property names matching the $datafields array
*/
const MAX_BULK_DATA = 3000;
public function bulkinsert($tablename, $datafields, &$data) {
$result = 0;
try {
try {
$this->connect();
$datacount = count($data);
// loop until all data has been processed
$start = 0;
$lastbinds = 0;
$this->begin();
while ($start < $datacount) {
$ins = array();
$bindscount = min(self::MAX_BULK_DATA, $datacount - $start);
if ($bindscount != $lastbinds) {
// prepare the binds
$binds = substr(str_repeat(',?', count($datafields)), 1);
$binds = substr(str_repeat(",($binds)", $bindscount), 1);
$lastbinds = $bindscount;
}
for ($go = $start, $last = $start + $bindscount; $go < $last; $go++) {
if (is_object($data[$go])) {
try {
foreach($datafields as $propname) {
$rfl = new ReflectionProperty($data[$go], $propname);
$rfl->setAccessible(true);
$ins[] = $rfl->getValue($data[$go]);
}
}
catch(ReflectionException $e) {
throw new InvalidArgumentException('PDOCONNECT_ERR_SQL_UNKNOWN_PROPERTY', 0, $e);
}
}
else {
foreach($data[$go] as $value) {
$ins[] = $value;
}
}
}
$sql = sprintf('INSERT INTO %s (%s) VALUES %s', $tablename, join(',',$datafields), $binds);
Log::trace($sql);
$stmt = $this->pdo->prepare($sql);
$stmt->execute($ins);
$start = $last;
$result += $bindscount;
}
$this->commit();
}
catch(PDOException $e) {
// do something with the exception if necessary
throw $e;
}
}
catch(Exception $e) {
$this->rollback();
throw $e;
}
return $result;
}
}

display multiple items with different statuses

I don’t know how to search the net for the answer and I’m not sure how to explain the problem, so I’m sorry if it’s not clear or if it’s been asked before.
Here’s the deal: I need to show some items which have different statuses (there’s “unanswered”, “in discussion” and “answered”). What’s happening now is that the unanswered questions are being shown, the correct text is being shown for the questions that are in discussion, but the text for the question that are answered is the same as the “in discussion”. When one of the questions is moved from “unanswered” to “in discussion”, the correct text is being show for the “answered” questions. When there are no “unanswered” questions, the text is correct for that item. But the other items are getting the same text as the “unanswered” and the questions that should be shown in “in discussion” aren’t visible.
Does someone know what I can do to fix this? I'll put some code below. Thanks!!!
overzicht.php
<?php
session_start();
if(!isset($_SESSION['views']))
{
header('Location: index.php');
}
else
{
$feedback = "";
try
{
include_once('classes/question.class.php');
$oQuestion = new Question();
$oQuestionsUnanswered = $oQuestion->getQuestionsUnanswered();
$oQuestionsInDiscussion = $oQuestion->getQuestionsInDiscussion();
$oQuestionsAnswered = $oQuestion->getQuestionsAnswered();
}
catch(Exception $e)
{
$feedback = $e->getMessage();
}
}
?>
To show the different items (this is repeated twice for the other 2 statuses, with other variables e.g $oQuestionsAnswered):
<h3>Vragen onbeantwoord:</h3>
<div id="questionUnanswered">
<?php
if(isset($oQuestionsUnanswered))
{
$unanswered_details = "";
while($arr_unanswered = mysqli_fetch_array($oQuestionsUnanswered))
{
$unanswered_details .= "<div class='head'>";
$unanswered_details .= "<div class='titel'>";
$unanswered_details .= "<a href='full_topic.php?id=".$arr_unanswered['bericht_id']."'>".$arr_unanswered['bericht_titel']."</a></div>";
$unanswered_details .= "<div class='datum'>" . $arr_unanswered['bericht_datum'] . "</div></div>";
}
echo $unanswered_details;
}
else
{
echo $feedback;
}
?>
</div>
question.class.php (this is also repeated for the other 2)
public function getQuestionsUnanswered()
{
include('connection.class.php');
$sql = "SELECT *
FROM tblbericht
WHERE fk_status_id = 3;";
$result = $conn->query($sql);
if($result->num_rows!=0)
{
return $result;
}
else
{
throw new Exception("Er zijn momenteel nog geen onbeantwoorde vragen");
}
mysqli_close($conn);
}
IMO you make a big mistake, you split the query moment from the fetch moment, so you can create an array of question objects, than you use it inside the page.
Here's a draft of your code adapted:
public function getQuestionsUnanswered()
{
include('connection.class.php');
$sql = "SELECT *
FROM tblbericht
WHERE fk_status_id = 3;";
$result = $conn->query($sql);
$_rv =array()
if($result->num_rows!=0)
{
while($arr = mysqli_fetch_array($result))
{
$_rv[] = new QuestionBean($arr);
}
}
else
{
throw new Exception("Er zijn momenteel nog geen onbeantwoorde vragen");
}
mysqli_close($conn);
return $_rv
}
Then
<h3>Vragen onbeantwoord:</h3>
<div id="questionUnanswered">
<?php
if(count($oQuestionsUnanswered))
{
$unanswered_details = "";
foreach( $oQuestionsUnanswered as $bean)
{
$unanswered_details .= "<div class='head'>";
$unanswered_details .= "<div class='titel'>";
$unanswered_details .= "<a href='full_topic.php?id=".$bean->bericht_id."'>".$bean->bericht_titel."</a></div>";
$unanswered_details .= "<div class='datum'>" . $bean->bericht_datum . "</div></div>";
}
echo $unanswered_details;
}
else
{
echo $feedback;
}
?>
</div>
Of course you can optimize it using 1 class for all kind of question and 1 function using the paramiter to select wich kind of question you need.
The type of question will be a parameter of QuestionBean.
QuestionBean is a Bean :)
As bean I mean a "simple" data object where each attributes has a getter and setter OR is public.
I use the __constructor as initializer with a function called populate:
class simpleBean{
public $id;
public function __construct($params){
// some logic as need
}
// simple populate
public function populate($params){
$valid = get_class_vars ( self );
foreach($params as $k => $v){
if(!isset($valid[$k]){
// if this key has no attrib matchig I skip it
continue;
}
$this->$k = $v;
}
}
}
class QuestionBean extend simpleBean{
public $attr1;
public function __construct($params){
// may be I've some common logic in parent
parent::__construc($params);
//
$this->populate($params);
}
}
Now after the
while($arr = mysqli_fetch_array($result))
{
$_rv[] = new QuestionBean($arr);
}
you will have an array ($rv) of beans, each bean is a single result row of your query, but it's an object, that's much better than a stupid array.

Categories