I have a piece of code for getting conferences from database, where a specific subscriber is not subscribed.
I have two tables:
conferences - which holds all conferences
read - which holds subscriber with conferences
Here is the code, but I get MySQL 1064 error.
function getPossibleConferencesOfSubscriber($sub_id)
{
$a = "SELECT * FROM conference C where C.ISSN not in (SELECT ISSN FROM read where sub_id=$sub_id)";
$query = $this->db->query($a);
return $query->result();
}
Ok, I found the problem. I guess "read" is something like a command, so MySQL confuses the table name read and command read. I hope this helps someone, saves his 1 hour.
Related
I'm trying to write a natural joint on eloquent but without success, please could you help me?
So my sql request is:
SELECT * FROM `case_managers` NATURAL JOIN risks
I have did
Case_managers::join('risks')->get()
Thank you :)
You can use 'with' instead of join like
Case_managers::with('risk')->get();
where risk is your model name related to risks table.
Create a relationship in Case_managers model.
public function risks()
{
return $this->hasOne('App\Models\Risk','foreign_key','local_key');
//if there is going to be multiple entries,change `hasOne` as `hasMany`
}
You can access risks as
$res = Case_managers::find($id);
echo $res->risks->risk_field; //risk_field is the column name you want from risks table
//if `hasMany` is the relationship,use `foreach`
foreach($res->risks as $risk)
{
echo $risk->risk_field;
}
I'm having problems to do a simple permission system on my Webapp. My DB has a table called "usuario" that has informations about the users of the system. One of these columns is called "privilegio" that has value '0' for administrators and 1 for regular users. An administrator has the power to Add and edit users on the system. Im trying to take this behavior querying my database with the cod of the logged user and getting its permission. If the user is not on the administrator group (privilegio=1) then the add/edit/delete buttons will be unset.
public function usuario() {
if($this->session->userdata('logged')){
$crud = new grocery_CRUD();
$crud->set_subject("Usuário");
$crud->set_theme('datatables');
$crud->set_table("usuario");
(...)
$crud->field_type('privilegio','dropdown',array('0'=>'Administrador','1'=>'Usuario'));
(...)
$this->db->select('privilegio');
$this->db->get('usuario');
$result = $this->db->where('cod_func',$this->session->userdata('cod_func'));
if(!$result){
$crud->unset_add();
$crud->unset_edit();
$crud->unset_delete();
}
(...)
The problem (and the question) is that this code only list the user that is logged on, not the others already registered on the system and stored on "usuario" table. I wonder that the list is been made by my query (what is not the behavior I would like) I hope you could undestand my doubt. Sorry for my bad english.
Thank you!
you're having trouble with the active record functions...
When you use the function
$this->db->get('usuario');
This translates to the query:
SELECT * FROM usuario
So try changing your code to something like this:
$this->db->select('privilegio');
$this->db->from('usuario');
$this->db->where('cod_func',$this->session->userdata('cod_func'));
$this->db->limit(1); //You're only expecting one result
$result = $this->db->get(); // Save the result to the variable result
//Edited for the comment, something like
$result = $result->first_row('array'); //Here I'm fetching only the first row into an array
$privilegio = $result['privilegio']; //Im saving the result from the query in the variable $privilegio
This translates to:
SELECT priviliegio FROM usuario WHERE cod_func = 'some_value' LIMIT 1;
Then you can do whatever you want with the $result variable, please refer to documentation to see what you can do...
Generating Query Results
I have been trying to join two custom table using magento's commands. After searching i came across this block of generic code
$collection = Mage::getModel('module/model_name')->getCollection();
$collection->getSelect()->join( array('table_alias'=>$this->getTable('module/table_name')),
'main_table.foreign_id = table_alias.primary_key',
array('table_alias.*'),
'schema_name_if_different');
Following this as template I have tried to join my tables together but have only returned errors such as incorrect table name or table doesn't exist or some other error.
Just to clear things up can someone please correct me on my understanding
$collection = Mage::getModel('module/model_name')->getCollection();
Gets an instance of your model. Within that model is the table that holds the required data (for this example I shall call the table p)
$collection->getSelect()
Select data from table p
->join()
Requires three parameters to join two table together
PARAM1
array('table_alias'=>$this->getTable('module/table_name'))
'the alised name you give the table' => 'the table you want to add to the collection (this has been set up in the model folder)'
PARAM2
'main_table.foreign_id = table_alias.primary_key'
This bit i don't get (it seems straight forward though)
my main table (p) doesn't have a foreign id (it has it's primary key - is that also its foreign id)?
has to be equal to the alised name you gave in param1
PARAM3
'main_table.foreign_id = table_alias.primary_key'
get all from alised name
Where have I gone wrong on my understanding?
Please have a look in below sql join statement, I am using it in my project and it is working perfectly.
Syntax
$collection = Mage::getModel('module/model_name')->getCollection();
$collection->getSelect()->join(Mage::getConfig()->getTablePrefix().'table_name_for_join', 'main_table.your_table_field ='.Mage::getConfig()->getTablePrefix().'table_name_for_join.join_table_field', array('field_name_you_want_to_fetch_from_db'));
Working Query Example
$collection = Mage::getModel('module/model_name')->getCollection();
$collection->getSelect()->join(Mage::getConfig()->getTablePrefix().'catalog_product_entity_varchar', 'main_table.products_id ='.Mage::getConfig()->getTablePrefix().'catalog_product_entity_varchar.entity_id', array('value'));
Hope this will work for you !!
I made two database connections in database.php, one for reading which is my localhost machine and one for writing "insert" which is an online database.
But I ran into this problem when I wanted to join the two tables "answers" in the localhost database and "user" in the online database.
function get_users() {
$write_db = $this->load->database('default', TRUE); /* database conection for localhost DB */
$read_db = $this->load->database('read', TRUE); /* database conection for online DB */
$read_db->select('*');
$read_db->from('ibitstore.user');
$write_db->join('english.answers','ibitstore.user.user_id = english.answers.user_id');
$query = $read_db->get();
if($query->num_rows() > 0)
return $query->result_array();
return array();
}
When I use $write_db with the join clause
$write_db->join('english.answers','ibitstore.user.user_id = english.answers.user_id');
data is retrieved from the online database but not from the localhost database, like it ignored the join I tried to use:
$read_db->join('english.answers','ibitstore.user.user_id = english.answers.user_id');
Then I got this SQL error. Looks like it thought that the "answers" table is on the online DB:
A Database Error Occurred
Error Number: 1142
SELECT command denied to user 'user'#'49.32.197.240' for table 'answers'
SELECT *
FROM (`ibitstore`.`user`)
JOIN `english`.`answers`
ON `ibitstore`.`user`.`user_id` = `english`.`answers`.`user_id`
Filename: C:\xampp\htdocs\english\system\database\DB_driver.php
Line Number: 330
Please Help :(
You only have one connection to a database at a time. You'd have to run a query on one database, run a query on the second database and then manipulate the results of both within PHP.
You can run one SQL Query to be executed on two or more different databases. Containing tables of each.
How ever you can get the data from one database then use your own logic to merge them.
Hello all i have a problem with my forum
my forum have a categori in that i show the threads thats created on that categori, and i show the user profile picture, the user name who created and the user name who last replay on the topic.
my problem is that if there is 2 comment on a topic it will show the topic 2 times in the categori like this: http://d.pr/QxAY
and my code is this:
traad = thread
kommentare = comments
$this->db->select('*,users.profile_picture as profil_billed, forum_traad.id as traad_id,
forum_kommentare.brugernavn as comment_username');
$this->db->from('forum_traad');
$this->db->join('users', 'forum_traad.brugernavn = users.username');
$this->db->join('forum_kommentare', 'forum_traad.id = forum_kommentare.fk_forum_traad', 'left');
$this->db->where('forum_traad.fk_forum_kategori', $id);
$this->db->order_by("forum_traad.id", "DESC");
I think your problem will be solved if you use MYSQL's GROUP_CONCAT function on the comments column of your result.
By doing this you will get topic only once and the multiple comments to that topic in the comma seperated format which you can seperate in your code later.
After trying this Let Me Know.