-2

I got this error: "Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\dome.php on line 29" on localhost:69/dome.php I thought I have wrote query wrong but I think it is good because when I type it in phpmyadmin I got one result just like expected. So can someone help me where the problem is. Ok so I just updated my code and now I got this error "Connection succesfull! 1046: No database selected".


So I finally found the solution thanks to a guy @Lock the problem was that I was using two diffrent libaries i mixed my_sql* and my_sqli* in one connection.

//old code
public function GenerirajRandomMode()
            {   
                if($conn = new mysqli("localhost", "root", "", "modedb"))
                {   
                    echo "Connection succesfull";

                    $qt = "SELECT * FROM mode ORDER BY RAND() LIMIT 1";
                    $results = mysql_query($qt);
                    if ($results)
                    {
                        while ($row = mysql_fetch_assoc($results)) //line 29
                        {
                            echo '<tr>';
                            foreach($row as $field) 
                            {
                                echo '<td>' . htmlspecialchars($field) . '</td>';
                            }
                            echo '</tr>';
                        }
                    }
                    else 
                    {
                        echo "</br>" . mysql_errno() . ": " . mysql_error();
                    }
                }
                else
                {
                    echo "Connection error";
                }

            }

-----Working code:-----

//new code (working)
    public function GenerirajRandomMode()
        {   
            if($conn = new mysqli("localhost", "root", "", "modedb"))
            {   
                echo "Connection succesfull!";

                $qt = "SELECT * FROM mode ORDER BY RAND() LIMIT 1";
                $results = $conn->query($qt);
                if ($results)
                {
                    while ($row = $results->fetch_assoc())
                    {
                        echo '<tr>';
                        foreach($row as $field) 
                        {
                            echo '<td>' . htmlspecialchars($field) . '</td>';
                        }
                        echo '</tr>';
                    }
                }
                else 
                {
                    echo "</br>" . mysql_errno() . ": " . mysql_error();
                }
            }
            else
            {
                echo "Connection error!";
            }

        }
Dharman
  • 26,923
  • 21
  • 73
  • 125
ApuchYz
  • 1
  • 2
  • Your query is failing to run which is why the value of `$results` is not a resource, but a `bool` with a value of `false`. You will have to check the syntax of your query or check out `mysql_error()` such as `echo mysql_errno() . ": " . mysql_error()`. I would also be explicitly passing the mysql connection resource to your `mysql_query` as a second parameter rather than relying on using the most recent opened. – Lock Dec 30 '15 at 02:03
  • You should take note that you are using the `mysql_*` functions which are now deprecated. – Lock Dec 30 '15 at 02:04
  • Ok so I just updated my code and with `echo mysql_errno() . ": " . mysql_error()` I got this error "1046: No database selected". – ApuchYz Dec 30 '15 at 02:37
  • so the point in your code that you are calling `mysql_connect`, you need to also do `mysql_select_db('database_name_here')` – Lock Dec 30 '15 at 02:38
  • Ok, where should I type it? On the begenning of the first `if`? – ApuchYz Dec 30 '15 at 02:46
  • Why are you mixing `mysqli_*` functions with `mysql_*` functions? You are using 2 separate sets of libraries. You are making the initial connection with `mysqli` but then querying with `mysql`. Pick one library (`mysqli`) and stick with it. Read up on it here: http://php.net/manual/en/book.mysqli.php – Lock Dec 30 '15 at 02:48
  • Thank you so much that solved my problem and now I know where should I be more careful next time. – ApuchYz Dec 30 '15 at 03:22

1 Answers1

0

I suspect the query is failing. Check $results to see if you're getting what you expect

Check that you're actually connecting to the database.

Also, mysql_ is deprecated, you should use mysqli or PDO instead

Evan Graham
  • 278
  • 3
  • 8
  • Please read the text I posted above the code... I said that when I paste that query in phpmyadmin I get what I've expected. When I try to `echo $results` I get nothing and when I try to `print_r $results` I get this error "Parse error: syntax error, unexpected '$results' (T_VARIABLE) in C:\xampp\htdocs\dome.php on line 26". – ApuchYz Dec 30 '15 at 02:12
  • I did read your post. I didn't say the query was wrong, just that is was failing. $result returning nothing confirms that. Check that the database connection is correct and open. – Evan Graham Dec 30 '15 at 02:21