1

I have a MySQL query, that runs on user input.

How would I write an IF statement to see if a row is set?

I have tried:

if (!isset($row['example']) === true) {
//row not set
}

and:

if (empty($row['example'])) {
//row not set
}

Thanks!

***Edit: The first IF statement I tried actually did work. Sorry for any trouble I caused.

7
  • If I understand you correctly: you run the query and see if any rows were returned, usually by testing the length of the returned array. It would help if we saw a more complete sample of your code, though. Commented May 8, 2013 at 17:55
  • Just a side note: You don't need to do (!isset($row['example']) === true), it can be written as (!isset($row['example'])) because isset() returns a boolean value. So writing !isset($var) will check if isset() returns false. Commented May 8, 2013 at 17:57
  • Thanks Chris, I'll remember that Commented May 8, 2013 at 17:58
  • @user2180108 no problem, can you share more of your code with us? Commented May 8, 2013 at 17:59
  • I have a user input, which accesses a database. If the input matches something in the database, it checks which type the command was that was entered, which is $row['type']. Then it does different things depending on the value of $row['type']. if ($row['type'] == 1) {}; if ($row['type'] == 2) {}; etc. Then, at the end, I want to see if the command matched the database at all, by seeing if $row['type'] is set. The problem being, that if statement doesn't work. Commented May 8, 2013 at 18:10

2 Answers 2

2

Simple check would be

if(isset($row['example']))

or better check you have any records returned from sql using

mysql_num_rows
Sign up to request clarification or add additional context in comments.

Comments

0

Simplest way ever:

if ((isset($row) === true) !== false) {
    if(!empty($row) === true){
       echo "row is good";
    }
}

1 Comment

you're gonna confuse the guy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.