Jump to content

Need mysql qurey help...


thara

Recommended Posts

Hello everyone...

 

I use this mysql query in my php script.

 

	$q = "SELECT introduction_note, institute_code,
		DATE_FORMAT(registration_date, '%Y-%m-%d') AS date, image_name
		FROM institutes 
		INNER JOIN institute_images ON institutes.institute_id = institute_images.institute_id
		WHERE institute_name = \"$institute\" AND institute_images.institute_id = $instituteId AND
		institute_images.image_type = \"slogan\"
		LIMIT 1";

 

Its work fine if it has an image in institute_images table with its image_type is slogan. My problem is when I run my script at first time there is no such an image in my institute_images table. In this case there is no content in my webpage. but when I add an image with its type in slogan to institute_images table every things display in my page properly.

 

can anybody tell me how can I figure out this query when it is run at first time. This mean I need to display my page even there is not such image in my table.

 

any comments are greatly appreciated.

 

thank you.

Link to comment
Share on other sites

Try this

SELECT introduction_note, institute_code,
DATE_FORMAT(registration_date, '%Y-%m-%d') AS date, image_name
FROM institutes 

LEFT JOIN institute_images 
ON institutes.institute_id = institute_images.institute_id
AND institute_images.image_type = 'slogan'

WHERE institute_images.institute_id = $instituteId 
LIMIT 1

If you have the ID, why are you searching on the name too>

Link to comment
Share on other sites

SELECT introduction_note, institute_code,
DATE_FORMAT(registration_date, '%Y-%m-%d') AS date, image_name
FROM institutes 

LEFT JOIN institute_images 
ON institutes.institute_id = institute_images.institute_id
AND institute_images.image_type = 'slogan'

WHERE institutes.institute_id = $instituteId 
LIMIT 1

 

 

Sorry, the whole query is kind of written backwards. The last line should refer to institutes not images.

 

When you write the joins, write everything for the first table and make sure it works. The only stuff in the WHERE clause should be for the main table. anything else should be in the join clauses.

Link to comment
Share on other sites

Thank you.. Now its working for me.

 

Now I need to check 'image_name' has a value or not. if it has a value need to display that image in a DIV. if there is no value I dont need to display image DIV. So can you tell me how can I check there is a value or not????

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.