SmexyPantsGnome Posted August 26, 2014 Share Posted August 26, 2014 (edited) Checking if php function mysqli_fetch_assoc() been removed from newer php versions. Running LAMP Apache 2.4.7 PHP 5.5.9 MySQL 5.5.38 Running WAMPserver Apache 2.4.9 PHP 5.5.12 MySQL 5.6.17 LAMP system, this code works fine: <?php session_start(); $_SESSION['dbhost'] = $_POST['dbhost']; $_SESSION['dbuser'] = $_POST['dbuser']; $_SESSION['dbpass'] = $_POST['dbpass']; $_SESSION['dbname'] = $_POST['dbname']; ?> <!store variables in a session> <html> <head> <link rel="stylesheet" href="style.css"> </head> <body> <?php if (!empty($_SESSION['dbname'])) { echo "Database connection settings are saved for ".$_SESSION['dbname'].".<br>"; $con = mysqli_connect($_SESSION['dbhost'],$_SESSION['dbuser'],$_SESSION['dbpass'],$_SESSION['dbname']); if (mysqli_connect_errno()) { die("Failed to connect to your database with the saved settings.<br>"); } else { echo "Successfully connected to the database.<br><br>"; } $sql = "SELECT count(*) FROM account"; $query=mysqli_fetch_assoc(mysqli_query($con,$sql)); $count = $query["count(*)"]; if ($count == 0) { echo "Detected your account has not been configured yet: <br>"; echo 'Click <a href ="setup_account.html">here</a> to configure.<br><br>'; } else { $sql = "SELECT ircnick FROM account"; $query=mysqli_fetch_assoc(mysqli_query($con,$sql)); $user = $query["ircnick"]; echo "Found your previous saved settings: <br>"; echo 'If you want to change the settings for '.$user.' click <a href="setup_account.html">here</a>.<br>'; } } else { echo "Error saving your settings.<br>"; } ?> But on WAMP get this output: (!) Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www\setup.php on line 44 Call Stack # Time Memory Function Location 1 0.0000 249936 {main}() ..\setup.php:0 2 0.0156 260184 mysqli_fetch_assoc() ..\setup.php:44 Trying to share my code with a friend who is using Windows. Edited August 26, 2014 by SmexyPantsGnome Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted August 26, 2014 Share Posted August 26, 2014 the error means that your query failed due to an error of some kind. you would need to use mysqli_error($con) to find out why. Quote Link to comment Share on other sites More sharing options...
SmexyPantsGnome Posted August 26, 2014 Author Share Posted August 26, 2014 I do believe it is caused when 'account' table doesn't exist yet. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted August 26, 2014 Share Posted August 26, 2014 Whatever caused this, the point is that you need to add some kind of error reporting to your code. There are two options: You can use the old way of manually checking the return value of every single function call. This is tedious as hell and will bloat your code beyond recognition. MySQLi can do the error reporting itself. I strongly recommend the second solution. All you need to do is activate error reporting in the MySQLi driver: $mysqli_driver = new mysqli_driver(); $mysqli_driver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT; This makes MySQLi throw an exception whenever a query fails. So instead of getting some cryptic error message when you try to fetch from a failed query, you now get the actual MySQL error, and the script terminates immediately. Quote Link to comment Share on other sites More sharing options...
SmexyPantsGnome Posted August 26, 2014 Author Share Posted August 26, 2014 Thanks, think solved ever possible senario: <?php session_start(); $_SESSION['dbhost'] = $_POST['dbhost']; $_SESSION['dbuser'] = $_POST['dbuser']; $_SESSION['dbpass'] = $_POST['dbpass']; $_SESSION['dbname'] = $_POST['dbname']; ?> <!store variables in a session> <html> <head> <link rel="stylesheet" href="style.css"> </head> <body> <?php if (!empty($_SESSION['dbname'])) { echo "Database connection settings are saved for ".$_SESSION['dbname'].".<br>"; $con = mysqli_connect($_SESSION['dbhost'],$_SESSION['dbuser'],$_SESSION['dbpass']); if (mysqli_connect_errno()) { die("Failed to connect to your database with the saved settings.<br>"); } else { //create database if it doesn't exist $sql="CREATE DATABASE ".$dbname; if (mysqli_query($con,$sql)) { echo "Database ".$dbname." created successfully<br>"; } //Get saved account info $con = mysqli_connect($_SESSION['dbhost'],$_SESSION['dbuser'],$_SESSION['dbpass'],$_SESSION['dbname']); $sql="SELECT count(*) FROM account"; if (mysqli_query($con,$sql)) { //found account table $query = mysqli_fetch_assoc(mysqli_query($con,$sql)); $count = $query["count(*)"]; if ($count == 0) { //account table is empty echo "Detected your account has not been configured yet: <br>"; echo 'Click <a href ="setup_account.html">here</a> to configure.<br><br>'; } else { //found previous account info $sql = "SELECT ircnick FROM account"; $query=mysqli_fetch_assoc(mysqli_query($con,$sql)); $user = $query["ircnick"]; echo "Found your previous saved settings: <br>"; echo 'If you want to change the settings for '.$user.' click <a href="setup_account.html">here</a>.<br>'; } } else { //account table does not exist $sql = "CREATE TABLE account(ircserver CHAR(6), ircnick CHAR(25), ircpass CHAR(99), ircchannel CHAR(25))"; if (mysqli_query($con,$sql)) { //create account table echo "Detected your account has not been configured yet: <br>"; echo 'Click <a href ="setup_account.html">here</a> to configure.<br><br>'; } else { //error with creating the table echo "Error configuring settings: ".mysqli_error($con)."<br>"; } } mysqli_close($con); } } Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted August 26, 2014 Share Posted August 26, 2014 (edited) Not really. There's a reason why I specifically advised against this approach. Printing the raw MySQL error message on the screen is a very bad idea. Not only does this leak internal information about your database system; it's also completely pointless, because the user cannot do anything useful with the information. I mean, what is an end user supposed to do with some internal MySQL error? They cannot fix it, can they? The MySQL messages are meant for you, the database administrator. When I take a closer look at your code, this all looks very dubious. So you hand out database admin accounts to other people and let them create their own database? Why would you do that? This is a major security risk and, again, absolutely pointless. You need one database. It seems you're repeating the same mistake that you already made at the beginning of the project: At first, you wanted to literally create a new folder for each user. When that turned out to be a bad idea, you've somehow decided to try the same thing with databases. That's just not how web applications work. You have one application with one database which holds all the user data. Your application may then render personalized content to create the illusion of separate user pages. But physically, it's just one application with one database. It might be a good idea to start with a smaller project and get familiar with the basics before you jump to the advanced stuff. For example, create a simple registration system which allows people to register, log-in and then view their personal information of a profile page. Edited August 26, 2014 by Jacques1 Quote Link to comment Share on other sites More sharing options...
SmexyPantsGnome Posted August 27, 2014 Author Share Posted August 27, 2014 Thanks for the input. Right now I am just trying to get code to work on a local computer, which seems all the coding syntax errors are fixed. I am looking over php example of "Simple login management" on various websites. I have no plans releasing this to a dedicated website yet. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted August 27, 2014 Share Posted August 27, 2014 I have no plans releasing this to a dedicated website yet. So? Does that mean you should take the wrong approach? Of course you're free to spend the next few weeks on an incorrect concept and then throw everything away and start from scratch. If you think that's a useful learning experience, go ahead. But if you want to save time, you should fix your mistakes now and not build a whole application on top of them. 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.