开发者

If statement to check if existing entry is in database

开发者 https://www.devze.com 2023-03-30 04:24 出处:网络
Trying to figure out why my code isn\'t working. Basically I have an elseif statment like so: mysql_connect(\"localhost\",\"xxxx\",\"xxxxx\");

Trying to figure out why my code isn't working. Basically I have an elseif statment like so:

mysql_connect("localhost","xxxx","xxxxx");
mysql_select_db("xxxxxx开发者_开发问答");
$sql = "SELECT COUNT(DATE) FROM calendar";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);

$checkdate = $row['DATE'];
$DATEFROM = $_POST['DATEFROM'];
$DAYCOUNT = $_POST['DAYCOUNT'];
$DAYS = $_POST['DAYS'];


if ( $DAYCOUNT < $DAYS ) { 
    header( 'Location: request_go_fail.php' );
}
else if ( $checkdate == $DATEFROM ) {
    echo "FAIL!";
}
else {

It doesn't work, the first check (to see if the DAYCOUNT is less than DAYS works fine, but when comparing to entries in the DB it doesn't seem to do it. Seems to be some issue with finding the already existing data, as when I change $checkdate to an entry that's already in the database it works great.

Any help is most appreciated :)


SELECT COUNT(DATE) FROM calendar doesn't return a field called date, print_r the $row variable to confirm that. Best solution is to change the statement to something like SELECT COUNT(DATE) AS datecount FROM calendar and then do $checkdate = $row['datecount'];

But while rereading your code fragment, I'm not sure that you really want the count of DATE's in the calendar table, and what exactly the intention is, is hard to determine from the code fragment.

Also, DATE is a reserved word in SQL, not the optimal choice for a column name!


Did you try printing $checkdate? I suspect it's null if that is indeed the SQL you're using.

Should be $row['COUNT(DATE)'] I believe, or you can use mysql_fetch_array and $row[0] instead, or use an AS in your SQL or

$checkdate = mysql_result($result, 0);

And skip the fetch call all together.

COUNT(DATE) will return the number of non-null DATE fields in your DB btw, is that really what you want?


You don't have a DATE key in the $row variable because of the sql command. Use this instead, it's called Alias:

SELECT COUNT(DATE) AS DATE_COUNT FROM calendar

Now you have a key DATE_COUNT which will contains value.

$checkdate = $row['DATE_COUNT'];
0

精彩评论

暂无评论...
验证码 换一张
取 消