I have a simple SQL query that is not working. I have tried everything I know to fix this query, but to no avail.
$Data = $connection->prepare("SELECT * FROM EXAMPLE");
The table on the database exists, and the connection to the database is set properly.
I have also tried this:
$Data = $connection->prepare("SELECT ID FROM EXAMPLE WHERE EXAMPLE1=:EXAMPLE1 ");
$Data->execute(array(
':EXAMPLE1' => $EXAMPLE1,
));
If your connection is fine..
$Data=$connection->prepare("SELECT ID FROM EXAMPLE WHERE EXAMPLE1 = :EXAMPLE1");
$Data->execute(array(
':EXAMPLE1' => $EXAMPLE1,
));
$result = $Data->fetchAll();
Or
$result = array();
$sql = "SELECT ID FROM EXAMPLE WHERE EXAMPLE1 = {$EXAMPLE1};";
foreach ($connection->query($sql) as $row) {
$result[] = $row;
}
i need some help , i have simple code like count rows in php, i use PDO ,
so i check if rowCount > 0 i do job if no other job but i have it in foreach function, in first step i get true result but in other i get invalid
so i think it is function like a closeCursor() in PDO but i try and no matter . maybe i do it wrong ?
it is part of my code
public function saveClinicCalendar($post){
$daysItm = '';
$Uid = $post['Uid'];
$ClinicId = $post['ClinicId'];
$type = $post['type'];
$resChck = '';
foreach($post['objArray'] as $arr){
foreach($arr['days'] as $days){
$daysItm = $days.",".$daysItm;
}
$daysItm = substr($daysItm, 0, -1);
$dateTime = $arr['dateTime'];
$sqlChck = 'SELECT * FROM clinic_weeks WHERE dates = :dates AND Uid = :Uid AND category = :category AND Cid = :Cid AND type = :type';
$resChck = $this->db->prepare($sqlChck);
$resChck->bindValue(":dates",$dateTime);
$resChck->bindValue(":Cid",$ClinicId);
$resChck->bindValue(":type",$type);
$resChck->bindValue(":Uid",$Uid);
$resChck->bindValue(":category",$Uid);
$resChck->execute();
$co = $resChck->rowCount();
if($co > 0){
/*UPDATE*/
$sql = 'UPDATE clinic_weeks SET dates = :dates ,time = :time, Cid = :Cid, type = :type, Uid = :Uid, category = :category ';
$res = $this->db->prepare($sql);
$res->bindValue(":dates",$dateTime);
$res->bindValue(":time",$daysItm);
$res->bindValue(":Cid",$ClinicId);
$res->bindValue(":type",$type);
$res->bindValue(":Uid",$Uid);
$res->bindValue(":category",$Uid);
}else{
/*INSERT*/
$sql = 'INSERT INTO clinic_weeks (dates,time, Cid,type,Uid,category) VALUES (:dates,:time, :Cid,:type,:Uid,:category)';
$res = $this->db->prepare($sql);
$res->bindValue(":dates",$dateTime);
$res->bindValue(":time",$daysItm);
$res->bindValue(":Cid",$ClinicId);
$res->bindValue(":type",$type);
$res->bindValue(":Uid",$Uid);
$res->bindValue(":category",$Uid);
}
$res->execute();
$resChck->closeCursor();
$resChck = null;
$daysItm = '';
}
}
what i am doing wrong?
many thanks to Barmar, he suggest me a true answer.
here is a code
$sql = "INSERT INTO clinic_weeks
(`timestam`,`time`,dates,Cid,type,Uid,category)
VALUES
('$timestamp','$daysItm','$dateTime','$ClinicId','$type','$Uid','$Uid')
ON DUPLICATE KEY UPDATE `time` = '$daysItm' ";
I use there "ON DUPLICATE KEY UPDATE" and it`s work perfectly!
instead a big code top of page i make a two line of code.
I am using bellow MYSQL query to delete single records from table and working perfect for me but how to write for multiple records with IN operator.
$sql = "DELETE FROM reg WHERE id = :id";
$query = $this->db->prepare($sql);
$query->execute(array(':id' => $id));
For example I have ids from array like $id = array(23,24); and I have treid with loop like bellow but not worked :(
for($i=0; $i<count($id); $i++) {
$id = $id[$i];
$sql = "DELETE FROM $table_name WHERE id = :$id";
$query = $this->db->prepare($sql);
$query->execute(array(':id' => $id));
}
I hope you understand my question and hope you will help me.
Thanks.
for($i=0; $i<count($id); $i++) {
$sql = "DELETE FROM $table_name WHERE id = :id";
$query = $this->db->prepare($sql);
$query->execute(array(':id' => $id[$i]));
}
In the below code i want to join or implode all arrays of $trackersurl in a single line. i am getting the results in different lines, so i want to join in a single line.
Can anyone help me out?
I am searching results in stackoverflow, but could not follow.
My code is in below:
$sql = "SELECT * FROM announce WHERE torrent = $id ORDER BY seeders DESC";
$query = #mysql_query($sql);
while ($result = #mysql_fetch_array($query)) {
$trackersurl1 = $result['url'];
$trackersurl2 = "&tr=".$trackersurl1;
$trackersurl = array($trackersurl2);
}
Results of [var.trackersurl] in html page is below:
&tr=http:ajgdsjhg/ann
&tr=udp://iuysidfu/ann
&tr=udp:wutefghgw/ann
&tr=http://sdhgsjdhgj/ann
I want to join them in a single line below
&tr=http:ajgdsjhg/ann&tr=udp://iuysidfu/ann&tr=udp:wutefghgw/ann&tr=http://sdhgsjdhgj/ann
You should be careful of sql injection.
Are you looking to create an array['trackers'] with a string of all the trackers for a magnet link?
<?php
$sql = "SELECT * FROM announce WHERE torrent = ".mysql_real_escape_string($id)." ORDER BY seeders DESC";
$query = mysql_query($sql);
$tracker = null;
if(mysql_num_rows($query)>=1){
while ($result = mysql_fetch_array($query)) {
$tracker .= "&tr=".$result['url'];
}
}
$tracker = array('trackers'=>$tracker);
//$tracker['trackers'] = "&tr=a.com&tr=b.com&tr=c.com";
?>
Try this code
$newArray=array();
while ($result = #mysql_fetch_array($query)) {
$trackersurl1 = $result['title'];
$newArray[] = "&tr=".$trackersurl1;
}
$urlString=implode('',$newArray);
PHP/MySQL (CodeIgniter)
I would like to add new interest_keywords in the exist database value.
here is my code
$query = 'SELECT u_interest_keyword FROM '.T_USER_ACCOUNT.' WHERE u_id = "'.$u_id.'"';
$result = $this->db->query($query);
$result_keyword = $result.','.$personal_keyword;
$query = 'UPDATE '.T_USER_ACCOUNT.' SET u_interest_keyword = "'.$result_keyword.'" WHERE u_id = "'.$u_id.'"';
$this->db->query($query);
It just replaces a new keyword in the database.
Can you tell me why it doesn't work?
$this->db->query returns object when read type queries are run.
So, you have to do something like this after $result = $this->db->query($query);
$result_row = $result->row();
Then Rectify this:
$result_keyword = $result_row->u_interest_keyword. ',' .$personal_keyword;
$row = $result->row();
$result_keyword = $row->u_interest_keyword.','.$personal_keyword;