I have the following code and the output will be in array. How can I find arrays that match between $pesara and $ahli. the condition between it is CURRENT_ID_NO is equal to ic_no
$sql = "SELECT CURRENT_ID_NO, NAME, MOBILE_NO FROM pesara";
$dbCommand = Yii::app()->db->createCommand($sql);
$pesara = $dbCommand->queryAll();
$sql2 = "SELECT name, ic_no, ic_type, tel_no, pesara, created_dt, created_by, updated_dt, updated_by FROM ost_ahli";
$dbCommand2 = Yii::app()->db->createCommand($sql2);
$ahli = $dbCommand2->queryAll();
$result = array_intersect($pesara->CURRENT_ID_NO, $ahli->ic_no);
print_r($result);
Sorry for my bad English but I hope someone can help me on this.
You can use array_uintersect() for custom comparison
and also have sample code for that here
Related
I have a function which runs daily which I want to check if a post is expired (based on a custom postmeta field) and if so set the post status to draft. I think I have the principle right but there's something probably pretty wrong with the syntax which isn't making it work correctly. This is the function so far:
function make_product_draft( ) {
global $wpdb;
$sql = "
UPDATE $wpdb->posts
INNERJOIN $wpdb->postmeta on $wpdb->posts.ID = $wpdb->postmeta.post_id
SET $wpdb->posts.post_status = 'draft'
WHERE $wpdb->posts.post_type = 'product' AND $wpdb->posts.post_status = 'publish' AND $wpdb->postmeta.post_type = 'expired'
";
$wpdb->query($sql);
}
I think where I am going wrong is when I do this: $wpdb->posts.ID? I am not sure what the correct syntax is. Your help is much appreciated :)
My requirements changed since I made the post, however the following code worked for me. Thanks Niklaz for the comment, your assistance was pivotal in getting it working right :)
function make_product_draft( ) {
global $wpdb;
$sql = "UPDATE {$wpdb->posts}
INNER JOIN {$wpdb->postmeta} on {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id
SET {$wpdb->posts}.post_status = 'draft'
WHERE {$wpdb->posts}.post_type = 'product' AND {$wpdb->posts}.post_status = 'publish' AND {$wpdb->postmeta}.meta_key = '_product_expiry' AND {$wpdb->postmeta}.meta_value < CURDATE()
";
$wpdb->query($sql);
}
I'm using this and it works fine.
$stmt1 = $conn->prepare("SELECT * FROM Authors WHERE First=:id");
$stmt1->bindParam(':id', $_GET['name'], PDO::PARAM_STR);
blah blah...
It also works when I change WHERE 'First=:id' to 'Last=:id' but it fails when I include an OR clause as below.
$stmt1 = $conn->prepare("SELECT * FROM Authors WHERE First=:id OR Last=:id");
$stmt1->bindParam(':id', $_GET['name'], PDO::PARAM_STR);
I found this below in Stack #3030650.
$stmt = $dbh->prepare("SELECT * FROM REGISTRY WHERE firstname = :name OR lastname = :name");
$stmt->bindParam(':name', $name);;
Since these appear the same, what am I overlooking.
Thanks for any advice.
I would bind it twice, perhaps with different placeholders even though it's the same value:
$query = "SELECT * FROM REGISTRY WHERE firstname = :fname OR lastname = :lname";
$stmt = $dbh->prepare($query);
$stmt->bindParam(':fname', $name);
$stmt->bindParam(':lname', $name);
Please, i have this query here:
$query_pag_num = "SELECT COUNT(*) AS count FROM forma";
$result_pag_num = odbc_exec($connection, $query_pag_num) or die(odbc_error());
$row = odbc_fetch_array($result_pag_num);
$count = $row['id'];
The issue is i get this error here:
Undefined index: id where it's: `$count = $row['id'];`
Help please! It won't work without this piece of code here.
I'm using odbc_connect..
Thanks in advance..
your select function will only return 1 column: "count". you are trying to get to column "id"
here is your fixed code
$query_pag_num = "SELECT COUNT(*) AS count FROM forma";
$result_pag_num = odbc_exec($connection, $query_pag_num) or die(odbc_error());
$row = odbc_fetch_array($result_pag_num);
$count = $row['count'];
you do not have any field in your SELECT called 'id'. Try: $count = $row['count'];
i have tried to make sql code in CI, the problem is the ID_LABEL at t_publisher is filled with 0 where it should filled with number that taken from t_label..
$sql['query1'] = "INSERT into t_user (USER_NAME, USER_PASS, USER_STATUS, USER_TYPE) values ('$user', '$pass','1','publisher')";
$sql['query2'] = "INSERT INTO t_label (LABEL) values('$user')";
$id_label = "select id_label from t_label where label ='".$user."'";
$id = $this->db->query($id_label)->result();
$sql['query3'] = "INSERT INTO t_publisher (PUBLISHER, ARTIS, ID_LABEL) values('$user', 'Various Artist', '$id')";
$result = array();
foreach($sql as $key => $value){
$result[$key] = $this->db->query($value);
}
please help :)
to get the id_label
try this
$id_result = $this->db->query($id_label);
foreach($id_result->result_array() as $row){
$id=$row['id_label'];
}
$id is returning an object not a single value. Try inserting $id->id_label instead of just $id
I wonder how to do:
params= new object[]{ new SqlParameter("ID",val),new SqlParameter("Label","'%Name%'")};
statement = "Select ID, Label from Country Where ID = ? or or Label Like ?";
I tried and it gave me :
Incorrect syntax near '?'.
You have a typo:
"Select ID, Label from Country Where ID = ? or or Label Like ?";
should be
"Select ID, Label from Country Where ID = ? or Label Like ?";