How can I convert this mysqli_fetch_array in php to codeigniter?
$kueri7 = "SELECT ceil(count(kelas_id)/(select count(user_id) from tuser where matpel_id = $idmatpel)) as jumlah from tkelas";
$cocok8 = mysqli_query($con, $kueri7);
$cocok_kelas_maksimal = mysqli_fetch_array($cocok8);
Thanks in advance.
With codeIgniter, your code would look like this:
$query = $this->db->query("SELECT ceil(count(kelas_id)/(select count(user_id) from tuser where matpel_id = $idmatpel)) as jumlah from tkelas", FALSE);
$cocok_kelas_maksimal = $query->result_array();
Related
I have written a code in PHP and not able to get a response from the code. Following is my code.
<?php
include_once("conectionFile.php");
$db = new Data();
$con = $db->Conn();
$val = $_REQUEST;
$lclQuery = "SELECT * FROM student_details WHERE st_id = ".$val."";
$lclResult = $con->query($lclQuery);
if($row = $lclResult->fetchAll(PDO::FETCH_ASSOC)) {
echo $res = json_encode($row);
}
?>
$lclQuery = "SELECT * FROM student_details WHERE st_id = ".$val."";
You need to change your query as it is not correct also $val is array so you need to use correct key there in order to get value for example $val = $_REQUEST['some_id'] then change query like
$lclQuery = "SELECT * FROM student_details WHERE st_id = '".$val."'";
I want to do a sql query like
select * where x = 10 and (y=12 or h=15)
How can I achieve that in CI ActiveRecord format?
See Active record reference
$where="x = 10 and (y=12 or h=15)";
$this->db->select('*');
$this->db->from('mytable');
$this->db->where($where);
$query = $this->db->get();
Try like
$sql = "SELECT * FROM TABLE_NAME WHERE x = 10 AND (y = 12 OR h = 15)";
$result = $this->db->query($sql);
Or like
$this->db->select('*');
$this->db->from('TABLE_NAME');
$this->db->where('x',10);
$this->db->AND('y',12);
$this->db->or_where('h',15);
$query = $this->db->get();
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;
I am using LIKE to do my searching, i try it in phpMyAdmin and return the result but when i use it in php it return empty result.
$search = "ip";
$start = 0;
$query = "SELECT * FROM product WHERE product_name LIKE '%$search%' LIMIT $start,30";
$result = mysql_query($query);
if(empty($result))
$nrows = 0;
else
$nrows = mysql_num_rows($result);
It will return result when i using phpMyAdmin to run this query but when i use it in php, it return empty.
Update:
Sorry guys,
I just found out the problem is i didn't connect database as well. anyway, thanks for helping.
Try This
$query = "SELECT * FROM `product` WHERE `product_name` LIKE '%".$search."%' LIMIT 0, 30";
And if the sole purpose of your code is to get the number of products with the searched-for name, use SELECT COUNT(*) instead of doing a mysql_num_rows() on all your data. It will decrease your querytime and the amount of data that is (unnecessarily) fetched.
I am not sure why this is not working, as the query seems to be correct to me. I would like to suggest you writing query this way
$query = <<<SQL
SELECT * FROM product WHERE product_name LIKE "%$search%" LIMIT $start,30
SQL;
please note that there should not be any space or any character after SQL;
$query = "SELECT * FROM product WHERE product_name LIKE '%" . $search . "%' LIMIT " . (int) $start. ",30";
you can use directly mysql_num_rows()
but here is right code
$query = "SELECT * FROM product WHERE product_name LIKE '%".$search."%' LIMIT $start,30";
$search = "ip";
$start = '0';
$query = "SELECT * FROM product WHERE product_name LIKE '%".$search."%' LIMIT $start,30";
$result = mysql_query($query)or die(mysql_error());
if(mysql_num_rows($result) == 0){
$nrows = 0;
} else{
$nrows = mysql_num_rows($result);
}
//use mysql_num_rows($result) instead of empty($result) because in this situation $result is every time not empty so use inbuilt PHP function mysql_num_rows($result);
How to use the codeigniter with $this->db->query() method?
If i use active record class i would do like this:
$query = $this->db->get('tb_cash_transaction',$num,$offset);
$this->db->order_by("CURRENCY_ID", "asc");
Now i am using the $this->db->query()
$query = "SELECT * FROM tb_cash_transaction, tb_currency, tb_user where tb_cash_transaction.CURRENCY_ID=tb_currency.CURRENCY_ID and tb_cash_transaction.USER_ID=tb_user.USER_ID and TYPE='cash_out'";
$query = $this->db->query($query);
How to implement it? Thank you.
Try with this one..
$query = "SELECT * FROM tb_cash_transaction, tb_currency, tb_user where tb_cash_transaction.CURRENCY_ID=tb_currency.CURRENCY_ID and tb_cash_transaction.USER_ID=tb_user.USER_ID and TYPE='cash_out' order by CURRENCY_ID asc LIMIT $offset , $num";
$query = $this->db->query($query);
Hope this will Help.
Thanks!
Hussain