How can I make this function
public function get_all_summary($year_dummy){
$current_year = $this->session->userdata('curr_year');
$new_db = $this->load->database('budget_db', TRUE);
$q = "select * from budget where sy=$current_year
AND sy_dummy=$year_dummy";
$query = $new_db->query($q);
return $query->result();}
to somewhat like this.
public function get_all_summary($year_dummy){
$current_year = $this->session->userdata('curr_year');
$new_db = $this->load->database('budget_db', TRUE);
$this->db->select('*');
$this->db->from('budget');
$this->db->where("sy",$current_year);
$this->db->where("sy_dummy",$year_dummy);
$query = $this->db->get();
return $query->result();}
The top function is correct but the bottom function is obviously wrong(I don't know how to select table from other db). I'm also connecting to other database and I'm selecting table from the other database(budget_db).
Hope you understand my problem.
I think you just need to use $new_db which is instance of budget_db.
public function get_all_summary($year_dummy){
$current_year = $this->session->userdata('curr_year');
$new_db = $this->load->database('budget_db', TRUE);
$new_db->select('*');
$new_db->from('budget');
$new_db->where("sy",$current_year);
$new_db->where("sy_dummy",$year_dummy);
$query = $new_db->get();
return $query->result();
}
Hope this might be useful for you.
Supposedly, 'budget_db' is the other database you are trying to connect to, make sure it has its own group defined in the database config. Otherwise, you can connect to it using
public function get_all_summary($year_dummy){
$current_year = $this->session->userdata('curr_year');
$config['hostname'] = "hostname";
$config['username'] = "db_user";
$config['password'] = "db_pass";
$config['database'] = "budget_db";
$config['dbdriver'] = "mysql";
$new_db = $this->load->database($config, TRUE);
$new_db->select('*');
$new_db->from('budget');
$new_db->where("sy", $current_year);
$new_db->where("sy_dummy", $year_dummy);
$query = $new_db->get();
return $query->result();
}
By adding the parameter TRUE to the load database method, $new_db becomes the database object of budget_db.
Related
here is my_sqli database connection
class FbChatMock {
// Holds the database connection
private $dbConnection;
private $_dbHost = 'localhost';
private $_dbUsername = 'root';
private $_dbPassword = '';
public $_databaseName = 'erp5_temp2';
public function __construct() {
$this->dbConnection = new mysqli($this->_dbHost, $this->_dbUsername,
$this->_dbPassword, $this->_databaseName);
if ($this->dbConnection->connect_error) {
die('Connection error.');
}
}
i am getting two parameters in the getchat function:
public function getchat($userId, $id){
$meesage = array();
$query = "SELECT u.user_id FROM `users` u where u.id='$id'";
$resultObj = $this->dbConnection->query($query);
$user = $resultObj->fetch_assoc())
$userid = $user;
}
I am getting the $userid variable from the query and using it inside this query:
$query = "SELECT u.id,c.message,c.sent_on FROM `chat` c JOIN
`users` u ON c.user_id=u.user_id where u.id='$id' AND c.user_id='$userid'";
// Execute the query
$resultObj = $this->dbConnection->query($query);
// Fetch all the rows at once.
while ($rows = $resultObj->fetch_assoc()){
$meesage[] = $rows;
}
return $meesage;
And the problem is that my first sql query is not working correctly . i have tested it by showing $userid value from echo.
Chage this line
$userid = $user;
To this
$userid = $user['user_id'];
Because $user it is array with all columns you have selected, and you need get only ID from it.
Your problem lies here :
$user = $resultObj->fetch_assoc())
$userid = $user;
because mysqli::fetch_assoc() with retrieve an associative array. Thus you have to access it like so (given the column name in your select):
$userid = $user["user_id"];
Hi guys I tried to use the variable equation on Codeigniter as in the code I wrote below, Is my code writing correct? and here's my code so far I wanna using variable $amount in my controller
In Controller
$amount = $this->m_data->getTotalSales->$data['TOTAL_SALES'];
and this in Model
//GET TOTAL REVENUE
function getTotalSales(){
$this->db->select("(SELECT SUM(grand_total) FROM sales_order WHERE member = ".$this->session->userdata('ID').") - (SELECT SUM(amount) FROM payment WHERE member_id = ".$this->session->userdata('ID').") AS total_sales");
$query = $this->db->get();
if ($query->num_rows() >0){
foreach ($query->result() as $data) {
$hasilSemua[] = $data;
}
return $hasilSemua;
}
}
$amount = $this->m_data->getTotalSales();
function getTotalSales(){
$result1 = $this->db->select_sum("grand_total")
->from('sales_order')
->where("member = $this->session->userdata('ID')");
->get();
$result2 = $this->db->select_sum("amount")
->from('payment')
->where("member_id = $this->session->userdata('ID')")
->get();
return $result1->row()-$result2->row();
}
}
I believe this is what you are after. Note that I pass the UserID in as the second parameter to the WHERE function. This is in case there is any chance of the user having inputted it (always safter to do this anyway).
//GET TOTAL REVENUE
function getTotalSales(){
$db = $this->db;
$db->select('SUM(grand_total)');
$db->where('member',$this->session->userdata('ID'));
$q1 = $db->get_compiled_select('sales_order');
$db->select('SUM(amount)');
$db->where('member_id',$this->session->userdata('ID'));
$q2 = $db->get_compiled_select('payment');
$query = $db->query("SELECT ($q1) - ($q2) as total_sales");
$data = $query->row_array();
return $data['total_sales'];
}
You can then call $amount = $this->m_data->getTotalSales; to get your result.
I have declared two databases in my database.php, how i can access the users list of db and db2 instances
//database initialisation
$CI =& get_instance();
$CI->db1 = $CI->load->database('default', TRUE);
$CI->db2 = $CI->load->database('stylior_db', TRUE);
//select query for db2
$this->$db2->from('users');
// $this->$CI->db2->where('id',1312);
$query = $this->$CI->db2->get();
print_r($query);
if ( $query->num_rows() > 0 )
{
$row = $query->row_array();
return $row;
}
Try this way
$db_result = $this->db->get("table_users"); // here default database connected
$this->db1 = $this->load->database('db1', TRUE);
$db1_result = $this->db1->get("table_users")
You can use $this keyword directly to get you result. You can use following code to get results:
//database initialisation
$CI =& get_instance();
$this->db1 = $this->load->database('default', TRUE);
$this->db2 = $this->load->database('stylior_db', TRUE);
//select query for db2
$this->$db2->from('users');
// $this->db2->where('id',1312);
$query = $this->db2->get();
print_r($query);
if ( $query->num_rows() > 0 )
{
$row = $query->row_array();
return $row;
}
I have a set of PHP functions for a URL shortener. I'd like to add another function which concerns updating a whole column in a MySQL table. So far I found the query which is: UPDATE urls SET redirect = 'http://example.com'
I'd like to have a text input where once I type a new URL and click "UPDATE ALL", the whole column in the database changes without having to do it from the database using the query I mentioned above. The only thing that stops me from doing so is the function(BOTTOM).
Here are the functions I mentioned earlier:
function createLink($slug,$url,$title,$disc,$thumb,$redirect){
global $nsqlQuery;
$f = array('slug','url','title','disc','thumb','redirect','time');
$v = array($slug,$url,$title,$disc,$thumb,$redirect,time());
$row = array_combine($f,$v);
$result = $nsqlQuery->insert("urls",$row);
return $result ? $result : false;
}
function getLink($by='NA',$val='NA'){
global $nsqlQuery;
if($by=='NA'):
$where = null;
else:
$f = array($by);
$v = array($val);
$where = array_combine($f,$v);
endif;
$result = $nsqlQuery->select("urls",'*',$where);
return $result ? $result : false;
}
function deleteLink($id){
global $nsqlQuery;
$f = array('id');
$v = array($id);
$where = array_combine($f,$v);
$result = $nsqlQuery->delete("urls",$where);
return $result ? $result : false;
}
Here's the public function that concerns updating the table and setting a new value to a certain column.
public function update($table,$row,$where,$where_condition=null,$dev='OFF'){
$query = "UPDATE ".$table." SET ".$this->getSetClause($row)." ".$this->getWhereClause($where,$where_condition);
if($dev=='ON')
return $query;
$response = $this->query($query);
return $response ? true : false;
}
Here's my progress so far which prompts a 500 error.
function updateRedirect($redirect){
global $nsqlQuery;
$where = $redirect;
$result = $nsqlQuery->update("urls",$where) SET ".$nsqlQuery->select("urls",'*',$where);
return $result ? $result : false;
}
The whole nsqlQuery.php file just in case.
Before anyone points out my code is flawed in security or etc know that I am quite a PHP noob and wouldn't mind you forwarding some help to fix that rather than just yelling it is terrible.
Also I did try this below and it won't work for me because it stores it into the session (Unless session is more secure than I thought. I assume users can extract data from one, correct?):
http://tinyurl.com/myqx3xo
As for my question, how would I be able to access the variable $connectdb in my users function? When I do that it gives me 'Undefined variable' error, and isn't detecting that it exists whatsoever. Both are requires in main\folder\start.php that is loaded every page, and on those pages I attempted to call the function and it gave me a failure. The code works fine when I attempt to hardcode the $connectdb's varible into the functions but again there are good reasons not to. Will add additional details if required.
Undefined variable: connectdb in main\folder\folder1\users.php on the line that starts with $data
main\folder\folder1\users.php function:
function user_data($id) {
$data = array();
$user_id = (int)$id;
$func_num_args = func_num_args();
$func_get_args = func_get_args();
if ($func_num_args > 1) {
unset($func_get_args[0]);
$fields = '' . implode(', ', $func_get_args) . '';
$data = mysqli_fetch_assoc(mysqli_query($connectdb,"SELECT $fields FROM users WHERE id = $id"));
return $data;
}
}
main\folder\folder2\connect.php:
<?php
$connect_fail = 'Example connection failure.';
$dbhost = 'host';
$dbuser = 'user';
$dbpass = 'pass';
$db = 'database';
$connectdb = mysqli_connect($dbhost, $dbuser, $dbpass, $db) or die($connect_fail);
?>
include your connect.php into your user.php
include('../fodler2/connect.php');
function user_data($id) {
$data = array();
$user_id = (int)$id;
$func_num_args = func_num_args();
$func_get_args = func_get_args();
if ($func_num_args > 1) {
unset($func_get_args[0]);
$fields = '' . implode(', ', $func_get_args) . '';
$data = mysqli_fetch_assoc(mysqli_query($connectdb,"SELECT $fields FROM users WHERE id = $id"));
return $data;
}
}
in your users.php file you need to add
include "../folder2/connect.php";