Jump to content

set a variable depending on the out of the query.


conan318

Recommended Posts

i have a small search profile search and need to set $getuname variable to users username

 

if((trim($country) !== ''&&($state) !== ''&&($city) !== '')){

 

 

$data = mysql_query("SELECT    *  FROM users WHERE users.state='$state' AND users.country='$Country' AND users.city='$city' ORDER BY RAND() DESC LIMIT 1 ");

Echo "Country city state search";

$getuname = mysql_real_escape_string($_GET['username']);

 

am i meant to use the get or is that only for forms?

Link to comment
Share on other sites

Here is a better way to write this:

 

<?php

if ((trim($country) !== "") && ($state !== "") && ($city !== "")) {
     $query = mysql_query("SELECT * FROM users WHERE users.state='" . $state . "' AND users.country='" . $country . "' AND users.city='" . $city . "' ORDER BY RAND() DESC LIMIT 1");
     $result = mysql_fetch_array($query);

     Echo "Country / City / State / Search";

    $getuname = $result['username'];

Link to comment
Share on other sites

$data = mysql_query("SELECT username FROM users WHERE users.state='$state' AND users.country='$Country' AND users.city='$city' ORDER BY RAND() DESC LIMIT 1 ");

$getuname = mysql_result( $data, 0 );

 

 

You also use $country and $Country to reference the same variable, which they are not.

 

Careful coding

Link to comment
Share on other sites

ive been mucking around with it and still havent been able to crack it. now when i submit the form it returns no data.

$getuname = mysql_result( $data, 0 ); it threw up an error cant find row 0.

so heres a larger snippit of my code.



$Country=$_GET['Country'];
$state=$_GET['state'];
$city=$_GET['city'];








if((trim($Country) !== ''&&($state) !== ''&&($city) !== '')){


$data = mysql_query("SELECT    *  FROM users WHERE users.state='$state' AND users.country='$Country' AND users.city='$city' ORDER BY RAND() DESC LIMIT 1 ");	

  $info = mysql_fetch_array($data);
  $getuname = $result['username'];

} elseif((trim($country) !== ''&&($state) == ''&&($city) == '')){

	$data = mysql_query("SELECT    *  FROM users WHERE users.country='$country'ORDER BY RAND() DESC LIMIT 1 ");		
$info = mysql_fetch_array($data);
  $getuname = $result['username'];
} elseif((trim($Country) == ''&&($state) !== ''&&($city) == '')){

	$data = mysql_query("SELECT    *  FROM users WHERE users.state='$state'ORDER BY RAND() DESC LIMIT 1 ");		
 $info = mysql_fetch_array($data);
  $getuname = $result['username'];
} elseif((trim($country) !== ''&&($state) !== ''&&($city) == '')){

	$data = mysql_query("SELECT *  FROM users WHERE users.state='$state' AND users.country='$Country'ORDER BY RAND() DESC LIMIT 1 ");	
	 $info = mysql_fetch_array($data);
  $getuname = $result['username'];
} elseif((trim($Country) !== ''&&($state) == ''&&($city) !== '')){

	$data = mysql_query("SELECT *  FROM users WHERE users.country='$Country' AND users.city='$city'ORDER BY RAND() DESC LIMIT 1 ");	
	 $info = mysql_fetch_array($data);
  $getuname = $result['username'];

} elseif((trim($Country) == ''&&($state) !== ''&&($city) !== '')){

	$data = mysql_query("SELECT *  FROM users WHERE users.state='$state' AND users.city='$city' ORDER BY  RAND() DESC LIMIT 1");	
 $info = mysql_fetch_array($data);
  $getuname = $info['username'];

}





$queryuser=mysql_query("SELECT * FROM freinds WHERE freind='$myusername' AND username='$getuname' OR freind='$getuname' AND username='$myusername'");
//$result=mysql_query($sql);
$checkuser=mysql_num_rows($queryuser);
if($checkuser != 0){





while($result = mysql_fetch_array( $data )){
Echo $getuname;
echo $myusername;
Echo'<div class="profile">';
Echo'<div class="pframe">'; 

Echo '<a href="new_message.php?username=' . "$getuname" . '">send message</a>'."<br/>";

Echo '<div class="pprofile">';
Echo "<img src='http://datenight.netne.net/images/".$info['img'] ."' width='300' height='300''> <br>"; 

Echo '<div class="pname">';
Echo "<b>Name:</b> ".$info['username'] . ": "; 
Echo "<b>Sex:</b> ".$info['sex'] . ": "; 
Echo "<b>age:</b> ".$info['age'] . ": "." <br>";
Echo '</div>';
Echo '<div class="plocation">';
Echo "<b>country:</b> ".$info['country'] . ": "."<br/>";
Echo "<b>city:</b> ".$info['city'] . ": "."<br/>"; 
Echo "<b>Suburb:</b> ".$info['suburb'] . ": "."<br/>";
Echo "<b>Sate:</b> ".$info['state'] . ": "." <br/>";
Echo '</div>';
Echo '<div class="pintrested">';
Echo "<b>Intrested in</b>" . ": "."<br/>"; 
Echo "".$info['rel'] . ": "."<br/>";
Echo "".$info['frwb'] . ": "."<br/>";
Echo "".$info['ons'] . ": "."<br>"; 
Echo "".$info['fr'] . "<br>";
Echo '</div>';
Echo '<div class="paboutme">';
Echo "<b>About me:</b> ".$info['aboutme'] . "<br> ";
Echo '</div>';
Echo '<div class="plooking">';
Echo "<b>Looking for:</b> ".$info['looking'] . " <br>"; 

}


?>

<form method="GET" action="<?php echo $_SERVER['PHP_SELF'];?>" name="searchForm">

<label>Select Country</label>
    <select name="Country">
    <option value="Australia">Australia</option>
    <option value="USA">USA</option>
<option value="Canada">Canada</option>
<option value="England">England</option>
<option value="New Zealand">New Zealand</option>
      </select><br />
      <label>Select State</label>
     <select name="state">
    <optgroup label="Australia">
    <option value="QLD">QLD</option>
<option value="NSW">NSW</option>
<option value="ACT">ACT</option>
<option value="VIC">VIC</option>
<option value="TAZ">TAZ</option>
<option value="SA">SA</option>
<option value="WA">WA</option>
<option value="NT">NT</option>
    </optgroup>

    </form>
<?
Echo '</div>';
} else {





if((trim($Country) !== ''&&($state) !== ''&&($city) !== '')){


$data = mysql_query("SELECT    *  FROM users WHERE users.state='$state' AND users.country='$Country' AND users.city='$city' ORDER BY RAND() DESC LIMIT 1 ");	

  $info = mysql_fetch_array($data);
  $getuname = $result['username'];

} elseif((trim($Country) !== ''&&($state) == ''&&($city) == '')){

	$data = mysql_query("SELECT    *  FROM users WHERE users.country='$Country'ORDER BY RAND() DESC LIMIT 1 ");		
$info = mysql_fetch_array($data);
  $getuname = $result['username'];
} elseif((trim($country) == ''&&($state) !== ''&&($city) == '')){

	$data = mysql_query("SELECT    *  FROM users WHERE users.state='$state'ORDER BY RAND() DESC LIMIT 1 ");		
 $info = mysql_fetch_array($data);
  $getuname = $result['username'];
} elseif((trim($Country) !== ''&&($state) !== ''&&($city) == '')){

	$data = mysql_query("SELECT *  FROM users WHERE users.state='$state' AND users.country='$country'ORDER BY RAND() DESC LIMIT 1 ");	
	 $info = mysql_fetch_array($data);
  $getuname = $result['username'];
} elseif((trim($Country) !== ''&&($state) == ''&&($city) !== '')){

	$data = mysql_query("SELECT *  FROM users WHERE users.country='$country' AND users.city='$city'ORDER BY RAND() DESC LIMIT 1 ");	
	 $info = mysql_fetch_array($data);
  $getuname = $result['username'];

} elseif((trim($Country) == ''&&($state) !== ''&&($city) !== '')){

	$data = mysql_query("SELECT *  FROM users WHERE users.state='$state' AND users.city='$city' ORDER BY  RAND() DESC LIMIT 1");	
 $info = mysql_fetch_array($data);
  $getuname = $info['username'];

}


while($info = mysql_fetch_array( $data )){


Echo'<div class="profile">';
Echo'<div class="pframe">'; 

Echo '<a href="freinds.php?username=' . "$getuname" . '">Contact request</a>'."<br/>";

Echo '<div class="pprofile">';
Echo "<img src='http://datenight.netne.net/images/".$info['img'] ."' width='300' height='300''> <br>"; 

Echo '<div class="pname">';
Echo "<b>Name:</b> ".$info['username'] . ": "; 
Echo "<b>Sex:</b> ".$info['sex'] . ": "; 
Echo "<b>age:</b> ".$info['age'] . ": "." <br>";
Echo '</div>';
Echo '<div class="plocation">';
Echo "<b>country:</b> ".$info['country'] . ": "."<br/>";
Echo "<b>city:</b> ".$info['city'] . ": "."<br/>"; 
Echo "<b>Suburb:</b> ".$info['suburb'] . ": "."<br/>";
Echo "<b>Sate:</b> ".$info['state'] . ": "." <br/>";
Echo '</div>';
Echo '<div class="pintrested">';
Echo "<b>Intrested in</b>" . ": "."<br/>"; 
Echo "".$info['rel'] . ": "."<br/>";
Echo "".$info['frwb'] . ": "."<br/>";
Echo "".$info['ons'] . ": "."<br>"; 
Echo "".$info['fr'] . "<br>";
Echo '</div>';
Echo '<div class="paboutme">';
Echo "<b>About me:</b> ".$info['aboutme'] . "<br> ";
Echo '</div>';
Echo '<div class="plooking">';
Echo "<b>Looking for:</b> ".$info['looking'] . " <br>"; 

}
?>
<form method="GET" action="<?php echo $_SERVER['PHP_SELF'];?>" name="searchForm">

<label>Select Country</label>
    <select name="Country">
    <option value="Australia">Australia</option>
    <option value="USA">USA</option>
<option value="Canada">Canada</option>
<option value="England">England</option>
<option value="New Zealand">New Zealand</option>
      </select><br />
      <label>Select State</label>
     <select name="state">
    <optgroup label="Australia">
    <option value="QLD">QLD</option>
<option value="NSW">NSW</option>
<option value="ACT">ACT</option>
<option value="VIC">VIC</option>
<option value="TAZ">TAZ</option>
<option value="SA">SA</option>
<option value="WA">WA</option>
<option value="NT">NT</option>
    </optgroup>
</form>
      
    
  
    <input type="submit" value="submit" />
    </form>

<?
 Echo '</div>';
}
?>

Link to comment
Share on other sites

$Country=$_GET['Country'];
$state=$_GET['state'];
$city=$_GET['city'];

 

where are these coming from? If they are coming from your form...can you please show the code for the form as well?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.