Codeigniter | Calling Stored Procedure with IN and OUT pratameters in MYSQL - mysql

This is my procedure... it's going to accept a parameter of $encoder as IN and then it will output the number of members as #current, #cancelled and #year it was encoded as OUT. I got one IN parameter ans four OUT parameters... and it works.
The problem is how am i going to do this in Codeigniter 3.
I've tried several codes refering to the codeigniter manual but i got this error..
Error Number: 1172
Result consisted of more than one row
CALL get_members_by_status(25,#current,#current,#year1)
Filename: models/Dashboardcounts.php
Line Number: 189
This is the Mysql Procedure code..
BEGIN
-- current members
SELECT
count(memstat) INTO current
FROM
members
WHERE
encoder = encoder_num
AND memstat = '1';
-- canceled members
SELECT
count(memstat) INTO cancelled
FROM
members
WHERE
encoder = encoder_num
AND memstat = '0';
-- year encoded
SELECT
date_format(memdateencode,"%Y") INTO year1
FROM
members
WHERE
encoder = encoder_num;
END
This is my Model Code...
public function linegraphchurchmemberbychurch_2(){
$query = $this->db->query('CALL get_members_by_status(25,#current,#current,#year1)');
$query1 = $this->db->query('SELECT #current');
foreach ($query1->result_array() as $row){
$data = $row;
}
return $data;
}
Any hint?

Related

retrieve data by inner join with defined variable in Codeigniter

I want to get the following result of the mysql code by codeignitor.
It gives non equals id's(tbl_user) for the provided time frame in tbl_user_unavailability table
MySql code
SELECT tbl_user.id
FROM tbl_user
INNER JOIN tbl_user_unavailability
on tbl_user_unavailability.user_id <> tbl_user.id
WHERE '2018-02-02' BETWEEN begin_date AND end_date;
Result
22
25
find the mysql code result screenshot here
to get this result by codeignitor ,I have used following codes
Controller code
public function test(){
$current_date = '2018-02-02';
$available_users = $this->mod_users->test_test($current_date);
$data['available_users'] = $available_users;
//to test the result array
//var_dump($available_users);die();
$data['related_view']='user_unavailable_new';
$this->load->view('template', $data);
}
Model code
public function test_test($current_date){
$this->load->database();
$this->db->select('*');
$this->db->join('tbl_user_unavailability', 'tbl_user_unavailability.user_id <> tbl_user.id','inner');
$this->db->where("'$current_date' BETWEEN 'tbl_user_unavailability.begin_date' AND 'tbl_user_unavailability.end_date'");
$query = $this->db->get($this->table);
return $query->result_array();
}
Result
not showing anything
I have tested it with var_dump($available_users);die();
It shows empty array

cakephp - How can I update a record using Hash class function

I am new to CakePhp and want to Update record.
I have successfully fetched records from my table.
$SQL = "SELECT Id, Name FROM Table WHERE Id = '123';
return Hash::combine($this->query($SQL), '{n}.{n}.Id', '{n}.{n}');
Above code returns records properly.
Table Name: Table__c
Model Name: Table.php
Controller Name: TablesController.php
I have tried following:
$sObjects = array();
$sObject = new stdClass();
$sObject->Id = 123;
$sObject->Name = "abc";
array_push($sObjects, $sObject);
$result = $this->Table__c->save($sObjects);
which gives me error: Call to a member function save() on a non-object
Earlier I was using Salesforce and following code was fine:
$this->query(array('update', $sObjects, 'Table__c'));
But with MySQL it gives me error:
SQLSTATE[42000]: Syntax error or access violation: 1065 Query was empty
Thank you guys for your comments I appreciate that.
Answer:
If your table name and model name do not match by adding in your model class:
var $useTable = 'Table_Name';
you can use that table.
Now how are you going to update record in this table:
$sObject = new stdClass();
$sObject->Name = "'abc'";
$sObject->Address = "'xyz'";
$condition = array('Id' => "123");
$this->updateAll((array)$sObject, $condition);
updateAll() function will update records matching the $condition

What is the correct syntax for a select query in drupal

If I have a table named book
Column 1 = current_user
, Column 2 = page_length
, Column 3 = author
, Column 4 = title
I'd like to select the data from columns 2, 3 and 4 that correspond to the currently logged in user. Is the following correct syntax?
<?
global $user;
$user_id=$user->name;
db_query('SELECT * FROM {book} WHERE current_user=$user_id', $page_length, $author, $title); ?>
Some tips:
You need to make sure to use quotation marks around the string that is the SQL statement.
Since the username is a user-supplied string, you should use parameter escaping to prevent SQL injections.
You don't really need to assign the user name to a separate variable ($user_id) to use it.
You need to retrieve your result from the return value of db_query.
Fixed code:
<?php
global $user;
$res = db_query("SELECT page_length, author, title FROM {book} WHERE current_user = '%s'", $user->name);
$row = db_fetch_array($res);
// now $row['page_length'], $row['author'] and $row['title'] are filled in with values if the query was successful
?>

Resource Id #22 inserted into database field

I'm having a problem with running this function. When it runs, it does exactly what I want, except that within my like_requests table the request_id is not the mysql query result linked to the variable $select but Resource Id #22. I thought that resource id's appear when you are trying to echo out a result, but I'm not using echo. What's wrong with the code?
function update_likes($band_requested, $new_likes, $session_user_id) {
$select = mysql_query("SELECT `primary_id` FROM `requests` WHERE
`user_requester_id` = '$session_user_id' AND `person_requested` =
'$band_requested'");
$sql_2 = "INSERT INTO `like_requests` (user_id, request_id) VALUES
('$session_user_id', '$select')";
mysql_query($sql_2);
}
$band_requested = 'rally done';
$new_likes = 239;
$the_session_user_id = 3;
update_likes($band_requested, $new_likes, $the_session_user_id);
UPDATE WITH CORRECTED ANSWER
Here is the code corrected with help from David.
function update_likes($band_requested, $new_likes, $session_user_id)
{
$select = mysql_query("SELECT `primary_id` FROM `requests` WHERE `user_requester_id` =
'$session_user_id' AND `person_requested` = '$band_requested'");
$row = mysql_fetch_row($select);
$request_id = $row[0];
$sql_2 = "INSERT INTO `like_requests` (user_id, request_id) VALUES ('$session_user_id',
'$request_id')";
mysql_query($sql_2);
}
mysql_query returns a resource (http://php.net/manual/en/function.mysql-query.php) not just a scalar value. You'd need to use a function like mysql_fetch_row() to get the, presumably, one row you want, assign that row to a variable $row, then retrieve the primary_id with array syntax like $row['primary_id']. By the way, apparently mysql_query is being eased out and we should use the MySQLi API with the mysqli_query() method.

PHP: Help modifying tables on MySQL database

Following this following example...
public function insert($model,$brand,$price){
mysql_query("INSERT INTO autos (model, brand, price) VALUES ($model,'$brand', $price)",$this->link);}
$conexion-> insert(05,"Ford",50000000);
...I made a modify/update code:
public function modify($model,$brand,$price,$newbrand,$newprice){
mysql_query("UPDATE 'crautos'.'autos' SET 'brand' = '$newbrand',
'price' = '$newprice' WHERE 'autos'.'model' =5 AND 'autos'.'brand' = '$brand' AND 'autos'.'price' ='$price' LIMIT 1) VALUES ($newbrand, $newprice)",$this->link);}
$conexion-> modify("Mitsubishi", 40000000);
But it didn't work, this is the error output:
Warning: Missing argument 3 for MyDataBase::modify(), called in on
line 15 Warning: Missing argument 4 for MyDataBase::modify(), called
in on line 28 and defined in on line 15 Warning: Missing argument 5
for MyDataBase::modify(), called in on line 28 and defined in on line
15 AND undefined variable newprice, newbrand, price. THE Insert code
is fine, and the modify code is causing the issue.
Here's the full code:
<?php
class MyDataBase{
private $link;
public function __construct($server,$user,$password,$base){
//Conectar
$this->link = mysql_connect($server,$user,$password);
mysql_select_db($base,$this->link);
}
public function insert($model,$brand,$price){
mysql_query("INSERT INTO autos (model, brand, price) VALUES ($model,'$brand', $price)",$this->link);}
public function modify($model,$brand,$price,$newbrand,$newprice){
mysql_query("UPDATE 'crautos'.'autos' SET 'brand' = '$newbrand',
'price' = '$newprice' WHERE 'autos'.'model' =5 AND 'autos'.'brand' = '$brand' AND 'autos'.'price' ='$price' LIMIT 1) VALUES ($newbrand, $newprice)",$this->link);}
public function __destruct(){
//desconectar
}
}
$conexion = new MyDataBase ('localhost', 'root', '','crcars');
$conexion-> insert(05,"Ford",50000000);
$conexion-> modify("Mitsubishi", 40000000);
?>
There is an invalid syntax in the update query. You do no need the VALUES part. Here is how it should be:
mysql_query("UPDATE 'crautos'.'autos' SET 'brand' = '$newbrand', 'price' = '$newprice' WHERE 'autos'.'model' =5 AND 'autos'.'brand' = '$brand' AND 'autos'.'price' ='$price' LIMIT 1");
Your UPDATE syntax is invalid, like #Martin pointed out, also you're calling modify() with two parameters, but the function is expecting 5. Add the extra values to $conexion-> modify("Mitsubishi", 40000000);
Something like:
$conexion-> modify("Mitsubishi", 40000000, $price, $newbrand, $newprice);