CodeIgniter fails Join Operation - mysql

I need to use a join operation. This is my code:
$this->db->from('d');
$this->db->where('id',$v);
$this->db->join('p', 'p.id = d.id');
$deal=$this->db->get();
The exception is:
Server returned HTTP response code: 500
The table is:
p(id,home) and d(id, p.id(this value is from p table),school);
What's wrong?

in your query
$this->db->from('d');
$this->db->where('id',$v);
$this->db->join('p', 'p.id = d.id');
$deal=$this->db->get();
where caluse is ambigeous, you have to put it like this
$this->db->where('d.id',$v);
also try
$this->db->join('p', 'p.id = d.id', 'inner' or 'left');
don't put both just put either inner or left in the join query,hopefully this will solve your problem and also make sure p, d are not synonyms they are actuall name of tables which exists in DB with same p, d names and have valid columns.

You have to try and check a few things in here:
1) Whether your database.php file contains the proper credentials. Look for any silly syntax errors as well.
2) Whether you are loading the database class?
3) Some times if you have installed the php package, the mysql driver would not have got installed. check whether you have installed mysql dirver php5-mysqlnd?

Related

what is wrong with this line of SQL?

SELECT Batch.NumStud
FROM Batch
WHERE CourseID='$courseid'
INNER JOIN Course
ON Batch.CourseID=Course.CourseID"
an error that says mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>F:\AppServ\www\anNoECourse.php
is shown.This code was written to feed in data to a google chart.
You put SQL in wrong order (JOIN and WHERE are switched):
SELECT Batch.NumStud
FROM Batch INNER JOIN Course
ON Batch.CourseID = Course.CourseID
WHERE Course.CourseID = '$courseid'
It seems, that your query can be simplified (check your data):
select Batch.NumStud
from Batch
where Batch.CourseID = '$courseid'
I think the error is a bit more complex. Due to the fact that your SQL is invalid, you're not getting a result set. This case is not handled correctly by your PHP code!
So in addition to correcting your SQL as the others have suggested, please make sure to handle the case where you get no results or your query results in an error correctly in your PHP code!
The second part to your solution is as follows:
$result = mysql_query(...);
if ($result)
{
while (...)
...
}
This makes sure that mysql_query actually returned a result set and not false, which it does in case of errors (due to your invalid SQL code, but also in other cases). So just fixing your SQL is not enough to make your script error proof.
But again, do no longer use the mysql_.... functions! They are deprecated.

Magento CSV Import Images Missing

I edited my products by exporting them with the Import/Export Tool in magento, after I imported them I got faced with There was a problem with reindexing process, which I finally fixed.
But now all my images are missing and not displaying in the front end, can someone help me fix this issue?
/1/_/1_7_138.jpg is the format that the CSV images locations are
I also tried moving the images in media/category/product to media/import but still not displaying correctly
I also tried editing file permissions from 755 to 77 in the media folder but still nothing
I also ran this in the corresponding SQL database
INSERT INTO catalog_product_entity_media_gallery (attribute_id, entity_id, `value`)
SELECT ga.attribute_id, v.entity_id, v.value
FROM catalog_product_entity_varchar v
INNER JOIN eav_entity_type et ON et.entity_type_code=\'catalog_product\'
INNER JOIN eav_attribute va ON va.entity_type_id=et.entity_type_id AND
va.frontend_input=\'media_image\' AND va.attribute_id=v.attribute_id
INNER JOIN eav_attribute ga ON va.entity_type_id=et.entity_type_id AND
ga.attribute_code=\'media_gallery\'
LEFT JOIN catalog_product_entity_media_gallery g ON g.entity_id=v.entity_id AND
g.value=v.value
WHERE v.value<>\'no_selection\' AND v.value<>\'\' AND g.value IS NULL;
and got the following error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near '\'catalog_product\' INNER JOIN
eav_attribute va ON va.entity_ty
Not sure if you're having the same problem that I did, but when I re-imported products my images broke but the image was still associated with the product, just not set as the base, small, thumbnail, etc
I did something like
update catalog_product_entity_varchar AS v inner join catalog_product_entity_media_gallery AS g on v.entity_id = g.entity_id set v.value = g.value where (v.attribute_id = '85' or v.attribute_id = '86' or v.attribute_id = '87');
in my database to reassign them all.
Check following in
data base table catalog_product_entity_media_gallery is there an entry for image you are looking for.
something like 1 703 17 /1/_/1_7_138.jpg
Then go to media/catalog/product/1/_/ and check if this image file exists.
Additional flush your cache and check.
When using Import/Export, the folder structure in your media/import folder has to be the same as specified in your csv file. So if a media filename is specified as "/1/_/1_7_138.jpg" in your CSV, there must exist a file "/media/import/1/_/1_7_138.jpg" in your Magento root. Otherwise Import/Export won't find the media file.
The easiest solution would be, to not define a folder structure in your csv and to put all media files, that should be imported, directly in media/import/. So in your case:
CSV entry (e.g. in column 'thumbnail'): 1_7_138.jpg
File: /media/import/1_7_138.jpg
Unfortunately, Magento is not capable of importing its own exports! So an exported CSV has to be modified, to be importable in another Magento installation.
Not sure if you're still having issues, but to help with the INSERT query you listed, it's not working because the single quotes are escaped. I'm guessing you found this query in a post somewhere and when the query was posted, the single quotes were escaped with "\'".
If you change all of the \' to just single quotes like this:
INSERT INTO ......
INNER JOIN eav_entity_type et ON et.entity_type_code='catalog_product' <-- is where I changed the \' to just a single '. You'll need to do that with the entire query.
The query should then work. I also suggest as good practice, that you first run the select without the insert command to make sure it gives back good results before running an insert command blindly.

Mysql OR query with a join returns an out of memory error

I have a query that tries to tell is a member had participated in any of 3 types of actions. Each action is represented by a table that I am joining. And then I am doing an or to see if there is a match in any of the tables. Here is the query:
select problems.problem_id, problem_title from
problems left join attempted_solutions
on roblems.creator_member_id = attempted_solutions.suggester_id
left join problem_comments
on problems.creator_member_id = problem_comments.user_id
left join suggested_solutions
on problems.creator_member_id = suggested_solutions.suggester_id
where attempted_solutions.suggester_id = 1
or problem_comments.user_id = 1 or suggested_solutions.suggester_id = 1;
But for some reason this gives a "MySQL client ran out of memory error." if I try it command-line, and a "database connection interrupted" error if I do it from a script.
Any idea where I am going wrong here?
This thread has some solutions maybe you should try them see if any of them is your problem
http://forums.mysql.com/read.php?37,94800,232288#msg-232288
also, try to simplify your query then add your joins and where statements back one by one so that you can detect the exact cause of your problem

Get Redmine custom field value to a file

I'm trying to create a text file that contains the value of a custom field I added on redmine. I tried to get it from an SQL query in the create method of the project_controller.rb (at line 80 on redmine 1.2.0) as follows :
sql = Mysql.new('localhost','root','pass','bitnami_redmine')
rq = sql.query("SELECT value
FROM custom_values
INNER JOIN projects
ON custom_values.customized_id=projects.id
WHERE custom_values.custom_field_id=7
AND projects.name='#{#project.name}'")
rq.each_hash { |h|
File.open('pleasework.txt', 'w') { |myfile|
myfile.write(h['value'])
}
}
sql.close
This works fine if I test it in a separate file (with an existing project name instead of #project.name) so it may be a syntax issue but I can't find what it is. I'd also be glad to hear any other solution to get that value.
Thanks !
(there's a very similar post here but none of the solutions actually worked)
First, you could use Project.connection.query instead of your own Mysql instance. Second, I would try to log the SQL RAILS_DEFAULT_LOGGER.info "SELECT ..." and check if it's ok... And the third, I would use identifier instead of name.
I ended up simply using params["project"]["custom_field_values"]["x"] where x is the custom field's id. I still don't know why the sql query didn't work but well, this is much simpler and faster.

Hibernate Exception on MySQL Cross Join Query

I'm trying to perform a bulk delete of an object, Feature, which has a birdirectional ManyToOne relationship with another class, FeaturesMetadata. I'm having a SQLGrammerException thrown.
The hql I'm using:
String hql = "delete from Feature F where F.featuresMetadata.stateGeoId = :stateGeoId";
Turning on show SQL, the following is generated:
delete from FEATURE cross join FEATURESMETADATA featuresme1_ where STATEGEOID=?
Running the SQL directly in the db client gives this exception:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'cross join FEATURESMETADATA featuresme1_ where stategeoid='01'' at line 1
Since the generated SQL is throwing the Exception, I tried changing dialects from MySQL5InnoDBDialect to MySQLInnoDBDialect, but no change.
Can anyone assist?
You may not have joins in such a HQL query. Quote from the reference documentation:
No joins, either implicit or explicit, can be specified in a bulk HQL
query. Sub-queries can be used in the where-clause, where the
subqueries themselves may contain joins.
So I guess something like this should work:
delete from Feature F where F.id in
(select f2.id from Feature f2 where f2.featuresMetadata.stateGeoId = :stateGeoId)
I had the same issue and struggled to find a sensible answer. It seems that, even if you get this approach to work, the SQL generated is highly inefficient (according to what I have read).
So I took a step back and did the following:
List<Properties> props = propDao.findPropertiesByHotelCode(hotel.getCode());
propDao.deleteInBatch(props);
propDao.flush();
Basically rather tan trying to 'delete where', I'm doing a select where and then deleting in batch the set that I retrieved.
Hope this helps.
This is indeed rather poor from Hibernate. But you can solve it like this in a repo: (at least in PostgreSQL, not sure if this syntax should be modified for MySql)
#Modifying
#Query(nativeQuery = true, value = """
DELETE FROM feature f
USING features_metadata fd
WHERE f.features_metadata_id = fd.id AND fd.state_geo_id = :stateGeoId
""")
void deleteByStateGeoIdId(#Param("stateGeoId") UUID stateGeoId);