Getting value from database custom table not working in wordpress - database

I have created a wordpress table with wp_compare_post with following column id, user_id, post_id, post_type. I have attached the image of the database.
Below is the coading for viewing data from database
function compare_counting(){
global $wpdb;
//get user id
$user_ID = get_current_user_id();
$sql = "SELECT * FROM $wpdb->compare_post WHERE user_id = '$user_ID '";
$results = $wpdb->get_results($sql) or die(mysql_error());
foreach( $results as $result ) {
echo $result->post_id.',';
}
}
But getting following error.
Notice: Undefined property: wpdb::$compare_post in

I think it could be because you need to use $wpdb->prefix
Try this:
$sql = "SELECT * FROM ".$wpdb->prefix."compare_post WHERE user_id = '$user_ID'";

Related

Laravel add values to object based on foreign key

I have a TABLE A called diagnoses . It has a disease_id and a visit_id. I am trying to return the diagnosis from TABLE A along with the disease name and visit details. I keep getting sql syntax errors. Is there a recommended way to push an objects to the array?
My code is
public function diagnosis_diseases(Disease $disease) {
$id = $disease->id;
$items = DB::select(DB::raw('SELECT * FROM diseases WHERE diseases.id = '.$id.' ;'));
foreach($items as $item){
$diagnosis = DB::select(DB::raw(' select * from diagnoses
where disease_id = '. $id.';' ));
$items->push($diagnosis);
}
dd($items);
}
You might need to use an inner join:
public function diagnosis_diseases(Disease $disease) {
$id = $disease->id;
$items = DB::select(DB::raw('SELECT * FROM diseases WHERE diseases.id = '.$id.' ;'));
foreach($items as $item){
$diagnosis = DB::select(DB::raw(' select * from diagnoses
inner join diseases on diagnoses.id = ' .$id' '));
$items->push($diagnosis);
}
dd($items);
}

How to show Data Use "Stored Procedure (Exec) Query SQL Server" codeigniter

CI_Controller ..
public function index(){
$this->load->database();
$this->load->model('model_contoh');
$data = $this->model_contoh->GetDataByProc();
echo '<pre>' ;
print_r($data->result_array());
}
}
CI_Model ..
function GetDataByProc(){
$data = $this->db->query("spCekSelisihJurnal '1/1/2018 00:00:00','12/31/2018 23:00:00'");
return $data ; }}
if this query execute in sql tool success to show the data
but not run in codeigniter ?
enter image description here
Do you not call like this. P1 and P2 would need to need procedure parameter names
$p_1 = '1/1/2018 00:00:00'
$p_2 = '12/31/2018 23:00:00'
$stored_procedure = "CALL spCekSelisihJurnal(?,?) ";
$result = $this->db->query($stored_pocedure,array('P1'=>$p_1,'P2'=>$p_2));

Query execution in codeigniter

I have a situation where I need to fetch details of an employee from the database using his ID and display them in the browser.
$sql = "SELECT * FROM employeesalarypayment WHERE empid = ".$val['empid'].";";
$query = $this->db->query($sql);
These are the statements that I have written to get the result array. My problem is how do I take a single field/column from this array? Also have I done it correctly?
Thanks in advance.
If your query return single data from database then you need to use
$row = $query->row_array()
Try this
$sql = "SELECT * FROM employeesalarypayment WHERE empid = ".$val['empid'].";";
$result = $this->db->query($sql)->result_array();
echo $result[0]['field_name'];

How to check whether value is in database without throwing an exception in Zend?

I have a registration action in Zend controller and I want to ensure that given username isn't already in the database.
$tUser = $userMapper->getDbTable();
$select = $tUser->select()
->from(array('u'=>'users'))
->where('u.user_username = ' . $value);
$row = $tUser->fetchRow($select);
Unfortunately, this snippet of code throws the Exception when i just want to check the $row is null or not and keep writing my code.
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'admin' in 'where clause', query was: SELECT u.* FROM users AS u WHERE (u.user_username = admin) LIMIT 1
Should I remove the try/catch blocks in my controller or can I do it in another way?
Judging by error description, I'd say it will throw an exception always, no matter if user exists or not, because bad SQL query will be generated. Try passing $value as second parameter to where, like this:
$tUser = $userMapper->getDbTable();
$select = $tUser->select()
->from(array('u'=>'users'))
->where('u.user_username = ?', $value);
$row = $tUser->fetchRow($select);
You will then check if user exists like this:
if(!$row) {
// user doesn't exist
} else {
// user exists
}
try {
$tUser = $userMapper->getDbTable();
$select = $tUser->select()
->from(array('u'=>'users'))
->where('u.user_username = ?', $value);
$row = $tUser->fetchRow($select);
} catch (Zend_Db_Exception $e) {
echo "<pre>";print_r($e);
}

update query sql table in yii

How can I update row in table in yii? I am using the following code but it is not working
$sql = "UPDATE auth_assignment SET itemname = 'Authenticated' WHERE userid = $user->accountID";
$command = $connection->createCommand($sql);
$command->execute();
My guess is that $user is being converted to a string and thus the ->accountID is not working. You have two methods, one unsafe and one safe.
Unsafe - Add {} around the $user->accountID.
$sql = "UPDATE auth_assignment SET itemname = 'Authenticated' WHERE userid = {$user->accountID}";
Safer - Use a parametrized query:
$sql = "UPDATE auth_assignment SET itemname = 'Authenticated' WHERE userid = :userid";
$command = $connection->createCommand($sql);
$command->execute(array(':userid' => $user->accountID))
ok I forgot to put commas around
'user->accountID'

Resources