Jump to content

Function removed from PHP code?


SmexyPantsGnome

Recommended Posts

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 by SmexyPantsGnome
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);
    }
}
Link to comment
Share on other sites

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 by Jacques1
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.