how do I delete rows in Yii? - yii

Using Yii, I want to delete all the rows that are not from today.
Is my solution ok ?
$query = "delete from `user_login_hash` where `day`!='".(date('Y-m-d',time()))."'";
Yii::app()->db->createCommand($query);

A prettier solution is
YourUserModel::model()->deleteAll("day !='" . date('Y-m-d') . "'");

Better user PDO parameters and on command you also have to call execute
$query = "delete from `user_login_hash` where `day`<> :date";
$command = Yii::app()->db->createCommand($query);
$command->execute(array('date' => date('Y-m-d')));
or
UserLoginHash::model()->deleteAll(
'day <> :date',
array('date' => date('Y-m-d'))
);

Try this...
$query = "delete from `user_login_hash` where `day`!='".(date('Y-m-d',time()))."'";
$query->queryAll($query);

You may use query builder
$command = Yii::app()->db->createCommand()
->delete('user_login_hash', 'day !=' . date('Y-m-d'));
http://www.yiiframework.com/doc/guide/1.1/en/database.query-builder#sec-15

Related

PDO bind variables to prepared mysql statement and fetch using while loop

I've used several of your guides but I can not get the following to run. If I hardcode the 2 variables in the Select statement it runs fine. I need to use variables, and I can't get the bind statement to work. Plenty of experience with the old Mysql_ but the PDO is still a challenge at this point.
$db_table = "ad";
$ID = "1";
$dsn = "mysql:host=$hostname;dbname=$database;charset=$charset";
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
$pdo = new PDO($dsn, $username, $password, $opt);
$result = $pdo->prepare("SELECT * FROM :ad WHERE id= :id "); // Line 359
$result->bindParam(':ad', $db_table, PDO::PARAM_STR);
$result->bindParam(':id', $ID, PDO::PARAM_STR);
$result->execute();
while($row = $result->fetch(PDO::FETCH_ASSOC))
{
$product = $row["product"];
$msrp = $row["msrp"];
$sale = $row["sale"];
$content = $row["content"];
echo "<strong>$product</strong> - $content<br />";
// echo $msrp . "<br />";
if($msrp != "0.00") { echo "MSRP $$msrp"; }
if($sale != "0.00") { echo "<img src='/images/c.gif' width='75' height='6' border='0'><span style='color: red;'>Sale $$sale</span>"; }
}
$pdo = null;
The above generates this error,
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '? WHERE id=?' at line 1' in
/XXXXXXXXXXXX/index_desktop_pdo.php:359
Your database structure is wrong. There should be always only one table to hold all the similar data. And therefore no need to make a variable table name.
To distinguish different parts of data just add another field to this table. This is how databases work.
So your code should be
$section = "ad";
$ID = "1";
$result = $pdo->prepare("SELECT * FROM whatever WHERE section=:ad AND id= :id");
$result->bindParam(':ad', $section);
$result->bindParam(':id', $ID);
$result->execute();

Insert Returning last ID with PDO

I am trying to get the last/current ID submitted in an Insert, I have tried lastInsertId however that didn't work. Alternatively I have used returning on the end of my insert. However that was using pg_sql. How would I use the returning line, with PDO? I am stuck with the logic of getting the value displayed using PDO in the second option.
php 5.1.6
See below
Doesn't Work
$stmt ->execute();
$newsheetID = $conn->lastInsertId('sheet_id');
echo $newsheetID . "last id";
Works But is pg_sql, I would like to get this working for PDO
$sql = "INSERT INTO sheet_tbl (site_id, username, additionalvolunteers) VALUES ('$_POST[site_id]', '$username','$_POST[additionalvolunteers]') returning sheet_id";
echo $sql;
$result = pg_query($sql);
while ($row = pg_fetch_row($result)) {
$sheet_id_post = $row[0];
echo $sheet_id_post . '<br/>';
Your looking for this, if you cannot get lastInsertId to work, this will do the job, a couple of extra lines tho:
foreach ($stmt as $row)
{
$sheet_id_post = $row[0];
echo $sheet_id_post;
}
You can always msg me. Or head to php website look for a similar loop and alter it, my answer is very similar to yours and when I first did this I just went to php.net :-)

Laravel Order By a alias

in code below i get a user_name columm not found if i remove my "workaround", how i can set the alias before the get command? have a trick?
//Select
$qb = DB::table('log as l');
//Joins
$qb->join('user as u', 'l.user_id', '=', 'u.id');
//Orders
if($sort[0]["field"]=="user_name")// This is a workaround for my problem
$sort[0]["field"] = "u.name";
$qb->orderBy($sort[0]["field"],$sort[0]["dir"]);
//Wheres
Data::applyFilter($qb,$filters);
$total = $qb->count("l.id");
$qb->skip($skip)
->take($take);
$result = $qb->get(array("l.id", "l.action", "l.entity_type", "l.entity_id", "u.name as user_name", "l.datetime"));
In documentation there is
$users = DB::table('users')->select('name as user_name')->get();
If you set a key-value variable instead of "user_name" it should do what you want ?
Bye

How to get particular column in zend using Left join

I am new to zend framework,
Following is the plain mysql query which takes particular column from table,
SELECT jobs_users.id,jobs_users.first_name from jobs_users left join friends on jobs_users.id=friends.friend_id where friends.member_id=29
I tried with zend to implement the above query like below,
public function getFriendsProfileList($id){
$db = Zend_Db_Table::getDefaultAdapter();
$select = $db->select();
$select->from('jobs_users')
->joinLeft(
'friends',
'jobs_users.id=friends.friend_id',
array('jobs_users.id','jobs_users.first_name','jobs_users.last_name','jobs_users.photo')
)
->where("friends.member_id = ?", $id);
$result = $db->fetchAll($select);
return $result;
}
Here i got result with all column name , not with exact column name which i have given in query.
Kindly help me on this.
Use this instead:
$select->from('jobs_users', array('jobs_users.id','jobs_users.first_name','jobs_users.last_name','jobs_users.photo'))
->joinLeft('friends', 'jobs_users.id=friends.friend_id')
->where("friends.member_id = ?", '20');
You may also try this:
$select = $db->select();
$select->setIntegrityCheck(false);
$select->joinLeft('jobs_users','',array('jobs_users.id','jobs_users.first_name','jobs_users.last_name','jobs_users.photo'));
$select->joinLeft('friends','jobs_users.id=friends.friend_id', array());
$select->where("friends.member_id = ?", $id);
$result = $db->fetchAll($select);
return $result;

getting data from 2 different table with JOIN sql. Codeigniter

I use codeigniter, and I need to get data from 2 different table. for now it returns data only from works_image table. how can I get data from both 2 table ?
thanks a lot!!
$this->db->select('works_image.*', 'works.*');
$this->db->from('works_image', 'works');
$this->db->join('works', 'works.id = works_image.id_work');
$result = $this->db->get();
foreach ($result->result() as $row) {
echo " # " . $row->id . " - " . $row->thumb . " - " . $row->wname . "";
}
As long as you're doing a SELECT * (Why is this a bad idea?), you shouldn't need to specify tables with the call to select(). It will select all fields by default.
$this->db->from('works_image', 'works');
$this->db->join('works', 'works.id = works_image.id_work');
$result = $this->db->get();
Should work fine.
Instead, what you really should be doing, is specifying exactly which fields you need:
$this->db->select('works_image.id, works_image.name, works_image.id_work, works.id, works.name'); // (or whichever fields you're interested in)
$this->db->from('works_image', 'works');
$this->db->join('works', 'works.id = works_image.id_work');
$result = $this->db->get();
This way you can be sure that (a) you're not pulling unnecessary data from your DB, and (b) your code won't break if/when you modify your DB schema.
This post should answer your question: http://www.whypad.com/posts/codeigniter-activerecord-join-tip/178/
In short, you need to rewrite your select statement from
$this->db->select('works_image.*', 'works.*');
to this:
$this->db->select('works_image.*, works.*');
Note that this was the first result on Google for 'CodeIgniter join'. Try Googling your questions first. You can often get yourself a faster answer :)
You should be to write below code
$this->db->select('works_image.*', 'works.*');
$this->db->from('works_image');
$this->db->join('works', 'works.id = works_image.id_work');
$result = $this->db->get();
foreach ($result->result() as $row) {
echo " # " . $row->id . " - " . $row->thumb . " - " . $row->wname . "
";
}
I think that you get your result