Include php-file depending on rows or no rows as result?

0 views
Skip to first unread message

DanH

unread,
Jun 30, 2009, 2:06:37 PM6/30/09
to Professional PHP Developers
I check in the database with the following statement which works:

SELECT * FROM `database` WHERE numeric_id LIKE "58473" AND
timestamp_insert <= (NOW() - INTERVAL 1 DAY);

So this will get me or a) nothing (no user with numeric_id or a user
with that numeric_id but not valid license anymore), or

b) one or several rows (several if user has subscribed to multiple
websites during the last day).

My question: how can i use the above to give access to a client; the
client will go to index.php, the above statement checks if the client
is indeed still a valid member and includes/echo's fail.php (text:
"sorry your license is invalid") or downloads.php (which shows several
download links).

Please help!! :) How can i do this with as simple code as possible?
Just an if-statement? Thanx!!

Robert Gonzalez

unread,
Jun 30, 2009, 2:41:44 PM6/30/09
to Professi...@googlegroups.com
After you run the query, do something like this...

<?php
$result = mysql_query($sql) or die('Couldn\'t handle it: ' . mysql_error()); // $sql here is your query

// Handle your conditional
if (mysql_num_rows($result)) {
  // There are rows, include the file for having rows
} else {
  // There are no rows, include the file for having no rows
}
?>

This is very stripped down logic, but it is a starting point for you.
--
Robert A. Gonzalez

Bobby Easland

unread,
Jun 30, 2009, 8:25:20 PM6/30/09
to Professi...@googlegroups.com
Dan,

Break up your decision branch into more manageable chunks and make it
into a mini-controller. Try something like this:

<?php
try {
if ( false === ($resource = mysql_query($sql)) ){
throw new RuntimeException('0:Query failed: ' . mysql_error());
}

if ( !is_resource($resource) || !mysql_num_rows($resource) ){
throw new RuntimeException('1:No rows returned');
}

} catch ( RuntimeException $e ) {
$code = array_pop(explode(':', $e->getMessage()));

switch((int)$code){
case 0:
header('HTTP/1.1 503 Service Unavailable', true, 503);
exit;
break;
case 1:
header('Location: http://www.domain.com/invalid.html');
exit;
break;
default:
header('Location: http://www.domain.com/error.html');
exit;
break;
} # end switch
} # end try/catch
?>

--
Bobby Easland
http://www.oscommerce-freelancers.com

DanH

unread,
Jul 1, 2009, 11:15:55 AM7/1/09
to Professional PHP Developers
Bobby,

Thanx for your detailed reply.
You might have missed my email send to your gmail account so i'll
mention it like this (regarding freelancing).

Thanx,
Dan
> Bobby Easlandhttp://www.oscommerce-freelancers.com
Reply all
Reply to author
Forward
0 new messages