Unknown column in 'where clause? - php

I Have the following code of php
$query = sprintf("SELECT to_go.to_location FROM to_go
INNER JOIN to_location ON to_go.to_location_id = to_location.id
WHERE match(to_location ) against(%s)", mysql_real_escape_string($location));
i tried every thing but it keep output me that following error "Unknown column in 'where clause ?" i tried to change the names of the columns and still the same problem

match(to_location ) against needs to be provided a field, not a table:
match(to_location.id) against(something)

I guess you may need to replace
WHERE match(to_location )
with
WHERE match(to_go.to_location)

Since you have a column name the same name as a table name, MySql is probably confusing them and thinking match(to_location) refers to the table. Try using the fully-qualified column name, i.e., table_name.column_name.

Related

Laravel Query Builder weird column names conversion

I am trying to figure out why is DB::raw() queries are getting sent as lowercases?
Query = DB::('table_name')->select(['is_read as isRead'])->get();
Result = [{ isRead: true }]
But when I do raw its converting it to lowercase
Query = DB::('table_name')->select(DB::raw('is_read as isRead'))->get();
Result = [{ isread: true }]
I have a reason to use DB raw so I really need to figure this thing out.
I'm not able to reproduce the issue you mention... Which version of Laravel are you using?
For instance, if I run the following:
DB::table('users')->select(DB::raw('is_read as isRead'))->get();
I would get the error:
Illuminate/Database/QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'is_read' in 'field list' (SQL: select is_read as isRead from users)'
Which is normal because I have no is_read column in my users table. However, you can see in the error that the isRead is not converted to lowercase.
Maybe you can use selectRaw instead:
DB::table('table_name')->selectRaw('is_read as isRead')->get();
The query would be:
select is_read as isRead from `table_name`
Otherwise, could you update your question to provide more information on how to reproduce so your issue with the capitalization.
I can confirm that when using DB:raw (or orderByRaw or any row raw methods) column names get converted to lowercase.
To avoid that, you can try to protect the name with quotes:
Query = DB::('table_name')->select(['is_read as "isRead"'])->get();

Laravel: Left join sql error

I have written a sql query using Laravel, but I am not understanding, why the error is creating! The codes are given below,
The join operation:
MeetingRoom::select('mr_id')
->leftJoin('meetingroomhistory',function($join)
{
$join->on('country','=',Session::get('country'));
$join->on('location','=',Session::get('location'));
$join->on('building','=',Session::get('building'));
$join->on('floor','=',Session::get('floor'));
$join->on('name_of_mr','=',Session::get('room'));
})
->where('meetingroom.id','=','meetingroomhistory.mr_id')
->get();
The error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Bangladesh' in
'on clause' (SQL: select `mr_id` from `meetingroom` left join
`meetingroomhistory` on `country` = `Bangladesh` and `location` =
`Dhaka` and `building` = `Uttara` and `floor` = `3` and `name_of_mr` =
`1` where `meetingroom`.`id` = meetingroomhistory.mr_id)
But if I run the query manually to change this syntax `Bangladesh` to "Bangladesh" using phpmyadmin so it runs well and show me the result. Please someone let me know, how can I fix it?
I think you've confused the where part and the join part.
$join->on($x, $y) statements are for connecting your tables together. Both $x and $y should be names of columns in your tables - that is why Laravel is adding backticks to them.
$where() statements are for comparing with values that you supply. Your Session::get() calls indicate that's what you're doing.
Here's a suggested rewrite, but it may need to be tweaked to suit your exact needs.
MeetingRoom::select('mr_id')
->leftJoin('meetingroomhistory', 'meetingroom.id', '=', 'meetingroomhistory.mr_id')
->$where('meetingroom.country','=',Session::get('country'))
->$where('meetingroom.location','=',Session::get('location'))
->$where('meetingroom.building','=',Session::get('building'))
->$where('meetingroom.floor','=',Session::get('floor'))
->$where('meetingroom.name_of_mr','=',Session::get('room'))
->get();
I assumed the country, location, etc. columns are part of the meetingroom table, if not you can just change them to $where('meetingroomhistory.country', ...) and so on instead.
Finally, I'm not sure why you're selecting only one value, but of course, that's up to you. If you need only a single cell, you can use pluck(), and if you need a lot of single results you could do lists().
I think you should try this.
MeetingRoom::select('mr_id')
->join('meetingroomhistory', function($join)
{
$join->on('meetingroom.id','=','meetingroomhistory.mr_id')
->where('meetingroomhistory.country','=',Session::get('country'));
->where('meetingroomhistory.location','=',Session::get('location'));
->where('meetingroomhistory.building','=',Session::get('building'));
->where('meetingroomhistory.floor','=',Session::get('floor'));
->where('meetingroomhistory.name_of_mr','=',Session::get('room'));
})
->get();

Weird MYSQL results using PDO::FETCH_ASSOC without column alias

I have a simple left join query.
SELECT e.employee_id as employee
, e.badge_id as badge
, e.first_nm as first
, e.last_nm as last
, e.work_phone as work_ph
, e.mobile_phone as mobile_ph
, e.manager_id as man_id
, e.title_id as titl_id
, e.username as user
, e.start_dt as start
, m.employee_id as memp_id
, m.last_nm as m_last
, m.first_nm as m_first
, t.title_nm as titl_nm
FROM employee e
left join employee m
on e.manager_id = m.employee_id
left join title t
on e.title_id = t.title_id
WHERE e.employee_id = 1
If I use column aliases as I have done above, the query works fine. If I do not use aliases, however, some values do not get returned. For example, the following returns a space if I do not give the column an alias.
e.first_nm as first //returns "Robert"
e.first_nm //returns ""
e.first_nm as first_nm //returns "" (alias matches column name)
In this same query,
e.middle_nm //will return "P"
regardless of whether it has an alias or not. I'm baffled.
I have given my tables aliases and I have used the table alias in the column names so there shouldn't be any ambiguous column names.
Any thoughts would be appreciated.
Thanks,
Rob
You have two columns with same name as first_nm and problably the PDO donĀ“t know what return to your code then return simple "". Although they are in diferent tables when came to a record they have the same name... you see the problem?
You have two columns with same name as first_nm and mysql knows how to return them all right, and then return simple "first_nm" for both. And then PDO have to assign them to array members, making field names as array keys. There is only one way, like this
$row['first_nm'] = first col;
$row['first_nm'] = second col;
If you try to see into $row, how many entries you will find?
So, you either have to use FETCH_ROW or give your fields distinct names. It's neither mysql nor PDO to blame - it's just how the things work.

MySQL Error - Unknown column - need to edit

Thanks for reading.
Reading through all the articles on here helped me locate my problem, I'm just not sure how I should edit the code. I still learning.
The message I get is:
SELECT attachid
FROM ilace_attachment WHERE attachtype = 'ads' AND user_id ='6' AND ads_id='1'
MySQL Error : Unknown column 'ads_id' in 'where clause'
Error Number : 1054
This all started after I upgrade my software script to a newer version. I checked the ads_ads in MYSQL and there isn't a colum for ads_id, just one called; id.
I believe the solution to my problem is to change the "ads_id" to just "id". But I'm not sure if thats right or what I should change.
$sql = $ilace->db->query("SHOW TABLE STATUS LIKE '". DB_PREFIX ."ads_ads'");
$ads_id_temp = $ilace->db->fetch_array($sql);
$ads_id=$ads_id_temp['Auto_increment'];
}
else
{
$ads_id=$ilace->GPC['id'];
}
$attachid = $ilace->db->fetch_field(DB_PREFIX . "attachment", "attachtype = '".'ads'."' AND user_id ='".$_SESSION['ilacedata']['user']['userid']."' AND ads_id='".$ads_id."'", "attachid");
Here is the script it runs.
http//wwwWEBSITEcom/campaign.php?id=0&cmd=_create- campaign&1=Advertise+here+for+%245.00+per+1000+views&2=Targeted+AdTITLENAME+adverts&3=http%3A%2F%2FwwwWEBSITEcom%2Fcampaign.php%3Fcmd%3Dcreate%26mode%3Dppc&4=Vist&zone=header&mode=PPI&clicks=0&5=1&keywords=KEYWORD1%2C+was%2C+KEYWORD2%2C+KETWORD3%2C+KEYWORD4&dotw[1]=1&dotw[2]=1&dotw[3]=1&dotw[4]=1&dotw[5]=1&dotw[6]=1&dotw[0]=1
You change AND ads_id= to AND id= because that is the field name in the SQL statement, and seemingly the field name has changed.
You do not change $ads_id because that is the name of your PHP variable, and this works fine as it is and does not need to be the same as the field name.
On a wider level, you should sit and figure out how the last line of the PHP quoted puts together the SQL statement quoted in the error. You should know that the . is used to concatenate strings together, that PHP strings must start and end with the same character but that can be either ' or ", and that the SQL statement requires ' around values.
Also, if you've updated a third-party software script and it's now incompatible with your database you should look to see if there was some sort of data migration script that you've not run.
Depends. Is the id column containing the same information as ads_id? If so, yes you should change it. If no, you should figure out what happened to the ids and why they were removed.

codeigniter select as

how doHow do we do
select table.value as table_value from table in codeigniter?
The AS part doesnt work because when i try to access the value,
this doesnt work:
$qry_inp = 'select table.value as table_value from table '
$query = $this->db->query($qry_inp);
echo $query->row('table_value ');// this will be empty, but it shouldn`t be
doesn`t matter if its in AR or simple query
Pretty simple thing.
$this->db->select('COLUMN_ACTUAL_NAME as `COLUMN_NAME_YOU_WANT_TO_SHOW`');
i'm joining two tables in which column names are same so i separate both tables columns by using as keyword , this is how you can use AS in codeigniter
$this->db->select("departments.name AS 'dname'");
$this->db->select('positions.name');
Where is that behaviour documented? row doesn't take a column name as a parameter; it optionally takes a row number, and that's it. Access it like any other field:
echo $query->row()->table_value;

Categories