-5

I am trying to show in a while loop the status (Active or Suspended) of a list of users.

0 = Active 1 = Suspended

<?php 
    $result = mysqli_query($con,"SELECT status FROM `e_customer`");
        while($row = mysqli_fetch_array($result)) {
        if ($row['status'] == "1") {
         echo "Suspended";
        } else {
          echo "Active";
        }
    } 
    ?>

The above gives me:

Notice: Undefined variable: con in /dashboard/index.php on line 101

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /dashboard/index.php on line 101

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in /dashboard/index.php on line 102

Patrick Simard
  • 2,294
  • 3
  • 24
  • 38
  • 2
    [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)*** Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Jul 24 '17 at 21:22
  • Mmmm im not sure but try mysqli_fetch_assoc instead – user2930137 Jul 24 '17 at 21:22
  • 2
    check for errors, am pretty sure you'll find the problem. I have my own thought. – Funk Forty Niner Jul 24 '17 at 21:22
  • 3
    Error checking? Have you looked in the logs? – Jay Blanchard Jul 24 '17 at 21:22
  • 1
    use `mysqli_error($con)` on the query; tell us what you see. Undefined index maybe? – Funk Forty Niner Jul 24 '17 at 21:24
  • The PHP is valid code (not the best way of doing it but should work) The problem might be the query. 2 things possible. There's a typo in col name of the query, if thats the case, or die(mysql_error() will show it to you. 2 the query returns nothing because $username is not properly set. Try a var_dump() before the query. – Patrick Simard Jul 24 '17 at 21:26
  • `print_r($row)` – u_mulder Jul 24 '17 at 21:27
  • 1
    *"There's a typo in col name of the query"* - @PatrickSimard That's what I have my money on, but the OP seems to want to remain silent or has left the question or doesn't know what to do about what I wrote. – Funk Forty Niner Jul 24 '17 at 21:29
  • 1
    You realize that `Status` and `status` are two different animals. Which one is the right one? *What say ye?* – Funk Forty Niner Jul 24 '17 at 21:30
  • 1
    OP's AWOL or *"the silent type"*. Sorry, no magic wand for you today. – Funk Forty Niner Jul 24 '17 at 21:35
  • 1
    @Fred Seems like OP is only interested in answers, not comments. – Qirel Jul 24 '17 at 21:46
  • 1
    @Qirel My sentiments exactly. and that's what they get for not listening to any of us. – Funk Forty Niner Jul 24 '17 at 21:47
  • @Fred-ii- i know Status & status are 2 different thing. row name is "status" it was a typo above. i am not able to comment on all query here because by default stackoverflow give around 20 comment facility. if i run our of it i can't give any answer to anyone. – Ankit Sinha Jul 24 '17 at 21:55
  • @AnkitSinha You can edit your question and include the information that is asked in the comments. – Paul Spiegel Jul 24 '17 at 22:08
  • *"i tried $"* - Undefined variable. – Funk Forty Niner Jul 24 '17 at 22:30
  • https://stackoverflow.com/questions/22662488/how-to-get-mysqli-error-in-different-environments – Paul Spiegel Jul 24 '17 at 22:32
  • @Fred-ii- is the correct one – Ankit Sinha Jul 24 '17 at 22:42
  • 1
    your code failed on too many levels. – Funk Forty Niner Jul 24 '17 at 22:47

3 Answers3

2

Be sure to answer the questions in the comments. Lot of smart people trying to help you.

I don't use mysqli. If I did I might try something like this...

<?php
if ($stmt = mysqli_prepare($con, "SELECT status FROM e_customer WHERE account = ?")) {

    /* bind parameters for markers */
    mysqli_stmt_bind_param($stmt, "s", $username);

    mysqli_stmt_execute($stmt);

    /* bind result variables */
    mysqli_stmt_bind_result($stmt, $status);

    mysqli_stmt_fetch($stmt);

    //is 0 really active and 1 suspended because than seems pretty backwards?
    if (!$status) { //per your comment then `!$status`. cringing...
        echo 'Active';
    } else {
        echo 'Suspended';
    }

    mysqli_stmt_close($stmt);
}

When expecting one row there is no need to loop. Not sure where username comes from - try and used parametrized (see PDO) statements.

ficuscr
  • 6,975
  • 2
  • 32
  • 52
  • Let me try your code, and yes in this case 0=active and 1=suspended this is i am pretty sure even i can see it in mysql console. – Ankit Sinha Jul 24 '17 at 21:48
  • When i use your given example code it doesnt give any output in PHP site. – Ankit Sinha Jul 24 '17 at 21:51
  • 1
    A blank page can mean you're facing a PHP error. Check your logs or activate debug. – Patrick Simard Jul 24 '17 at 21:53
  • No one will know what's wrong since the OP refuses to answer any questions. It's hard to help someone that won't help us help them. You could even call it "a waste of time". – M. Eriksson Jul 24 '17 at 21:54
  • We tried...We really tried. Ciao. – ficuscr Jul 24 '17 at 21:56
  • when i try to check connection, it apears to be working, i tried with it shows me as 0 and 1, whatever is there. but trouble is i am not able to translate that 1 and 0 to Active and Suspended keyword. – Ankit Sinha Jul 24 '17 at 22:07
1

First i would try your query directly in the database to make sure that it works, then try the following:

<?php 
    $result = mysqli_query($con,"SELECT status FROM `e_customer` WHERE account='$username'");
    $row = mysqli_fetch_assoc($result);
    echo ($row['status'] == "1") ? "Suspended" : "Active";
?>

If nothing still then try doing a var_dump of the $con variable and also the $row variable. see if you are even connecting and/or getting a result back.

...and also the $username variable.

Dallas Caley
  • 5,367
  • 6
  • 40
  • 69
  • I tried your way too, but now it is getting stuck to Active only even though mysql is giving 1(suspended) value. – Ankit Sinha Jul 24 '17 at 22:03
  • Hmm, well and you said the query is working directly in mysql so that doesn't leave many options. I suspect the problem is the $con connection link. what if, instead of echoing based on the ternary (as i show) you just tried to print_r or var_dump the entire $row variable. this might shed some light. – Dallas Caley Jul 24 '17 at 22:07
  • sorry, that thought is kinda scatterbrained. first, var_dump the $result. if it's FALSE then it's your connection, if it's a resultset then do what i said earlier – Dallas Caley Jul 24 '17 at 22:09
  • when i try to check connection, it apears to be working, i tried with it shows me as 0 and 1, whatever is there. but trouble is i am not able to translate that 1 and 0 to Active and Suspended keyword. – Ankit Sinha Jul 24 '17 at 22:09
  • $r or $row? sometimes it's as simple as a typo? – Dallas Caley Jul 24 '17 at 22:11
  • it is $r, it is not typo. – Ankit Sinha Jul 24 '17 at 22:11
  • you got me, i'm stumped. I guess the next thing i would look at is whether or not you are connecting to the same data source in all of your tests, and also check for things like data type. maybe you have a triple equals or something? maybe try without the quotes around the "1". pretty much stumped though. – Dallas Caley Jul 24 '17 at 22:17
  • it is same data source, the difference is the page already have few more tags on same table on same host. so i just tried to pull records from it and it worked. – Ankit Sinha Jul 24 '17 at 22:20
  • *"it is $r, it is not typo"* - Yet your query shows as `$row`. You are leaving out some important bits of information being "detail" that you may not feel as being important @AnkitSinha Plus, the added code `` you included in comments does not support your question. The source and value of `$username` is unknown. In light of all this, it's best that you check for errors as I told you to do in comments, that and PHP's error reporting. We've all done the best we could here, and I wish you well with this, good luck. – Funk Forty Niner Jul 24 '17 at 22:26
  • i cannot past complete website code here, the query i asked is for checking Account status from table for particular user and convert thos 0 and 1 to Active and suspended keyword and show to the user. $username is required for login that is why it is there because there could be thousands of user, i got to show whose account is active and whose is suspended. – Ankit Sinha Jul 24 '17 at 22:34
0

Now that we have the real PHP error, we can deal with it.

Make sure $con is initialized before your query. You might have forgotten an include or something. For debugging purpose add it above the query. Let me know if this solves your problem.

NOTE: mysqli_connect() needs 4 param

Host: 127.0.0.1 or localhost or the IP/DNS of the remote server

User: The username to connect to the DB

Password: The password to connect to the DB

DB Name: The name of the database

$con = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");

So the complete code would look like this:

<?php 
error_reporting( E_ALL );
ini_set('display_errors', 1);
$con = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");

$result = mysqli_query($con,"SELECT status FROM `e_customer`") or die(mysqli_error($con));
    while($row = mysqli_fetch_array($result)) {
    if ($row['status'] == "1") {
     echo $row['account']." is Suspended</br>";
    } else {
      echo $row['account']." is Active</br>";
    }
} 
?>
Community
  • 1
  • 1
Patrick Simard
  • 2,294
  • 3
  • 24
  • 38
  • there is no blank page. it is just no result neither Active(0) nor Suspended(1). and please whenever you give reply to any question have some patience i am checking the suggestion and replying both at the same time. atleast you should know so many suggestion pouring in with answer, everyone asking different things from debug, output and what not. how to get all info at once and reply to all? – Ankit Sinha Jul 24 '17 at 22:23
  • In the above suggestion, there are a few things tested. First, we make sure the php debug is activated and that warnings will show. Then we make sure username is really equal to something. Then we use mysqli_error() in case there's a query error. All the above addresses the questions that were asked. Try it and tell us what happens. – Patrick Simard Jul 24 '17 at 22:29
  • @PatrickSimard not my downvote here; sorry to see you got one. I edited your answer btw. – Funk Forty Niner Jul 24 '17 at 22:30
  • Thx @Fred-ii- ! I forgot about the $con hehe. As for the downvote, idk ... kind of pathetic right? lol At least it's not you hehe – Patrick Simard Jul 24 '17 at 22:32
  • this is the output of debug Notice: Undefined variable: con in /dashboard/index.php on line 101 Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /dashboard/index.php on line 101 Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in /dashboard/index.php on line 102 – Ankit Sinha Jul 24 '17 at 22:39
  • Well there you go! $con is the culprit! Make sure it's initialized. Did you forget an include("db.php"); or something like that? Might want to insert the $con above the debug and see how that goes. – Patrick Simard Jul 24 '17 at 22:42
  • I just edited my post above – Patrick Simard Jul 24 '17 at 22:48
  • 1
    @PatrickSimard (T'as l'air trop d'un bon gars pour que ch'te fasse ça), I wouldn't downvote you, your answer looks fine to me and for what the OP posted anyway. This is what's called the classic "rabbit hole" question and am sorry to see that you (and others) were chased down there. – Funk Forty Niner Jul 24 '17 at 22:49
  • Merci ;-) As long as the OP's problem is solved I don't mind getting a minus for giving my best. SO saved me a few times :-) Just glad I can give back. – Patrick Simard Jul 24 '17 at 23:00
  • yes my problem is solved, i was over thinking it. all i did this and it worked like charm.

    Account Status =
    – Ankit Sinha Jul 25 '17 at 07:56