conan318 Posted May 6, 2011 Share Posted May 6, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/235740-set-a-variable-depending-on-the-out-of-the-query/ Share on other sites More sharing options...
fugix Posted May 6, 2011 Share Posted May 6, 2011 $getuname = mysql_real_escape_string($_GET['username']); are you trying to grab data from your table here? Quote Link to comment https://forums.phpfreaks.com/topic/235740-set-a-variable-depending-on-the-out-of-the-query/#findComment-1211725 Share on other sites More sharing options...
fugix Posted May 6, 2011 Share Posted May 6, 2011 also, i wouldnt reccomend using ORDER BY RAND(), bad practice to get into, look at a couple links from here Quote Link to comment https://forums.phpfreaks.com/topic/235740-set-a-variable-depending-on-the-out-of-the-query/#findComment-1211726 Share on other sites More sharing options...
conan318 Posted May 6, 2011 Author Share Posted May 6, 2011 yes trying to get the usersname store it the variable $getuname and call it up later here Echo '<a href="new_message.php?username=' . "$getuname" . '">send message</a>'."<br/>"; Quote Link to comment https://forums.phpfreaks.com/topic/235740-set-a-variable-depending-on-the-out-of-the-query/#findComment-1211727 Share on other sites More sharing options...
conan318 Posted May 6, 2011 Author Share Posted May 6, 2011 the idea of order by random is just so it displays a differnt record everytime im not 2 concerned on how random it is Quote Link to comment https://forums.phpfreaks.com/topic/235740-set-a-variable-depending-on-the-out-of-the-query/#findComment-1211730 Share on other sites More sharing options...
smsmarketeers Posted May 6, 2011 Share Posted May 6, 2011 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']; Quote Link to comment https://forums.phpfreaks.com/topic/235740-set-a-variable-depending-on-the-out-of-the-query/#findComment-1211736 Share on other sites More sharing options...
xyph Posted May 6, 2011 Share Posted May 6, 2011 $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 Quote Link to comment https://forums.phpfreaks.com/topic/235740-set-a-variable-depending-on-the-out-of-the-query/#findComment-1211737 Share on other sites More sharing options...
conan318 Posted May 7, 2011 Author Share Posted May 7, 2011 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>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/235740-set-a-variable-depending-on-the-out-of-the-query/#findComment-1211755 Share on other sites More sharing options...
fugix Posted May 7, 2011 Share Posted May 7, 2011 $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? Quote Link to comment https://forums.phpfreaks.com/topic/235740-set-a-variable-depending-on-the-out-of-the-query/#findComment-1211759 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.