if negative result in sql search how to express?
If you run a sql search and the result is negative, meaning the word isn't found in the database what should I write.
PHP Code:
$sql = "SELECT * FROM [I]TABLENAME[/I] WHERE this='".$this."' or this='".$that."' ";
$result = mysql_query($sql) OR exit( 'Error: ' . mysql_error() );
while($row = mysql_fetch_array($result)){}
How can I put an if/else statement?
if it finds $this but doesn't find $that?
Re: if negative result in sql search how to express?
some sort of if statement figuring out what the value of this is?
PHP Code:
if($row['this'] == $this){
echo 'this';
}elseif($row['this'] == $that){
echo 'that';
}
or, perhaps even better (at least structurally, especially for many values):
PHP Code:
switch($row['this']){
case $this:
echo 'this';
break;
case $that:
echo 'that';
break;
}
Re: if negative result in sql search how to express?
Quote:
Originally Posted by
kows
some sort of if statement figuring out what the value of
this is?
PHP Code:
if($row['this'] == $this){
echo 'this';
}elseif($row['this'] == $that){
echo 'that';
}
or, perhaps even better (at least structurally, especially for many values):
PHP Code:
switch($row['this']){
case $this:
echo 'this';
break;
case $that:
echo 'that';
break;
}
if the email is found in the db then echo "login" else "register".
Re: if negative result in sql search how to express?
PHP Code:
for($i=0; $i<count($emails); $i++){
while($row = mysql_fetch_array($result)){
if($emails[$i]==$row["email"]){
$message .="<a href=\"member.php\">".$row["username"]."</a>";
}else{
$message .="<a href=\"http://".$_SERVER['HTTP_HOST']."/thewheelofgod/twotexts/registration.php\">Register</a>";
}
}
}
Doesn't work. If the email doesn't match any in the db it should register If it does it should be $username.
Re: if negative result in sql search how to express?
code out of context means nothing. you have to post enough for someone to actually see what you're doing.
just the fact that you're looping through your entire query result in every iteration of some for() loop means that you're doing something terribly wrong. post the rest of the code.
Re: if negative result in sql search how to express?
Quote:
Originally Posted by
kows
code out of context means nothing. you have to post enough for someone to actually see what you're doing.
just the fact that you're looping through your entire query result in every iteration of some for() loop means that you're doing something terribly wrong. post the rest of the code.
PHP Code:
<?php
//$time = set_time_limit(30);
require_once("includefiles/dbconnection.php");
require_once("logininfo.php");
$from = $_GET['youremail'];
$emails = explode(", ", $_GET['theiremail']);
$subject = $_GET['subject'];
$sql="SELECT * FROM $dbTable WHERE";
$to="";
$num=count($emails)-1;
//echo $num;
for($i=0; $i<count($emails); $i++){
if($i<$num){
$sql.=" email='".$emails[$i]."' OR ";
$to .= $emails[$i].", ";
}else{
$sql.=" email='".$emails[$i]."'";
$to .= $emails[$i];
}
}
if($group == "Administrator"){
echo $sql;
}
$errors = array();
?>
<!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>
<?php //echo "<meta http-equiv='Refresh' content='".$time."; url=".$continue."' />";
//echo $time;
?>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?php echo $subject; ?></title>
</head>
<body>
<?php
// Remove $_COOKIE elements from $_GET.
if(count($_COOKIE)){
foreach(array_keys($_COOKIE) as $value){
unset($_GET[$value]);
}
}
// Validate email field.
if(isset($_GET['email']) && !empty($_GET['email'])){
$_GET['email'] = trim($_GET['email']);
if(substr_count($_GET['email'],"@") != 1 || stristr($_GET['email']," ")){
$errors[] = "Email address is invalid";
}else{
$exploded_email = explode("@",$_GET['email']);
if(empty($exploded_email[0]) || strlen($exploded_email[0]) > 64 || empty($exploded_email[1])){
$errors[] = "Email address is invalid";
}else{
if(substr_count($exploded_email[1],".") == 0){
$errors[] = "Email address is invalid";
}else{
$exploded_domain = explode(".",$exploded_email[1]);
if(in_array("",$exploded_domain)){
$errors[] = "Email address is invalid";
}else{
foreach($exploded_domain as $value){
if(strlen($value) > 63 || !preg_match('/^[a-z0-9-]+$/i',$value)){
$errors[] = "Email address is invalid"; break;
}
}
}
}
}
}
}
/*
// Check referrer is from same site.
if(!(isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']) && stristr($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST']))){
$errors[] = "You must enable referrer logging to use the form";
}
*/
// Check for a blank form.
function recursive_array_check_blank($element_value){
global $set;
if(!is_array($element_value)){
if(!empty($element_value)){
$set = 1;
}
}else{
foreach($element_value as $value){
if($set){
break;
} recursive_array_check_blank($value);
}
}
}
recursive_array_check_blank($_GET);
if(!$set){
$errors[] = "You cannot send a blank form";
}
unset($set);
// Display any errors and exit if errors exist.
if(count($errors)){
foreach($errors as $value){
print "$value<br>";
}
exit;
}
if(!defined("PHP_EOL")){
define("PHP_EOL", strtoupper(substr(PHP_OS,0,3) == "WIN") ? "\r\n" : "\n");
}
// Build message.
function build_message($request_input){
if(!isset($message_output)){
$message_output ="";
}
if(!is_array($request_input)){
$message_output = $request_input;
}else{
foreach($request_input as $key => $value){
if(!empty($value)){
if(!is_numeric($key)){
$message_output .= str_replace("_"," ",ucfirst($key)).": ".build_message($value).PHP_EOL.PHP_EOL;
}else{
$message_output .= build_message($value).", ";
}
}
}
}
return rtrim($message_output,", ");
}
$html_begin = "<html>
<head>
<title>".$subject."</title>
</head>
<body>
<table style=\"border: 1px solid #7A1010;background-image:url(images/ripples.jpg);\">
<tr>
<td>
<a href=\"http://".$_SERVER['HTTP_HOST']."/thewheelofgod/twotexts/\">
<img src=\"http://".$_SERVER['HTTP_HOST']."/thewheelofgod/twotexts/images/wheeltitle.jpg\" style=\"border: none;\" />
</a>
</td>
</tr>";//$_SERVER['REQUEST_URI'].
$html_end = "</table>
</body>
</html>";
$message = $html_begin."<tr>
<td>".$_GET['passage']."</td>
</tr>
<tr>
<td>
<table style=\"background-color: #EAE8C8; width: 100%;\">
<tr>
<td><span style=\"font-weight: bold;\">".$_SESSION['username']."</span> says: </td>
</tr>
<tr>
<td style=\"background-color: #EAE8C8; \">".$_GET['comments']."</td>
</tr>
<tr>
<td style=\"color: silver; text-align: right; background-color: #EAE8C8; \">";
$result = mysql_query($sql) OR exit( 'Error: ' . mysql_error() );
for($i=0; $i<count($emails); $i++){
while($row = mysql_fetch_array($result)){
if($emails[$i]==$row["email"]){
$message .="<a href=\"member.php\">".$row["username"]."</a>";
}else{
$message .="<a href=\"http://".$_SERVER['HTTP_HOST']."/thewheelofgod/twotexts/registration.php\">Register</a>";
}
}
}
Early in the code I get the emails by the get method and form an array $emails. Then my intention is to search these emails in the db. If it's not found I want the send the register link. If it's found, the username link.
Re: if negative result in sql search how to express?
the query you make returns a result that finds all of the emails that might have been input. there is no need for you to loop through your email array -- all you need to do is loop through the result set. also, there is again no need to check if the email is one of the entered emails; you're only returning the entered emails. if no emails are found, then mysql_count() will return 0.
PHP Code:
$query = mysql_query($sql);
if(mysql_count($query) > 0){
// loop through result set
}else{
echo 'register';
}
Re: if negative result in sql search how to express?
Quote:
Originally Posted by
kows
the query you make returns a result that finds all of the emails that might have been input. there is no need for you to loop through your email array -- all you need to do is loop through the result set. also, there is again no need to check if the email is one of the entered emails;
you're only returning the entered emails. if no emails are found, then mysql_count() will return 0.
PHP Code:
$query = mysql_query($sql);
if(mysql_count($query) > 0){
// loop through result set
}else{
echo 'register';
}
Is this good to keep?
OR exit( 'Error: ' . mysql_error() )
it gives error at times but with no line indication. so I block it.
Re: if negative result in sql search how to express?
if it gives you an error it's because your script is building an incomplete query. look at the query it's trying to execute when you get errors.
Re: if negative result in sql search how to express?
Quote:
Originally Posted by
kows
if it gives you an error it's because your script is building an incomplete query. look at the query it's trying to execute when you get errors.
Nothing shows after the body tag:
PHP Code:
<?php
//$time = set_time_limit(30);
require_once("includefiles/dbconnection.php");
require_once("logininfo.php");
$from = $_GET['youremail'];
$emails = explode(", ", $_GET['theiremail']);
$subject = $_GET['subject'];
$sql="SELECT * FROM $dbTable WHERE";
$to="";
$num=count($emails)-1;
//echo $num;
for($i=0; $i<count($emails); $i++){
if($i<$num){
$sql.=" email='".$emails[$i]."' OR ";
$to .= $emails[$i].", ";
}else{
$sql.=" email='".$emails[$i]."'";
$to .= $emails[$i];
}
}
if($group == "Administrator"){
echo $sql;
}
$errors = array();
?>
<!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>
<?php //echo "<meta http-equiv='Refresh' content='".$time."; url=".$continue."' />";
//echo $time;
?>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?php echo $subject; ?></title>
</head>
<body>
<?php
// Remove $_COOKIE elements from $_GET.
if(count($_COOKIE)){
foreach(array_keys($_COOKIE) as $value){
unset($_GET[$value]);
}
}
// Validate email field.
if(isset($_GET['email']) && !empty($_GET['email'])){
$_GET['email'] = trim($_GET['email']);
if(substr_count($_GET['email'],"@") != 1 || stristr($_GET['email']," ")){
$errors[] = "Email address is invalid";
}else{
$exploded_email = explode("@",$_GET['email']);
if(empty($exploded_email[0]) || strlen($exploded_email[0]) > 64 || empty($exploded_email[1])){
$errors[] = "Email address is invalid";
}else{
if(substr_count($exploded_email[1],".") == 0){
$errors[] = "Email address is invalid";
}else{
$exploded_domain = explode(".",$exploded_email[1]);
if(in_array("",$exploded_domain)){
$errors[] = "Email address is invalid";
}else{
foreach($exploded_domain as $value){
if(strlen($value) > 63 || !preg_match('/^[a-z0-9-]+$/i',$value)){
$errors[] = "Email address is invalid"; break;
}
}
}
}
}
}
}
/*
// Check referrer is from same site.
if(!(isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']) && stristr($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST']))){
$errors[] = "You must enable referrer logging to use the form";
}
*/
// Check for a blank form.
function recursive_array_check_blank($element_value){
global $set;
if(!is_array($element_value)){
if(!empty($element_value)){
$set = 1;
}
}else{
foreach($element_value as $value){
if($set){
break;
} recursive_array_check_blank($value);
}
}
}
recursive_array_check_blank($_GET);
if(!$set){
$errors[] = "You cannot send a blank form";
}
unset($set);
// Display any errors and exit if errors exist.
if(count($errors)){
foreach($errors as $value){
print "$value<br>";
}
exit;
}
if(!defined("PHP_EOL")){
define("PHP_EOL", strtoupper(substr(PHP_OS,0,3) == "WIN") ? "\r\n" : "\n");
}
// Build message.
function build_message($request_input){
if(!isset($message_output)){
$message_output ="";
}
if(!is_array($request_input)){
$message_output = $request_input;
}else{
foreach($request_input as $key => $value){
if(!empty($value)){
if(!is_numeric($key)){
$message_output .= str_replace("_"," ",ucfirst($key)).": ".build_message($value).PHP_EOL.PHP_EOL;
}else{
$message_output .= build_message($value).", ";
}
}
}
}
return rtrim($message_output,", ");
}
$html_begin = "<html>
<head>
<title>".$subject."</title>
</head>
<body>
<table style=\"border: 1px solid #7A1010;background-image:url(images/ripples.jpg);\">
<tr>
<td>
<a href=\"http://".$_SERVER['HTTP_HOST']."/thewheelofgod/twotexts/\">
<img src=\"http://".$_SERVER['HTTP_HOST']."/thewheelofgod/twotexts/images/wheeltitle.jpg\" style=\"border: none;\" />
</a>
</td>
</tr>";//$_SERVER['REQUEST_URI'].
$html_end = "</table>
</body>
</html>";
$message = $html_begin."<tr>
<td>".$_GET['passage']."</td>
</tr>
<tr>
<td>
<table style=\"background-color: #EAE8C8; width: 100%;\">
<tr>
<td><span style=\"font-weight: bold;\">".$_SESSION['username']."</span> says: </td>
</tr>
<tr>
<td style=\"background-color: #EAE8C8; \">".$_GET['comments']."</td>
</tr>
<tr>
<td style=\"color: silver; text-align: right; background-color: #EAE8C8; \">";
$query = mysql_query($sql) OR exit( 'Error: ' . mysql_error() );
if(mysql_count($query) > 0){
for($i=0; $i<count($emails); $i++){
if($emails[$i]==$row["email"]){
$message .="<a href=\"member.php\">".$row["username"]."</a>";
}
}
}else{
$message .="<a href=\"http://".$_SERVER['HTTP_HOST']."/thewheelofgod/twotexts/registration.php\">Register</a>";
}