db fetch array drupal 7 - php

I've an issue with a migration from a drupal 6 to 7 website. I'm a beginner in PHP and MySQL and I can't find a solution to my problem.
The code I'm struggling with is following:
$sql = "select ID_Speler from TB_Spelers where uid = ".$id;
$row = db_fetch_array(db_query($sql));
$speler = $row['ID_Speler'];
I always get a "Call to undefined function db_fetch_array()"
Any help is very much appreciated.

Use this in Drupal 7:
$query = db_select('field_data_field_order_no', 'fdfon');
$query->addField('fdfon', 'entity_id', 'nid');
$query->addField('fdfnt', 'field_notification_type_value', 'type');
$query->join('field_data_field_notification_type', 'fdfnt', 'fdfon.entity_id = fdfnt.entity_id AND (fdfon.bundle = :fdfon_bundle AND fdfnt.bundle = :fdfnt_bundle)', array(':fdfon_bundle' => "order_notification_type", ':fdfnt_bundle' => "order_notification_type"));
$query->condition('fdfon.field_order_no_value', $order_id)->orderBy('fdfnt.entity_id', 'asc');
$result = $query->execute();
while ($records = $result->fetchAssoc()) {
...
}

There's no db_fetch_array() in Drupal 7, the (almost) equivalent code would be
$sql = "select ID_Speler from TB_Spelers where uid = :uid";
$args = array(':uid' => $uid);
$row = db_query($sql, $args)->fetchObject();
$speler = $row->ID_Speler;
See the Database API docs for more info.

Try like that:
$sql = "select ID_Speler from TB_Spelers where uid = %d";
$query = db_query($sql, $id);
while ($records = db_fetch_array($query)) {
$spelers[] = $records['ID_Speler'];
}
print_r($spelers);

Related

Sample PDO Query don't work

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;
}

how to use PDO rowCount() function in foreach?

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.

Deleting Multiple Rows in a MySQL Database using PHP PDO extension

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]));
}

How can i join or implode the below array code?

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 select value and update value

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;

Categories