Link to home
Start Free TrialLog in
Avatar of chipsterva69
chipsterva69

asked on

using PHP and MySQL to build a web query

I have been staring at this for far too long to admit, but I am stumped.  I am a newbie to PHP and MySQL but have a relatively good grasp on SQL, so I would like to think I know what I am doing here.  Unfortunately, results prove different.

I have built a simple form to submit information into a very simple MySQL dbase - will expand it later when the client firms up the requirements.  The submission part works.

Now to pull the results.  I can get a simple 'SELECT * FROM table' to work, but when I try to insert a WHERE parameter, I get a syntax error.

Here is the code on the page:

<!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>Dynamic Menu</title>
</head>

<body>

<?
// Make a MySQL Connection
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

// Retrieve all the data from the "example" table
$result = mysql_query("SELECT * FROM test1 WHERE show = '1'")
or die(mysql_error());  

// store the record of the "example" table into $row
$row = mysql_fetch_array( $result );
// Print out the contents of the entry

while($row = mysql_fetch_assoc($result))
{
    echo "Title :{$row['title']} <br>" .
         "Description : {$row['description']} <br>" .
         "Price : {$row['price']} <br><br>";
}

?>



</body>
</html>

Like I said - this works fine until I add the 'WHERE show = '1'.

My thinking is the show field in the dbase table is a bit flag - true or false - that indicates if a particular entry should be visible or not.  I have tried setting the field type to tinyint (1 or 0), varchar (1 or 0), or boolean.  I have tried permutations of 'show is 1', 'show is true', 'show > 0' and 'show = '1' '.

I hate to say how much time I have spent on this one particular piece - it is killing me.  As near as I can tell, the PHP/MYSQL dbase does not have bit value, so should I be using tinyint?  VARCHAR?  What is the syntax to successfully query against it?

Thanks in advance!
ASKER CERTIFIED SOLUTION
Avatar of jayarjo
jayarjo
Flag of Georgia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
our filed = your fields :D

damn typos...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chipsterva69
chipsterva69

ASKER

okay - the error is gone!  thanks a million!

follow-up though - when I run the query against '0' in the now-named isVisible field, it shows everything listed with the 0 value.  I use '1' and the one item with that value does not show up.  is this a related problem?  The isVisible field is a tinyint, with values of 1 or 0.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
kruger_monkey - that sounds like the problem I just came followed up with - an expected return of one item isn't showing.  How do I avoid the problem?
Drop the first $row= and only use the while $row option.

Caught me out a few times.  

$row = mysql_fetch_association($query)  This line will take the first row and assign it here, so drop this.

Only use

while($row......) you will get the first line back again. Basically the following should work, I believe.

// store the record of the "example" table into $row
//$row = mysql_fetch_array( $result );  -- either comment out or remove this line.
// Print out the contents of the entry

while($row = mysql_fetch_assoc($result))
{
    echo "Title :{$row['title']} <br>" .
         "Description : {$row['description']} <br>" .
         "Price : {$row['price']} <br><br>";
}

kruger_monkey - thanks!  that is just what I did.  works like a charm.  thanks a million for the help!