I am trying to insert data from feed into the database using Zend and also adding a column date_updated in table so that whenever the article in feed is updated the column also gets updated. But the problem is, the first article is being inserted earlier and thenafter the others. So, when I am trying to do a select of top 10 articles based on date_updated DESC, the article being inserted at last is getting on top and if i use ASC then the older ones are getting selected. Please suggest how do i proceed. The query I am writing is:
$sql = "INSERT INTO news_article
(original_article_id, headline,summary, keywords, link, section, topic, date_published, date_updated, content, source_id)
VALUES (?,?,?,?,?,?,?,?,?,?,?)
ON DUPLICATE KEY UPDATE
original_article_id = ?,
headline = ?,
summary = ?,
keywords = ?,
link = ?,
section = ?,
topic = ?,
date_published = ?,
date_updated = ?,
content = ?,
source_id = ?";
$values = array(
"original_article_id"=>$id,
"headline"=>$item->title,
"summary"=>$summary,
"keywords"=>$keywords,
"link"=>$item->link,
"section"=>"property",
"topic"=>"property",
"date_published"=>$formattedPubDate,
"date_updated"=>$currentDate,
"content"=>$data,
"source_id"=>"3"
);
$result = $db->query(
$sql,
array_merge(array_values($values), array_values($values))
);
and thenafter i am using
SELECT * FROM news_article ORDER BY date_updated DESC LIMIT 10
use below query
SELECT * FROM news_article ORDER BY date_updated DESC LIMIT 0,10
After limit we need to pass offset and number of records to fetch.
Related
I based on the sales person to loop and insert the data with 3 rows into database 2. But I cannot insert the category with 2 times with 2 sales person based on the first row, it keep get the last row of category data and insert to database. Like below image of 1.
$product_type = $request->post('product_type');
$emp_amount = $request->post('amount');
foreach($_POST['employee'] as $index => $employee_list){
$statement = $this->db->prepare('
INSERT INTO `selling_employee` (
invoice_no, payment_id, category, sale_person, amount, created_at
) VALUES (?, ?, ?, ?, ?, ?)
');
$statement->execute([
$invoice_id, $payment_id, $product_type[$index], $employee_list, $emp_amount[$index], $created_at
]);
}
Any solution how to detect the first row got more than sales person and insert multiple same data based on first row.
I wonder if it is possible to optimize my queries to avoid having to send ten requests, to check if three main tables and two composite tables has the needed elements and if not then insert them.
("Composite table" is what i call a table that holds two primary keys that both are a reference to the primary key in another table, cause i do not know the correct term)
So i have tried to see if i can get all the needed ids from the three main tables in one query, but i can not it it to work.
Here is some of the things i have tried.
select t1.a AS EID, t2.a AS CID, t3.a AS IID from (select ID as a from Events where event = "test3") as t1, (select ID as a from Coordinates where Coordinates = "10.22,14.15") as t2, (select ID as a from Intervals where Interval = "From 12:15 To 18:30") as t3;
SELECT ID AS eid FROM Events WHERE event = "test3" UNION ALL SELECT ID AS cid FROM Coordinates WHERE Coordinates = "10.22,14.15" UNION ALL SELECT ID AS iid FROM Intervals WHERE Interval = "From 12:15 To 18:30"
And this is what i am currently doing
if(!empty($this->event) && $this->event!== false){
$sql = "SELECT ID FROM Events WHERE event = ?";
$values = array($this->event);
$db->setQuery($sql, $values);
$db->singleFetch();
if($db->getResults() == 1){
$eid = $db->getFetch()[0]['ID'];
}
else{
$sql = "INSERT INTO Events (event, Description) VALUES (?, ?)";
$values = array($this->event, $this->description);
$db->setQuery($sql, $values);
$db->singleQuery();
$eid = $db->getLast();
}
}
//I am doing this for all three main tables,
//to get the ids needed for me to be able to do this.
if(isset($eid) && isset($cid)){
$sql = "SELECT count(*) FROM EC WHERE EID = ? AND CID = ?";
$values = array($eid, $cid);
$db->setQuery($sql, $values);
$db->singleFetch();
if($db->getFetch()[0]['count(*)'] == 0){
$sql = "INSERT INTO EC (EID, CID) VALUES (?, ?)";
$values = array($eid, $cid);
$db->setQuery($sql, $values);
$db->singleQuery();
}
}
//Which i then do for both composite tables
Either i get my fetch array with every possible combination or i do not even get my fetch array and other times i get it but with two of the same event id and nothing else.
You can write the stored procedure in MySQL for this and then you need to call that stored procedure
This is the query for SQL statement
UPDATE TrendingEvents
SET Name = ?, Image = ?, Date = ?, EventCategory = ?
WHERE ID = ?;')
I would like to access the ID of the other table within my TrendingEvents table. This is the example I've done although it doesn't fully work;
UPDATE TrendingEvents INNER JOIN
Events AS eID
ON TrendingEvents.ID = Events.ID
SET Name = ?, Image = ?, Date = ?, EventCategory = ?
WHERE eID = ?;')
I would like to update the TrendingEvents table with the ID column from Events table.
The error I'm getting from my statement is
Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'Name'
Which just shows that I've poorly designed my query.
Both tables seem to contain a column called Name. You need to properly prefix the fields with the table name, like :
UPDATE
TrendingEvents AS t
INNER JOIN Events AS e
ON t.ID = e.ID
SET
t.Name = ?,
t.Image = ?,
t.Date = ?,
t.EventCategory = ?
WHERE e.eID = ?
You have misused the table alias, so your query should not even compile. Try this:
UPDATE TrendingEvents te INNER JOIN
Events e
ON te.ID = e.ID
SET te.Name = ?,
te.Image = ?,
te.Date = ?,
te.EventCategory = ?
WHERE e.eID = ?;
I found myselfe lately in need of pagination therefore i dug up my old mysql_query script that have served its purpose. I tried to redone it so it fits for my current MySQL PDO class but one thing isnt right.
The problem is that to find out how many pages query will produce, i need to first conduct entire query without LIMIT'ation, than i have to conduct again the same query to be able to limit it, it feels like waste of resources and time, those queries are really long, take approx 20 different filters to do it, so i believe that i must be doing something wrong.
Is there a way to check using PDO how many rows are there in total in the same time as applying LIMIT into it?
For now this is what i have to do:
exec i.e. this query:
SELECT * FROM inventory WHERE
season = ?,
category = ?,
code = ?,
storage = ?,
price > ?,
price < ?,
purchase_date > ?,
purchase_date < ?,
index_date > ?,
index_date < ?,
product_class = ?
and than i got access to afected rows so i can exec it again and append LIMIT:
SELECT * FROM inventory WHERE
season = ?,
category = ?,
code = ?,
storage = ?,
price > ?,
price < ?,
purchase_date > ?,
purchase_date < ?,
index_date > ?,
index_date < ?,
product_class = ?
LIMIT {$BEGIN}, {$END}
Thank you in advance for all the help :)
Why wouldn't you store all the query's result into an array, and print only the 10 first, then [11-20]. (10 is the arbitrary number of results showed)
Something like
for($i = ($page - 1) * 10; $i <= ($page * 10); i++)
{
echo $result[i]['season'];
// other things
}
I need to read the date that a request was created from our website. When that request is created, the information corresponding to that request and its meta-request is inserted in the DAI_REQ.REQUEST and DAI_REQ.META_REQUEST tables, respectively. We also have a dev server and a public deployment server. The problem happens only on our deployment server for some reason..
Unfortunately, the INSERT query to insert the information of the meta-request in the DAI_REQ.META_REQUEST table does not work, but the SELECT query I do right after does (so in my eyes, this removes any connection problems with the database/table itself). I also use the same syntax as the INSERT query I do on the DAI_REQ.REQUEST, so I do not think it is a query syntax problem. I also tried manually inserting as line within sql-server and it works fine. Finally, I echo'ed the value of $this->userId that I use as a parameter for the INSERT query to see if it contained the right ID, and it does. I did the same for the return value of $this->db->query(...), and it does NOT return anything (on our deployment server only).
I also know that my way of retrieving the last inserted row in a table is not perfect, but this is not the problem at hand here and it will be changed later on.
Here is the actual code where the problem happens:
public function dbInsert(){
// The actual problematic query
$this->db->query("INSERT INTO DAI_REQ.META_REQUEST ".
"(DATE_RECU, DATE_TERMINEE, USER_ID, STATUS) ".
"VALUES(GETDATE(), '', ?, 'R');", array($this->userId));
// This works fine though
$mr_select = $this->db->query("SELECT TOP 1 ID FROM DAI_REQ.META_REQUEST WHERE USER_ID = ? ORDER BY ID DESC;",
array($this->userId));
$mr_result = $mr_select->result_array();
$mr_id = $mr_result[0]['ID'];
$sim = 'N/A';
if(isset($this->recurrenceType))
$sim = 'Recurrent';
$this->db->query("INSERT INTO DAI_REQ.REQUEST ".
"(USER_ID, ASSIGNED_DATE, REQUEST_END_DATE, MODEL, EXPERIMENT, VARIABLE, START_DATE, END_DATE, ".
"LON_FROM, LAT_FROM, LON_TO, LAT_TO, RESOLUTION, FORMAT, SIMULATION, STATUS, ".
"CANCELLED_YN, PROJECT, MR_ID, URL_ORIGIN, DATE_EMAIL) ".
"VALUES(?, GETDATE(), '', ?, 'N/A', 'N/A', ?, ?, ?, ?, ?, ?, ?, ?, ?, 'R', 0, 'N/A', ?, ?, ?);",
array($this->userId, $this->model, $this->startDate, $this->endDate,
$this->lonFrom, $this->latFrom, $this->lonTo, $this->latTo,
$this->resolution, $this->format, $sim, $mr_id, $this->url_origin, $this->date_email));
$r_select = $this->db->query("SELECT TOP 1 ID FROM DAI_REQ.REQUEST WHERE USER_ID = ? ORDER BY ID DESC;",
array($this->userId));
$r_result = $r_select->result_array();
$this->id = $r_result[0]['ID'];
}
The database that the deployment server is using isn't set up to auto increment the ID column. In Microsoft SQL Server, for the ID column, you can set the Identity to Yes and Identity Increment to whatever number you want the ID column to increment by.