PHP SQL QUERY

I am making an SQL query such as

[php]
$SQLCat = "SELECT * from CATEGORIES WHERE ID = ‘$row[CATEGORY]’ AND STATUS = ‘0’;
$resultCat = @mysql_query( $SQLCat ); $rowCat = @mysql_fetch_array( $resultCat );
[/php]

and it is completely ignoring AND STATUS = 0 Is there something wrong with my syntax? Thanks

you sql query has some syntax errors…

$SQLCat = "SELECT * from CATEGORIES WHERE ID = '$row[CATEGORY]' AND STATUS = '0'; $resultCat = @mysql_query( $SQLCat ); $rowCat = @mysql_fetch_array( $resultCat ); ?>

it should be something like this…

$SQLCat = "SELECT * from CATEGORIES WHERE ID =".$row['CATEGORY']." AND STATUS = '0'"; $resultCat = @mysql_query( $SQLCat ); $rowCat = @mysql_fetch_array( $resultCat ); ?>

Hope u got it…

I changed it to that, I think I tried that first. It still ignores status. I have double checked the USERS table and STATUS in it has a value of 1 on the listing Im trying to exclude. Here is the full code

[php]

<?php include './include/mysql.php'; mysql_connect("$host", "$user", "$pass") or die(mysql_error()); mysql_select_db("$database") or die(mysql_error()); if ($_GET[search] == 1) $qstring = "WHERE NAME LIKE '%$_POST[search]%'"; $setpage = 'add_user'; $return = 'man_user'; $table = "USERS"; if (empty($qstring)) $qstring = 'ORDER BY ID DESC LIMIT 20'; $qstring = "$qstring"; ?> <?php $SQL = "SELECT * from $table $qstring"; $result = @mysql_query( $SQL ); while( $row = @mysql_fetch_array( $result ) ) { $check = 1; $SQLCat = "SELECT * from CATEGORIES WHERE ID =".$row['CATEGORY']." AND STATUS = '0'"; $resultCat = @mysql_query( $SQLCat ); $rowCat = @mysql_fetch_array( $resultCat ); ?>
<?php } ?>
Last 20 Added | Listing Products | Add Product
<?php echo "$row[NAME]"; ?> "> Edit this listing "> Delete this listing
<?PHP if (empty($check)) { ?>
No products found.
<?PHP } ?> [/php]

Try taking out all of the @'s - This suppresses errors and isn’t recommended for use during development.

Ok, I removed the @ and at least now it is giving me an error. Heres what I got

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/bizcards/public_html/sltrinkets.com/Projects/securedwnld/man_user.php on line 24

Heres the code

[php]

<?php include './include/mysql.php'; mysql_connect("$host", "$user", "$pass") or die(mysql_error()); mysql_select_db("$database") or die(mysql_error()); if ($_GET[search] == 1) $qstring = "WHERE NAME LIKE '%$_POST[search]%'"; $setpage = 'add_user'; $return = 'man_user'; $table = "USERS"; if (empty($qstring)) $qstring = 'ORDER BY ID DESC LIMIT 20'; $qstring = "$qstring"; ?> <?php $SQL = "SELECT * from $table $qstring"; $result = mysql_query( $SQL ); while( $row = mysql_fetch_array( $result ) ) { $check = 1; $SQLCat = "SELECT * from CATEGORIES WHERE ID =".$row['CATEGORY']." AND STATUS = '0'"; $resultCat = mysql_query( $SQLCat ); $rowCat = mysql_fetch_array( $resultCat ); ?>
<?php } ?>
Last 20 Added | Listing Products | Add Product
<?php echo "$row[NAME]"; ?> "> Edit this listing "> Delete this listing
<?PHP if (empty($check)) { ?>
No products found.
<?PHP } ?> [/php]

I figure its probably syntax but everything looks right.

  1. please chech ur spelling (lower case vs. upper case). eg if the field is u can’t use $_POST[‘FIELD’]. as well u cann’t use $row[‘CATEGORY’] if the field was called category. i suggest not to use uppercase at all, or just as wordseperators (e.g. my_field or myField, but never MYFIELD nor Myfield)

  2. please add error_repoting(E_ALL), that should make u see the mysql-error. or use: [php]$resultCat = mysql_query( $SQLCat ) or die(mysql_error());[/php]

Ok, I fixed all the errors that brought up, and did away with 1 end of the query, Basically I want it to list all records where STATUS = 0 it is currently listing 0’s and 1’s Code was changed so here is the new code, Spelling and case has been verified but Ill post part of the SQL here too

I have disabled the line

[php]

<?php error_reporting(E_ALL); include './include/mysql.php'; mysql_connect("$host", "$user", "$pass") or die(mysql_error()); mysql_select_db("$database") or die(mysql_error()); $setpage = 'add_user'; $return = 'man_user'; $table = "USERS"; if (empty($qstring)) $qstring = 'ORDER BY ID DESC LIMIT 20'; $qstring = "$qstring"; ?> <?php $SQL = "SELECT * from $table $qstring"; $result = mysql_query( $SQL ); while( $row = mysql_fetch_array( $result )) { $check = 1; $SQLSTA = "SELECT * from USERS WHERE STATUS = '0'"; $resultSTA = mysql_query( $SQLSTA ) or die(mysql_error()); $rowSTA = mysql_fetch_array($resultSTA); ?>
<?php } ?>
Last 20 Added | Listing Products | Add Product
<?php echo "$row[NAME]"; ?> "> Edit this listing "> Delete this listing
<?PHP if (empty($check)) { ?>
No products found.
<?PHP } ?> [/php]

SQL

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

CREATE TABLE `USERS` (
  `ID` int(11) NOT NULL auto_increment,
  `NAME` varchar(255) NOT NULL default '',
  `PASSWORD` varchar(255) NOT NULL default '',
  `EMAIL` varchar(255) NOT NULL default '',
  `STATUS` varchar(255) NOT NULL default '0',
  PRIMARY KEY  (`ID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

Just occured to me, will the varchar(255) have anything to do with it?

i thing i’m seen ur prob:

u use a query selecting all rows:
[php]$SQL = “SELECT * from $table $qstring”;
$result = mysql_query( $SQL );
while( $row = mysql_fetch_array( $result ))
{
…[/php]
and then u echo out all rows ($row)

the second query u put i there, is never uses:
[php]…
$check = 1;
$SQLSTA = “SELECT * from USERS WHERE STATUS = ‘0’”;
$resultSTA = mysql_query( $SQLSTA ) or die(mysql_error());
$rowSTA = mysql_fetch_array($resultSTA);
…[/php]
u nighther have a while loop nor is $rowSTA used ever again

u have to use just one query and loop:
[php]$check = 1;
$SQL = “SELECT * from USERS WHERE STATUS = ‘0’”;
$result = mysql_query( $SQL ) or die(mysql_error());
while( $row = mysql_fetch_array($result))
{
…[/php]

That was it, Q you should be nominated for PHP helper of the year :) Thanks to all who replied

Sponsor our Newsletter | Privacy Policy | Terms of Service