-1

This is my PHP code :

<html>
    <head>  
        <title>SQL DB Management</title>
    </head>
    <body>
        <h1>MySqlAdmin</h1>
        <table>
            <tr>
                <th><!--Heading 1--></th>
                <th><!--Heading 2--></th>
                <th><!--Leave it blank--></th>
            </tr>
        <?php
            $db_host = "mysql.freehostingnoads.net";
            $db_username = "";
            $db_password = "";
            $db_name = "";
            $db_table = "";
            $db = mysqli_connect($db_host, $db_username, $db_password, $db_name);
            if (mysqli_connect_errno()){die("Failed to connect to MySQL: " . mysql_connect_error());}
            $content = mysqli_query($db,"SELECT * FROM " . $db_table);

            while($line = mysqli_fetch_array( $content )){
                echo("<tr>");
                echo("<td>" . $line['c1'] . "</td>");
                echo("<td>" . $line['c2'] . "</td>");
                #delete button
                echo("<td><form action='del.php' method='GET'><input type='text' name='c1' value='" . $line['c1'] . "' /><input type='text' name='c2' value='" . $line['c2'] . "' /><input type='button' value='DEL' /></form></td>");
                echo("</tr>");
            }
        ?>
    </body>
</html>

And simply skip reading the <head>, this is the warning I received:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/u273577101/public_html/index.php on line 23

I have searched but cannot find the answer. What's the problem here?
There is no connection error, I guess. It doesn't seem to give me any error message about connecting.

Tool Box
  • 69
  • 11
  • 1
    are you aware that `$db_table` is evaluated as an empty string in this snippet? – bruchowski Apr 22 '14 at 06:37
  • check whether the query is working fine by echoing the query. – Jenz Apr 22 '14 at 06:37
  • `$db_table = "";` table name is blank, also, read the loooooooooooong list of similar question on the right hand side column which says **RELATED** – Mr. Alien Apr 22 '14 at 06:37
  • sorry, sorry. i forgot to mention that there is NO error for the table name. i added it but cleaned it here. i don't want people to know it. also, is using the table name `table` okay? – Tool Box Apr 22 '14 at 06:38
  • You do not do any error detection or handling in your code when running the query. So you don't see that mysql tells you that something is wrong with your syntax. – arkascha Apr 22 '14 at 06:38
  • You must have to write a valid db_table name, you are not allow to leave it blank, or such a name that table dosen't exist. Must write a Valid table name. – Keyur Mistry Apr 22 '14 at 06:42
  • @Mr.Alien : what are aliens doing in `PHP` :p – NoobEditor Apr 22 '14 at 06:50
  • @NoobEditor Am a server side programmer as well ;) – Mr. Alien Apr 22 '14 at 06:51
  • @ToolBox using `table` as the name of your table is ***not*** okay, as `table` is a reserved keyword. You *can* use it, but need to quote it anywhere you use it. also it may add confusion for a reader of your source code. Just don't do it. And: most likely this is the cause of your error, as you didn't put `$db_table` in SQL quotes. – Kaii Apr 22 '14 at 07:05

3 Answers3

0

mysqli_query is returning boolean FALSE because your $db_table variable is set to an empty string and so the resulting query you are giving it is invalid.

You simply need to set the $db_table variable to be a valid table name.

flagoworld
  • 3,177
  • 2
  • 19
  • 16
0

mysqli_query returns false on failure, so there is something wrong with your code or server. Please make sure the table exists and the table's name is not a keyword in MySQL.

us3.php.net/mysqli_query

Louis Matthijssen
  • 496
  • 1
  • 6
  • 18
0

Sorry, that was not the problem.

The problem is that I assigned the value "table" for $db_table and it might be a key word.

Thank you for your help.

Tool Box
  • 69
  • 11