kyme Posted April 27, 2017 Share Posted April 27, 2017 (edited) Hi, I was just playing php and try to check if the database is empty. I want to check the DB if it's empty. And if it's not, the button will hide for "Insertion btn". I made some basic understanding of the structure of the code. I don't have a clue of how it will hides/shows the button once the database was checked if it's empty or not. Some comment says that I should use JS to hide and show the button. But I don't have a clue if how to intergrate the btn hide() and show(). What statement should I add to enable to check the db and hide/show the btn? <!DOCTYPE html> <html> <body> <form action="mypractice.php" method="post" enctype="multipart/form-data"> Message: <textarea name='message' rows='3' cols='10'> </textarea><br><br> <button id="insert" type="submit" value="insert sample" name="submit">insert sample</button> </form> <?php //My DB connection require_once('config.php'); //Checks if DB is empty if(empty($conn)) { shows the button if db is empty } else { hides the button if db is not empty } ?> </body> </html> Edited April 27, 2017 by kyme Quote Link to comment Share on other sites More sharing options...
glenelkins1984 Posted April 27, 2017 Share Posted April 27, 2017 Hi, I was just playing php and try to check if the database is empty. I want to check the DB if it's empty. And if it's not, the button will hide for "Insertion btn". I made some basic understanding of the structure of the code. I don't have a clue of how it will hides/shows the button once the database was checked if it's empty or not. Some comment says that I should use JS to hide and show the button. But I don't have a clue if how to intergrate the btn hide() and show(). What statement should I add to enable to check the db and hide/show the btn? <!DOCTYPE html> <html> <body> <form action="mypractice.php" method="post" enctype="multipart/form-data"> Message: <textarea name='message' rows='3' cols='10'> </textarea><br><br> <button id="insert" type="submit" value="insert sample" name="submit">insert sample</button> </form> <?php //My DB connection require_once('config.php'); //Checks if DB is empty if(empty($conn)) { shows the button if db is empty } else { hides the button if db is not empty } ?> </body> </html> Hey, What do you mean if the db is empty? You mean empty of tables? Or do you mean if a specific table is empty? The code if(empty($conn)) - I am assuming $conn is a resource of a database connection right? Checking if that variable is empty isn't going to tell you if the database is empty of tables or anything like that, you would need to run a mysql query to check for tables. A query like this would do the trick: SELECT COUNT(*) AS TableCount FROM information_schema.tables WHERE table_schema = 'dbo' and TABLE_TYPE='BASE TABLE' So you would run this query, fetch the row and check TableCount > 0 Quote Link to comment Share on other sites More sharing options...
kyme Posted April 27, 2017 Author Share Posted April 27, 2017 Hey, What do you mean if the db is empty? You mean empty of tables? Or do you mean if a specific table is empty? The code if(empty($conn)) - I am assuming $conn is a resource of a database connection right? Checking if that variable is empty isn't going to tell you if the database is empty of tables or anything like that, you would need to run a mysql query to check for tables. A query like this would do the trick: SELECT COUNT(*) AS TableCount FROM information_schema.tables WHERE table_schema = 'dbo' and TABLE_TYPE='BASE TABLE' So you would run this query, fetch the row and check TableCount > 0 When the table is empty. It will check the table and if it's empty the btn for insertion is visible. Quote Link to comment Share on other sites More sharing options...
glenelkins1984 Posted April 28, 2017 Share Posted April 28, 2017 When the table is empty. It will check the table and if it's empty the btn for insertion is visible. Ok, so if you need to check a specific table is empty you can just run simple mysql query, for example: SELECT COUNT(*) AS TotalRows FROM `table` It's difficult to say exactly how to modify your code as i can't see the full extent of it, but i would do this using PDO like this: $query = $dbOBJ->prepare("SELECT COUNT(*) AS TotalRows FROM `table`"); $query->execute(); if($query->fetch(PDO::FETCH_OBJ)->TotalRows >0){ } Quote Link to comment Share on other sites More sharing options...
benanamen Posted April 28, 2017 Share Posted April 28, 2017 What are you trying to do that a database table is only going to ever have one row or no row? Quote Link to comment Share on other sites More sharing options...
kyme Posted May 1, 2017 Author Share Posted May 1, 2017 Ok, so if you need to check a specific table is empty you can just run simple mysql query, for example: SELECT COUNT(*) AS TotalRows FROM `table` It's difficult to say exactly how to modify your code as i can't see the full extent of it, but i would do this using PDO like this: $query = $dbOBJ->prepare("SELECT COUNT(*) AS TotalRows FROM `table`"); $query->execute(); if($query->fetch(PDO::FETCH_OBJ)->TotalRows >0){ } So I did this but this is my structure. $query = $conn->prepare("SELECT COUNT(*) AS TotalRows FROM `myTableName`"); $query->execute(); if($query->fetch(PDO::FETCH_OBJ)->TotalRows >0){ echo Btn insert shows } else{ echo btn insert hides } So basically what I want to do is to check the table if it's empty or not. If empty, it shows the button to insert. And if it's not empty, the button(insert) is not visible. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted May 1, 2017 Share Posted May 1, 2017 And why would you want this? Only allowing a single row is a rather strange goal. It's also unclear how exactly you want to enforce that. If there are multiple users or even just one user with multiple tabs/windows/browsers, you'll quickly run into a scenario where the page is initially loaded with no rows present (i. e. the button is visible), but then a row does get inserted outside of the current page. With your code, the insertion button is still visible and probably functional as well. If there is no meaning behind this all, and you're just practicing, then you might want to choose a more realistic task. Quote Link to comment Share on other sites More sharing options...
glenelkins1984 Posted May 1, 2017 Share Posted May 1, 2017 So I did this but this is my structure. $query = $conn->prepare("SELECT COUNT(*) AS TotalRows FROM `myTableName`"); $query->execute(); if($query->fetch(PDO::FETCH_OBJ)->TotalRows >0){ echo Btn insert shows } else{ echo btn insert hides } So basically what I want to do is to check the table if it's empty or not. If empty, it shows the button to insert. And if it's not empty, the button(insert) is not visible. This depends on what you're using to connect to the database, is $conn a PDO object or a mysql connection resource? The code I used was just an example i would use with PDO Quote Link to comment Share on other sites More sharing options...
benanamen Posted May 1, 2017 Share Posted May 1, 2017 And why would you want this? @Jaques1 and I both asked the obvious question, why, yet the OP hasn't answered. I believe there is no real and logical reason to do this. Unless the OP shows otherwise, I would file this as an XY problem or worse. It just makes no sense. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.