Solved

MySQL Query Error

Posted on 2012-03-13
2
337 Views
Last Modified: 2012-03-13
So I'm having an issue with the query below... it was created by my app developer but she is on maternity leave and is unreachable at the moment. This mysql query is linked to two tables for an online shopping site along with several fields. It was originally written to randomly pull product ids and post 4 of them into an image slider (through jquery and css) i believe. What I am trying to accomplish is to alter the code in order to specify which products i want to include in the slide show. I have been trolling forums and trying different things but they all seem to lead to an error... if someone could perhaps give me an idea of what I would need to change to specify the products that are being chosen that would be great. Thanks! ...an example of the random pull is written below:

global $db;


//first product
    $sliderid1 = 1;
    $sql = "
SELECT   products_image, products_price, products_description, products_name, " . TABLE_PRODUCTS . ".products_id
FROM " . TABLE_PRODUCTS . ", " . TABLE_PRODUCTS_DESCRIPTION . "
WHERE " . TABLE_PRODUCTS . ".products_id = " . TABLE_PRODUCTS_DESCRIPTION . ".products_id
ORDER BY RAND()
LIMIT 0 , 30";

$sql = $db->bindVars($sql, ':sliderid1:', $sliderid1, 'integer');
$result1 = $db->Execute($sql);
0
Comment
Question by:JBober14
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 14

Accepted Solution

by:
Scott Madeira earned 500 total points
ID: 37716498
Something like this may work for you.  It is based on you knowing the product IDs you want to include.  If you get errors you will want to post the error message.  Also, you will want to run the actual SQL commands in phpMyAdmin to make sure the query does what you want.  After you have a query that generates the right output you can convert it into PHP.

the key line in the SQL is this:  AND " . TABLE_PRODUCTS . ".products_id in ". $id ;

Which limits the results to the list of product IDs (contained in the $id variable.)


		// Example product IDs. add as many as you want
		$pidarr[0] = 14;
		$pidarr[1] = 29;

		// Use the IDs to build part of the WHERE clause
		$ids = "(";
		foreach ($pidarr as $value){
			$ids .= $value . ", ";
		}
		
		// Trim the final ", " from the string
		$id	= substr($ids, 0, strlen($ids)-2). ")";
	
    $sliderid1 = 1;
    $sql = "
SELECT   
	products_image, 
	products_price, 
	products_description, 
	products_name, " . 
	TABLE_PRODUCTS . ".products_id
FROM " 
	. TABLE_PRODUCTS . ", " 
	. TABLE_PRODUCTS_DESCRIPTION . "
WHERE " 
	. TABLE_PRODUCTS . ".products_id = " . TABLE_PRODUCTS_DESCRIPTION . ".products_id
	AND " . TABLE_PRODUCTS . ".products_id in ". $id ;

$sql = $db->bindVars($sql, ':sliderid1:', $sliderid1, 'integer');
$result1 = $db->Execute($sql);

Open in new window

0
 

Author Closing Comment

by:JBober14
ID: 37716871
This worked flawlessly! I'm still trying to dissect what you did as I am a new-comer to php and mysql but nonetheless i'd like to thank you for the quick response and accuracy first time around!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
bootstrap wrap text 1 30
alert on input text 2 23
Position Absolute Goes Beyond Parent 11 20
Put POST values into cookies. 14 31
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question