I am using the codeigniter framework. I have session set in $this->session->userdata('sess_place') but I cannot echo using if condition. Maybe my if condition is wrong?
$pl = $this->session->userdata('sess_place');
if(isset($pl) && $pl = TRUE) {
echo $pl;
}
else {
echo "Select your city";
}
You have used assign operater(=) instead of ==, which is wrong. Try this:
$pl = $this->session->userdata('sess_place');
if(isset($pl) && $pl == true) // and also no need of isset($pl) here jusy if($pl == TRUE) is enough
{
echo $pl;
}
else
{
echo "Select your city";
}
This should work for you:
$pl = $this->session->userdata('sess_place');
if(!empty($pl)) {
echo $pl;
} else {
echo "Select your city";
}
Try to fetch data from the session. If it returns something other than false, it must be set.Try with -
if ($this->session->userdata('sess_place')) {
echo $this->session->userdata('sess_place');
} else {
echo "Select your city";
}
If you storing Boolean value in session then try this.
$pl = $this->session->userdata('sess_place');
if(isset($pl) && $pl === true) // this checks value and its data type
{
echo "Got it";
}
else
{
echo "Select your city";
}
Related
I am trying to make a website that echos out a number or a word based on some conditions. I connected it to my database, but it always echos out 2 (user not found), instead of yes100 (password and username correct).
The weird thing is, it works on my main domain, where it outputs yes100, but here it just can not do that for some reason.
I am sure my database details are correct, and I have uploaded the file where it should be.
This is my code (not secure at all, but it is for personal use only.)
$result = $link->query($sql);
if ($result->num_rows > 0) {
// Outputting the rows
while($row = $result->fetch_assoc())
{
$password = $row['password'];
$salt = $row['salt'];
$plain_pass = $_GET['password'];
$stored_pass = md5(md5($salt).md5($plain_pass));
function Redirect($url, $permanent = false)
{
if (headers_sent() === false)
{
header('Location: ' . $url, true, ($permanent === true) ? 301 : 302);
}
exit();
}
if($stored_pass != $row['password'])
{
echo "BLAHAHAHAHAHAHAHAHA";
exit();
}
else
{
echo "yes"; // Correct pass
}
if (strlen($row['hwid']) > 1)
{
if ($hwid != $row['hwid'])
{
echo "0"; // Wrong
}
else
{
echo "100"; // Correct
}
}
else
{
$sql = "UPDATE ". $tables ." SET hwid='$hwid' WHERE username='$user'";
if(mysqli_query($link, $sql))
{
echo "rdy"; // HWID Set
exit();
}
else
{
echo "4"; // Else errors
exit();
}
}
}
}
else
{
echo "2"; // User doesn't exist
exit();
}
?>
I forgot to give the user the permissions. It works now. Thanks everyone.
I have a search function which matches keywords from a database and echo's out some html but I'm missing how to enable the handling of empty searches. Can I use an else statement or do I have to redefine the same parameters and use !isset for if not set?
<?php
$con = mysqli_connect("localhost", "database", "password", "table");
if (isset($_GET['search'])) {
$search_query = $_GET['search_query'];
global $con;
$get_item = "select * from database where keywords like '%$search_query%'";
$run_item = mysqli_query($con, $get_item);
while ($row_item = mysqli_fetch_array($run_item)) {
$item_keywords = $row_item['item_keywords'];
echo "Search found for $search_query";
} // working fine up to here
} else {
echo "Search not found for $search_query";
}
?>
You can use isset() and mysqli_num_rows() to check empty result. ANd use mysqli_real_escape_string for fire your query
if (isset($_GET['search'])) {
if (isset($_GET['search_query']) && $_GET['search_query'] != "") {/// check variable is set or not
$search_query = $_GET['search_query'];
$$search_query = mysqli_real_escape_string($con, $search_query);//
$get_item = "select * from `database` where `keywords` like '%$search_query%'";
$run_item = mysqli_query($con, $get_item);
$row_cnt = mysqli_num_rows($run_item); // count number of rows
if ($row_cnt > 0) {
while ($row_item = mysqli_fetch_array($run_item)) {
$item_keywords = $row_item['item_keywords'];
echo "Search found for $item_keywords";
}
} else {
echo "Search not found for $item_keywords";
}
} else {
echo "Search not found for $item_keywords";
}
}
Change your condition to:
if (isset($_GET['search']) && trim($_GET['search_query']) != '') {
}
You can set condition for empty search like this.
if (isset($_GET['search']) && trim($_GET['search']) !='') { ... }
Modify your if condition
if (isset($_GET['search']) && !empty($_GET['search_query'])) {
}
You can do it by using the empty function:
if (!empty($_GET['search'])) {
//your code
}
/MY CODE/
The if part is working properly but else is not working.
i even tried $variable instead of direct echo but still it is not working 'else'
Updated
<?php
$db = new mysqli('localhost', 'root' ,'', 'timeline');
if(!$db) {
echo 'Could not connect to the database.';
} else {
if(isset($_POST['queryString'])) {
$queryString = $db->real_escape_string($_POST['queryString']);
if(strlen($queryString) >0) {
$query = $db->query("SELECT collegename FROM college WHERE collegename LIKE '$queryString%' LIMIT 10");
if(isset($query)) {
echo '<ul>';
while ($result = $query ->fetch_object()) {
echo '<li onClick="fill(\''.addslashes($result->collegename).'\');">'.$result->collegename.'</li>';
}
echo '</ul>';
} else {
echo 'create some'; // this part is not working
}
} else {
// do nothing
}
} else {
echo 'There should be no direct access to this script!';
}
}
?>
help me out.....
even read lots of like problem on stackoverflow but no real return
If you are using mysqli::query then your if(isset($query)) statement will always be evaluated as true, as $query would be either FALSE or a mysqli_result object. isset returns TRUE for both these values, so your else code will never be called.
Documentation on isset:
Returns TRUE if var exists and has value other than NULL, FALSE otherwise.
Use if($query !== false) instead.
Update
It also seems like you are checking $query to see whether or not there was a hit in the database. You need to check the number of rows in the result for that, e.g:
if ($query !== false && $query->num_rows > 0) {
// Query was ok and at least one row was returned
}
else {
// Will be reached if query was bad or there were no hits
}
Try
if($query_run = $db->query("SELECT collegename FROM college WHERE collegename LIKE '$queryString%' LIMIT 10")){
echo '<ul>';
while ($result = $query ->fetch_object()) {
echo '<li onClick="fill(\''.addslashes($result->collegename).'\');">'.$result->collegename.'</li>';
}
echo '</ul>';
} else {
echo 'create some';
}
This is a really simple one, I just can't get my head around it sorry. I have this PHP code which picks up my form value, then compares it with the value stored in the database. That works fine.
However I am not sure how to write this logic in terms of this query:
If posted value = database value {
// do something } else { // do something else }
if (empty($_POST['order_id']) === false) {
// prepare data for inserting
$order_id = htmlentities(trim($_POST['order_id']));
$order_id = preg_replace("/[^0-9]/","", $order_id);
$result = mysqli_query($con,"SELECT * FROM listings WHERE order_id = $order_id");
$row = mysqli_fetch_assoc($result);
echo $row['order_id'];
}
SOLVED:
Solved the question, was a silly one I know! Just needed this at the end of the code:
if($order_id === $row['order_id']) {
echo 'found';
} else {
echo 'not found';
}
Try
If ($result->num_rows === 1) { do something } else { do something else }
Since you did the business logic in your query you can just use
if( ! is_null($row)) {
// do
} else {
// nothing
}
Did I read too much into "If posted value = database value "? Are you just referring to the order_id?
if ($row['listingName'] == $_POST['frm_listingName']) {
// something
}
else {
//something else
}
Check this code:
if (empty($_POST['order_id']) === false) {
// prepare data for inserting
$order_id = htmlentities(trim($_POST['order_id']));
$order_id = preg_replace("/[^0-9]/","", $order_id);
$result = mysqli_query($con,"SELECT * FROM listings WHERE order_id = $order_id");
if(mysqli_num_rows($result)>0)
{
//Match found. do something.
}
else
{
//No match found. do something.
}
}
N.B. In place of mysqli_num_rows($result) you can also use $result->num_rows
What I'm trying to do is make it so that when a specific user level is set in my Database it shows up specific text. Here is my code.
$userget = mysql_query("SELECT * FROM `usertable` WHERE `username`='".$_SESSION['user']."'");
$user = mysql_fetch_array($userget);
function getUserlevel() {
if($user['userlevel'] == '1') { echo "Regular User"; }
elseif($user['userlevel'] == '2') { echo "Moderator"; }
elseif($user['userlevel'] == '3') { echo "Administrator"; }
else { echo "Undefined"; }
}
I know the function is working, because the Echo of Undefined works, which I set up as a test but the others are not working despite my user level being set to 3 and only echoing Undefined. A session is also set for when the user is logged in.
This is when the function is called.
<div class="userData">Welcome Back, <?php echo $_SESSION['user']; ?><hr /><?php
getUserlevel(); ?></div>
Ok, I'm going to take a stab at:
getUserlevel($user['userlevel']);
function getUserlevel($usrlvl) {
if($usrlvl == '1') { echo "Regular User"; }
elseif($usrlvl == '2') { echo "Moderator"; }
elseif($usrlvl == '3') { echo "Administrator"; }
else { echo "Undefined"; }
}
I just included query into function, and its working :P
<?php
function getUserlevel() {
$userget = mysql_query("SELECT * FROM `users` WHERE `username`='".$_SESSION['username']."'");
$user = mysql_fetch_array($userget);
if($user['user_level'] == 1) { echo "Regular User"; }
elseif($user['user_level'] == 2) { echo "Moderator"; }
elseif($user['user_level'] == 3) { echo "Administrator"; }
else { echo "Error"; }
}
?>
You can not access $user as $user[userlevel] because mysql_fetch_array return an array. Does username is unique if not use an id instead of using username. So you should write the function as below
function getUserlevel()
{
foreach($user as $u)
{
if($u['userlevel'] == 1){echo "Regular User";}
else if($u['userlevel'] == 2){echo "Moderator";}
else if($u['userlevel'] == 3){echo "Administrator";}
else{echo "Undefined";}
}
}
If it does not work make sure you have write a vaild mysql query