MySQL query

Neil_Bradley
Neil_Bradley used Ask the Experts™
on
I have written a query and code to interpret the response however I must be missing something as my result always comes up and being empty and I know it is not. Can you look over my code with a fresh pair of eyes and spot where I am going wrong?
Regards,
N

echo "<p>Please choose a category:</p>";
  ///set the variable
     $masterCat = "clothing";
    // make the function
	  function masterCat($catid) {
   // query database for the books in a category
   $conn = db_connect();
   $query = "select * from catagories where masterCat = '".$masterCat."'";
   $result = @$conn->query($query);
   if (!$result) {
     return false;
   }
   $num_items = @$result->num_rows;
   if ($num_items == 0) {
      return false;
   }
   $result = db_result_to_array($result);
   return $result;
}

  $masterCat_array = masterCat($masterCat);

  //display all books in the array passed in
  if (!is_array($masterCat)) {
    echo "<p>No products currently available in this category</p>";
  } else {
    //create table
    echo "<table class=\"tclass\" width=\"100%\" border=\"0\">";

    //create a table row for each book
    foreach ($catname as $row) {
      $url = "show_product.php?isbn=".$row['isbn'];
      echo "<tr><td>";
      if (@file_exists("images/".$row['isbn'].".jpg")) {
        $title = "<img src=\"images/".$row['isbn'].".jpg\"
                  style=\"border: 1px solid black\"/>";
        do_html_url($url, $title);
      } else {
        echo "&nbsp;";
      }
      echo "</td><td>";
      $title = $row['title'];
      do_html_url($url, $title);
      echo "</td></tr>";
    }

    echo "</table>";
  }

  echo "";

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Maybe like this:
"select * from catagories where masterCat = '" & $masterCat & "'";
Neil_BradleyWeb UX/UI Developer

Author

Commented:
No, doesnt fix it.  The vaiiarble "clothing" was chose to thest the system as it is definatly there.
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
I don't find 'db_result_to_array' in PHP.  Could you mean 'mysql_fetch_array' or 'mssql_fetch_array'?  You put MySQL in your title and MSSQL as one of your zones.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Try:
"select * from catagories where masterCat = '$masterCat'";

i.e. $masterCat surrounded by single quotes and entire query overall by double quotes.

PHP Variable in mySQL query
http://www.daniweb.com/forums/thread118055.html
MySQL :: how to use PHP variable in MYSQL query?
http://forums.mysql.com/read.php?52,1237823702,older
http://lists.mysql.com/php/90
Pass a php variable to mysql query
http://stackoverflow.com/questions/1999458/pass-a-php-variable-to-mysql-query
PHP MySQL by examples
http://www.roscripts.com/PHP_MySQL_by_examples-193.html

Commented:
My bad:
"select * from catagories where masterCat = '" & $masterCat & "';"


After the ampresand it should have been:
double, single, semi, double
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
Is 'catagories' the correct spelling for your table name?  categories?
Neil_BradleyWeb UX/UI Developer

Author

Commented:
Thats got it.. Brilliant thanks
N

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial