1

I wrote a code in PHP to connect with Mysql database and add data into it.. code is given below:

$dbhostname = "localhost";
$dbusername = "root";
$dbpassword = "root";
$dbname = "rms_invoice";

function showDBMessage(){
    if($_POST["name"] != NULL){
        //came from register page
        if(register()){
            print("<br/>Registered successfully!!!");
        } else {
            print("<br/>Registration failed!!!");
        }
    }
}
function register(){
    global $dbhostname,$dbusername,$dbpassword,$dbname;
    // Create connection
    $con=mysqli_connect($dbhostname,$dbusername,$dbpassword);
    // Check connection
    if (!mysqli_connect_errno()) {
        print("MYSQL Connection established...<br/>");
        echo "<br/>Successful: ".mysqli_get_host_info($con);
        $selected = mysqli_select_db($con, $dbname);
        if(false!==$selected){
            $name = mysqli_real_escape_string($con, $_POST["name"]);
            $username = mysqli_real_escape_string($con, $_POST["username"]);
            $password = mysqli_real_escape_string($con, $_POST["password"]);
            $address = mysqli_real_escape_string($con, $_POST["address"]);

            $insertUserQuery="INSERT INTO USER(name,username,password,address) VALUES ('$name','$username','$password','$address')";
            echo $insertUserQuery;
            $result = mysqli_query($con, $insertUserQuery);
            if ( false===$result ) {
                printf("<br/>mysqli_connect_error: %s\n", mysqli_connect_error());
                printf("<br/>mysqli_error: %s\n", mysqli_error($con));
            } else {
                echo "<h4>New User Added!!!</h4><br/>";
                return true;
            }
        }else{
            printf("<br/>Error while selecting database, error:%s\n",mysqli_errno($con));
        }
    } else {
        print("<br/>Failed to connect to MySQL: "+mysqli_connect_error());
    }
    mysqli_close($con);
    return false;
}

and the output is:

MYSQL Connection established...
Successful: localhost via TCP/IP
INSERT INTO USER(name,username,password,address) VALUES ('nitin','ndthokare','p','beed')
mysqli_connect_error: 
mysqli_error: No database selected 
Registration failed!!!

It is getting connected but database selection seems to be problem. (same query executes successfully through mysql cli).

I tried many possible corrections in code discussed on SO, but could not succeed.

Can anybody help me to find out the mistake?

2
  • 2
    To be sure, "rms_invoice" is the correct database name, right? Commented Apr 28, 2014 at 4:37
  • Please read this - php.net/manual/mysqli.quickstart.prepared-statements.php. Injecting values directly into your SQL statements is unsafe Commented Apr 28, 2014 at 5:05

1 Answer 1

2
 if(false!==!$selected){

is extremely unusual operator (called "double negative") means you want to execute the following code block only if $selected is FALSE. to make it much less confusing, write it without all these magic chants

 if($selected){

or even omit this useless check completely.

Sign up to request clarification or add additional context in comments.

5 Comments

Hmm, I do agree the double NOT is a definite weird but in actual that if block is getting executed, so this in itself cannot be the complete answer, maybe you should address more.
@ICanHasCheezburger The if block is being executed because $selected is FALSE meaning mysqli_select_db failed. This answer is completely correct
Ya.. that was actually mistake by overlook... I it was if(false!==$selected) or if(true===!$selected)...
but now the error is.. MYSQL Connection established... Successful: localhost via TCP/IP... Error while selecting database, error:1046 ... Registration failed!!!
that's quite obvious and self-explained one

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.