using a PHP variable in a MySQL IN batch query example

Note the ‘s

$product_list = “‘1869’, ‘1929’, ‘549’, ‘189’, ‘419’, ‘176’, ‘192’, ‘310’, ‘291’, ‘514’, ‘1643’, ‘1033’;

$db_name = “dbName”;
//conditional platform logic to support multiple connection scenarios

switch($_SERVER[‘HTTP_HOST’])
{
case ‘localhost’:
$db_host = “localhost”;
$db_username = “userName”;
$db_password = “Password”;
$dblink = mysql_connect ($db_host, $db_username, $db_password);
mysql_select_db($db_name, $dblink);
break;
case ‘www-staging.site.com’: break;
case ‘www.site.com’:
include(‘/var/www/includes/connection_script.php’);
break;
}

$result = mysql_query(”
SELECT
product_id,
product_name
FROM
tableNameHere
WHERE
product_id IN
(“.$product_list.“)
“);
if (!$result) { echo “Could not successfully run query ($sql) from DB: . mysql_error(); exit; }


//let’s break out the resource so that it can be singularly requested when needed

while($one = mysql_fetch_assoc($result))
{
$prod_id = $one[‘product_id‘];
$product_name[$prod_id] = $one[‘product_name‘];
}
@
mysql_close($dblink);

Comments are closed.