-1

This is how i put my information from the database in a array:

$sql = "SELECT city, read FROM location";
$info = mysql_query($sql);
$read = array();
while ($row = mysql_fetch_assoc($info)){
    $read[] = $row;
}

This is a piece of the output of $read (there are 40 city`s in the table):

Array ([0] => Array ([city] => New York [read] => false) [1] => Array ([city] => Paris [read] => true ))

Now I want to check if the read value of a city is true then echo something.

I don`t know how to do this do I need a for loop or just a if statement?

Can someone help?

roberto06
  • 3,844
  • 1
  • 18
  • 29
user3356007
  • 393
  • 1
  • 6
  • 20
  • you can just if at the same time you are putting data into `$read[]` – Naruto Feb 10 '17 at 14:23
  • 3
    **Don't** use the **deprecated and insecure** `mysql_*`-functions. They have been deprecated since PHP 5.5 (in 2013) and were completely removed in PHP 7. Use MySQLi or PDO instead. – M. Eriksson Feb 10 '17 at 14:24
  • @MagnusEriksson Still using an older version so that`s no issue – user3356007 Feb 10 '17 at 14:28
  • 1
    @user3356007 That doesn't mean `mysql_*` functions are any less insecure. – roberto06 Feb 10 '17 at 14:29
  • 2
    You're script won't be as secure as it could be + it won't be future proof. It's better to use the non deprecated libraries now, while you're developing. Otherwise you will get much more to rewrite later on. – M. Eriksson Feb 10 '17 at 14:32
  • your code is failing here; know why? least for what you posted and if that is your actual code you're using – Funk Forty Niner Feb 10 '17 at 14:33
  • @MagnusEriksson I understand thx for the feedback! – user3356007 Feb 10 '17 at 14:35
  • @Fred-ii- what do you mean? – user3356007 Feb 10 '17 at 14:37
  • 1
    BTW, how could you possibly have different keys in your sub-arrays (`city` vs. `locatie`), and how do you manage to get `bool` values for `read` from your request (instead of `string`) ? – roberto06 Feb 10 '17 at 14:37
  • Tell you what; try this along with the answers given and see what they missed, then post your error `$info = mysql_query($sql); if(!$info) { echo "Failed: " . mysql_error(); }` – Funk Forty Niner Feb 10 '17 at 14:38
  • I feel like you are sending out the wrong message here by accepting an answer that did not show the obvious error you made in your query. – Funk Forty Niner Feb 10 '17 at 14:40
  • @Fred-ii- don`t see any error and the array is getting filled? there is nog Fail in the query? – user3356007 Feb 10 '17 at 14:42
  • Probably what you posted isn't what you're really using because `READ` is a MySQL reserved word https://dev.mysql.com/doc/refman/5.7/en/keywords.html and find it hard to believe that your query didn't fail. Again; least not for what you posted. I know my reserved words quite well ;-) – Funk Forty Niner Feb 10 '17 at 14:43
  • @Fred-ii- You are right about that i typed it from another screen if have changed it! sorry en THX for your feedback! – user3356007 Feb 10 '17 at 14:50
  • @roberto06 You are right my fault I have changed it thx! – user3356007 Feb 10 '17 at 14:51
  • @user3356007 I had to do a rollback to a previous revision. Please leave the question as it was, since others may get downvoted because of it. It's fine as it is now. – Funk Forty Niner Feb 10 '17 at 15:12
  • @Fred-ii- I edited at the same time as you rolled back, looks like the system went with my edit instead of yours, sorry. – roberto06 Feb 10 '17 at 15:14
  • @roberto06 no worries. – Funk Forty Niner Feb 10 '17 at 15:15

2 Answers2

4
function checkCityRead($city){
    foreach($read as $r){
        if($r['city']==$city){
            if($r['read']=='true'){
                //do something
            }
        }
    }
}

or if want it as a general loop for all cities

foreach($read as $r){
    if($r['read']=='true'){
        //do something
    }
}

EDIT:

As many recommendations in the comments, you should not use the deprecated library mysql, and also you should your query if it has been executed or not "Credits to @Fred-ii", so your code could be like this:

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

// Check connection
if (mysqli_connect_errno()){
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else{
    $sql = "SELECT city, `read` FROM location";
    $info = mysqli_query($sql, $con);
    if(!$info){
        echo mysqli_error($con);
    } else{
        $read = array();
        while($row = mysqli_fetch_assoc($info)){
              $read[] = $row;
        }
    }
}
Mohammad
  • 3,449
  • 6
  • 48
  • 75
4

You need a foreach statement :

foreach ($read as $value) {
    if ($value['read'] == true)
        echo 'something';
}

And, as stated by Magnus Eriksson in his comment, you should really refrain from using mysql_* functions.

EDIT (as caught and suggested by Fred -ii-) : read is a reserved mySQL keyword, thus your query should have failed if you didn't include backticks, as follows :

$sql = "SELECT city, `read` FROM location";

Side note : You might want to compare $value['read'] to 'true' instead of true (string and not bool) as I'm pretty sure mysql_fetch_assoc() always returns string-type values :

foreach ($read as $value) {
    if ($value['read'] == 'true')
        echo 'something';
}
Community
  • 1
  • 1
roberto06
  • 3,844
  • 1
  • 18
  • 29
  • this answer missed something in the query – Funk Forty Niner Feb 10 '17 at 14:41
  • I saw your comments on OP's question, and I have to say I am at a loss to find what I've missed. I'll wait for OP to output the result of `mysql_error()`, but if he doesn't, could you eventually tell me what I missed ? – roberto06 Feb 10 '17 at 14:43
  • 1
    Nice catch with the `READ` reserved keyword, I have to admit I hadn't thought of that. – roberto06 Feb 10 '17 at 14:44
  • One gets to know them almost by heart after a while ;-) – Funk Forty Niner Feb 10 '17 at 14:45
  • You could modify your answer in accordance with what they did post. If they did not tick `\`` that column, then it's failing. If they did tick it, then they didn't post their real code and is a bit deceiving for (future) visitors to the question/answers. – Funk Forty Niner Feb 10 '17 at 14:47
  • @Fred-ii- and Roberto You guys are right I have changed it in the question thx for your attentiveness – user3356007 Feb 10 '17 at 15:02
  • @user3356007 You haven't replaced the right `read`. It's the one in the SQL query that you should escape with backticks (`\``), there's no need for renaming the PHP variable `$read`. – roberto06 Feb 10 '17 at 15:05
  • @user3356007 Just don't go editing your question with the new code around that column, because we would have done all that for nothing ;-) – Funk Forty Niner Feb 10 '17 at 15:05
  • @Fred-ii- I have changed it! Sorry for all the problems I will be more accurate next time! – user3356007 Feb 10 '17 at 15:09