URL variables in PHP and SQL
Have what is likely a pretty basic problem, but those are always the hardest ones, where the solution's staring you in the face....
I have a query in a php mysql database that works perfectly if i have it with a hardcoded query like "category=11". But I'm trying to make it so that a passed url variable defines the category. I've implemented the url variable and confirmed the value's being passed (by having an echo statement to show if it worked or not), and then i set up a sprintf statement to take that value and insert it into the SQL statement. Only problem, it comes back saying the query is empty.
I suspect the problem has something to do with variable types string vs integer or something like that, but i can't figure it out. I think my biggest problem is not knowing enough about the debugging tools to be able to test things. I'm used to dev environments where there's an "immediate" window so I can watch data as it's being manipulated. any suggestions on how to track this issue down? here's the code. I stripped it down so it's just a white page with the query. Here's the code, with the most relevant parts highlighted in bold red.
------------------------------------------------------------------------------------------------------ code ------------------------------------------------------------------
<?php require_once('Connections/connection1.php'); ?>
<?php echo $_GET['CT']; ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$trr=$CT; <--- added this so i could switch between hard coded value and url variable easily.
mysql_select_db($database_connection1, $connection1);
$query_R1 = sprintf("SELECT CatalogItems.ID, CatalogItems.NM, CatalogItems.DS, CatalogItems.FS, CatalogItems.TH, CatalogItems.P FROM CatalogItems WHERE CatalogItems.Category1 = %s OR CatalogItems.Category2 = %s OR CatalogItems.Category3 = %s",$trr);
$R1 = mysql_query($query_R1, $connection1) or die(mysql_error());
$row_R1 = mysql_fetch_assoc($R1);
$totalRows_R1 = mysql_num_rows($R1);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<?php echo $row_R1['ID']; ?>
</body>
</html>
<?php
mysql_free_result($R1);
?>
------------------------------------------------------------------------------------------------------------------
