Error in Database Connection

时间:2015-06-26 09:35:49

标签: php database phpmyadmin connection

I have created a phplogin name database but it is giving me an error as

Parse error: syntax error, unexpected '=' in C:\xampp\htdocs\Login\log.php on line 7

I am using Xamp server having version xampp-win32-1.7.7-VC9-installer

<?php
    $username=$_POST['username'];
    $password=$_POST['password'];

    if($username&&$password)
    {
        connect = mysql_connect("localhost","root","root") or die ("couldn't connect");
        mysql_select_db("phplogin") or die ("Couldn't find");
    }
    else
    {
        echo "you are in";
    }
?>

3 个答案:

答案 0 :(得分:1)

Missing $. connect is a variable & it need to be defined as $connect. Should be -

$connect = mysql_connect(...

答案 1 :(得分:0)

As @b0s3 wrote you missed $ before connect how ever since I believe you are trying to learn mysql connection i would suggest you to you PDO with prepare statements since it's much more safer to use and mysql_connect is getting old now.

You can find more about PDO prepare statements at http://www.w3schools.com/php/php_mysql_prepared_statements.asp

Good luck :)

答案 2 :(得分:0)

Let's make a little edit here:

<?php

$username=$_POST['username'];
$password=$_POST['password'];

if(isSet($username) && isSet($password))
{
   $connect = new mysqli("localhost","root","root","phplogin");
}
else if() //some conditional here
{
  //some instructions
}

?>

use

isSet()

to check variable content.

Also you can use (when previous conditional statement returned TRUE) something like

if(trim($username)!='' && trim($password)!='')

to check if something was post. There is an empty() method too.

It's strongly recommended to use object-oriented style. Then you are able to comfortable using any mysqli methods and code is clear, readable etc. Forget about procedural style in PHP (in most cases).