Jump to content

PHP Mysql Query


chrispulliam

Recommended Posts

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%')"

Link to comment
Share on other sites

Here is my 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');
?>
<!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=iso-8859-1" />
<title>
user Page</title>
<link href="../loginmodule.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Welcome <?php echo $_SESSION['SESS_NAME'];?></h1>
<p><a href="users">Home</a> | <a href="admin">Admin</a> | <a href="logout.php">Logout </a>
<form action="reports.php" method="post">
  <? 
$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 "<select name=program_name value=''></option>";
// printing the list box select command
while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[Title]>$nt[Title]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box 
?></p>
  <p><br />
    <input type="submit" name="submit" value="Submit" />
  </p>
</form>
<p> </p>
<p> 
Records <?php echo ($startRow_processing + 1) ?> to <?php echo min($startRow_processing + $maxRows_processing, $totalRows_processing) ?> of <?php echo $totalRows_processing ?>
</p>
<table border="1" align="center">
  <tr>
    <td>media_number</td>
    <td>date_entering</td>
    <td>program_name</td>
    <td>program_num</td>
    <td>comments</td>
  </tr>
  <?php do { ?>
    <tr>
      <td><a href="users/detail.php?media_number=<?php echo $row_processing['media_number']; ?>"> <?php echo $row_processing['media_number']; ?>  </a></td>
      <td><?php echo $row_processing['date_entering']; ?>  </td>
      <td><?php echo $row_processing['program_name']; ?>  </td>
      <td><?php echo $row_processing['program_num']; ?>  </td>
      <td><?php echo $row_processing['comments']; ?>  </td>
    </tr>
    <?php } while ($row_processing = mysql_fetch_assoc($processing)); ?>
</table>
<br />
</body>
</html>
<?php
mysql_free_result($processing);
?>

 

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.