0

I'm very new here as well as in php and mysql and I hope you can help me :)

I'm trying to build a JSON for a visualization using data from a database. My problem is, that it doesn't passes the if-request. In the while-loop the data should be used row by row in a certain way but before that, there have to be done something else. But everthing ONLY if there is any row in the result.

Following code is relevant for my issue:

            //get data where a value fits former input
            $query = "SELECT * FROM table1 WHERE column1 = '" . $input. "'";
            $data = mysql_query($query) or die (mysql_error());

            if ( mysql_num_rows(data) > 0){
            //if($data != NULL){ (didn't work either)

                //do something

                while($data_Row = mysql_fetch_array($data)){

                    //do something

                }
            }

I hope you understood the issue (not sure whether I was able to make it clear). I already tested several solutions from diverse sites but nothing worked so far.

I would appreciate your help :)

Maki
  • 109
  • 13
  • 3
    `mysql_num_rows(data)` is a typo, `$data`. Also looks like you are open to SQL injections. – chris85 Oct 23 '16 at 16:19
  • omg. Except of another typo in my JSON that's it -.- sometimes one should leave the code for some hours and read it again later I don't think I'm open to injections. It's not a real input... it was just for testing purpose. Thanks for help :D – Maki Oct 23 '16 at 18:22
  • **WARNING**: If you're just learning PHP, please, do not use the [`mysql_query`](http://php.net/manual/en/function.mysql-query.php) interface. It’s so awful and dangerous that it was removed in PHP 7. A replacement like [PDO is not hard to learn](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/) and a guide like [PHP The Right Way](http://www.phptherightway.com/) explains best practices. Your user parameters are **not** [properly escaped](http://bobby-tables.com/php) and there are [SQL injection bugs](http://bobby-tables.com/) that can be exploited. – tadman Oct 23 '16 at 19:40
  • Ok, thanks. I'll have a look at this! – Maki Oct 24 '16 at 16:29

1 Answers1

0

Try this

if ($data){
    while($row = mysql_fetch_array($data)){
        //do stuff
    }
}
Peck3277
  • 1,383
  • 8
  • 22
  • 46
  • 3
    Why should the OP "try this". A ***good answer*** will have an explanation of what was done and why. This helps to the OP to understand as well as future visitors to Stack Overflow. – Jay Blanchard Oct 23 '16 at 16:45
  • This doesn't work. Tried this already in all kinds of variations. – Maki Oct 23 '16 at 18:24