How to disable button based on sql query?

April 13, 2019, at 5:30 PM

I need to disable or hide, button if existsusername in the table, and == logged in user

For example: username John exists in table paym we should disable button to John

table: paym

 ID        username        column1      column2  
|   1   |  John       |  Value      |    Value  |
|   2   |  Alex       |  Null       |    Null   |

May be some syntax error or error in Query??

  $sql = "SELECT * FROM paym WHERE username = '" . $_SESSION['username'] . "'and column1 IS NOT NULL ";
  $result = mysqli_query($connection, $sql);
       if($result === true) {
        echo "<button class='btn btn-primary' disabled='disabled' name='add_box'>Add Box</button>";
    } else {
        echo "<button class='btn btn-primary' type='submit' name='add_box'>Add Box</button>";
Answer 1

You should check for number of rows matched by query, so your condition should be

if($result !== false && $result->num_rows > 0)

because $result is only false when there was an error.

If your query is

SELECT * FROM paym WHERE username = 'not existing user' and column1 IS NOT NULL

mysqli_query will return mysqli_result not false, even if there are no rows matched.

Answer 2

As mentioned in the comments, please make sure to read the documentation for functions you use, so you understand what they do, what they return, and how you can use that. is pretty clear on what it returns, and explains why what you've written cannot ever work: the function only ever returns either false, or a mysql_result object. It will never return true, so you can't test against that.

Of course, you can absolutely check against false, because that is a value it returned, if nothing was found:

if ($result !== false) {
  // generate button

but remember that $thing !== false in no way implies that $thing === true.

Answer 3

Here mysqli_query returns mysqli_result object:

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE. Here

instead you can count row

UNION 3 Identical Tables and SUM 4 Columns

UNION 3 Identical Tables and SUM 4 Columns

I have 3 tables on my database that are identical in structure

How to save a page (only) data with N tables in database

How to save a page (only) data with N tables in database

So, there is a form which is going to be filled every day by users and there is N tables in this formSince every table has a special size and a lot of nested headers, I had to save their headers in a separate table which is a faster and easier way to handle

How to reduce PostgreSQL database size?

How to reduce PostgreSQL database size?

I was planning to move from MySQL to PostgreSQL because I wanted to make use of TimescaleDB

mysql select statement help needed

mysql select statement help needed

mysql Table column names: