PHP Mysql Query

I have the following two statements in php that I am trying to pass to my database. If I perform a search for Test One with the first statement It displays all entries that have Test One as well as Test Two Test Three and so forth. I then try statement 2 and it does not show me any results. If I Hard code Test One in the search succeeds successfully. What am I doing wrong?

Statement 1

“SELECT * FROM media WHERE (program_name LIKE ‘%$program_name%’)”

Statement 2

“SELECT * FROM media WHERE (program_name = ‘%$program_name%’)”

Here is my code

[code]<?php require_once('../Connections/umdb.php'); ?>

<?php require("../Connections/grab_globals.inc.php3") ?> <?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; } } $currentPage = $_SERVER["PHP_SELF"]; $maxRows_processing = 10000000; $pageNum_processing = 0; if (isset($_GET['pageNum_processing'])) { $pageNum_processing = $_GET['pageNum_processing']; } $startRow_processing = $pageNum_processing * $maxRows_processing; $program_name = $_POST['program_name']; $colname_processing = "-1"; if (isset($_GET['No'])) { $colname_processing = $_GET['No']; } mysql_select_db($database_umdb, $umdb); $query_processing = "SELECT * FROM media WHERE (program_name LIKE '%$program_name%')"or die(mysql_error()); $query_limit_processing = sprintf("%s LIMIT %d, %d", $query_processing, $startRow_processing, $maxRows_processing); $processing = mysql_query($query_limit_processing, $umdb) or die(mysql_error()); $row_processing = mysql_fetch_assoc($processing); if (isset($_GET['totalRows_processing'])) { $totalRows_processing = $_GET['totalRows_processing']; } else { $all_processing = mysql_query($query_processing); $totalRows_processing = mysql_num_rows($all_processing); } $totalPages_processing = ceil($totalRows_processing/$maxRows_processing)-1; $queryString_processing = ""; if (!empty($_SERVER['QUERY_STRING'])) { $params = explode("&", $_SERVER['QUERY_STRING']); $newParams = array(); foreach ($params as $param) { if (stristr($param, "pageNum_processing") == false && stristr($param, "totalRows_processing") == false) { array_push($newParams, $param); } } if (count($newParams) != 0) { $queryString_processing = "&" . htmlentities(implode("&", $newParams)); } } $queryString_processing = sprintf("&totalRows_processing=%d%s", $totalRows_processing, $queryString_processing); ?> <?php require_once('../auth.php'); ?> user Page

Welcome <?php echo $_SESSION['SESS_NAME'];?>

Home | Admin | Logout <? $query="SELECT * FROM programs"; /* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */ $result = mysql_query ($query); echo ""; // printing the list box select command while($nt=mysql_fetch_array($result)){//Array or records stored in $nt echo "$nt[Title]"; /* Option values are added by looping through the array */ } echo "";// Closing of list box ?>


 

  Records <?php echo ($startRow_processing + 1) ?> to <?php echo min($startRow_processing + $maxRows_processing, $totalRows_processing) ?> of <?php echo $totalRows_processing ?>

<?php do { ?> <?php } while ($row_processing = mysql_fetch_assoc($processing)); ?>
media_number date_entering program_name program_num comments
<?php echo $row_processing['media_number']; ?>  <?php echo $row_processing['date_entering']; ?>  <?php echo $row_processing['program_name']; ?>  <?php echo $row_processing['program_num']; ?>  <?php echo $row_processing['comments']; ?> 

<?php mysql_free_result($processing); ?>[/code]

Why is your WHERE statement in brackets?

“SELECT * FROM media WHERE program_name LIKE ‘%$program_name%’”

“SELECT * FROM media WHERE program_name = ‘%$program_name%’”

Are you getting any errors at all?

There is also the small chance your web-host doesn’t allow the short php opening tags (<?) and only allows long (<?php) As such it’s usually better to always use the long one. I found this in your code:
[php]

<? $query="SELECT * FROM programs"; /* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */ $result = mysql_query ($query); echo ""; // printing the list box select command while($nt=mysql_fetch_array($result)){//Array or records stored in $nt echo "$nt[Title]"; /* Option values are added by looping through the array */ } echo "";// Closing of list box ?>

[/php]
Change that opening tag to the long version see if it makes a difference. The select tag also shouldn’t have a “value=” tag.

And I can’t see your second query statement anywhere in that code.

Statement 2 is what I am trying to get to work. I am able to get the LIKE statement to work If I do a search for “Test” it will work fine but if I do a search for “Test One” with the LIKE statement it displays all results that have “Test” “Test One” and “Test Two” in them. That is where I came up with the = Statement. I have tried a number of combinations and none of them seem to work.

I also removed the brackets and add the long code for the PHP(I own the server that it is hosted on) and this does not seem to work either.

Just for grins and giggles I echoed back what was being displayed to the $program_name varable and this is only showing one word not all words that are in my drop down menu.

[php]$program_name = $_POST[‘program_name’];
echo “$program_name”; [/php]

So maybe my problem lies with the drop down?

[php] <?
$query=“SELECT * FROM programs”;
/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order /
$result = mysql_query ($query);
echo “”;
// printing the list box select command
while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo “$nt[Title]”;
/
Option values are added by looping through the array */
}
echo “”;// Closing of list box
?>[/php]

Here was my issue! thanks guys for your help
[php]echo “$nt[Title]”;[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service