Jasmine
Jasmine

Reputation: 117

PHP error when connecting to MySQL database

I was trying to connect to my local database through the php code but I get this error:

Syntax error, unexpected '$result' (T_VARIABLE), expecting ',' or ')'

and I don't understand where's the problem. Here's my code:

    <?php
    $hostname = "localhost";
    $username = "root";
    $password = "";

    $databaseName = "newspage";

    $dbConnected = @mysqli_connect($hostname, $username, $password);

    $dbSelected = @mysqli_connect($databaseName, $dbConnected);

    $query = "INSERT INTO news(titolo, testo, data)VALUES('".$_POST["titolo"]."', '".$_POST["testo"]."', NOW())";
    $result = @mysqli_query($query);
    if(!$result){
    echo("Errore aggiunta news: " . mysqli_error(mysqli $result));
    exit();
    }

    else {
    mysqli_close(mysqli $dbConnected);
    echo('News caricata!<br><a href="add.php">Clicca qui</a> per aggiungere altre news.<br><a href="edit.php">Clicca qui</a> per apportare modifiche alle news.<br><a href="../index.php">Clicca qui</a> per tornare alla pagina principale.'); 
    }
?>

Upvotes: 1

Views: 53

Answers (2)

alanfcm
alanfcm

Reputation: 663

Your problem is in this line:

echo("Errore aggiunta news: " . mysqli_error(mysqli $result));

It should be:

echo("Errore aggiunta news: " . mysqli_error($result));

Upvotes: 1

Naveed Ramzan
Naveed Ramzan

Reputation: 3593

You should pass dbname too in mysqli_connect

$dbConnected = @mysqli_connect($hostname, $username, $password, $databaseName);

and to execute query.

$result = $dbConnected->query("select * from tablename");

Further your query should be:

    $query = "INSERT INTO news(titolo, testo, data) 
    VALUES('".$_POST['titolo']."', '".$_POST['testo']."', NOW())";

Upvotes: 0

Related Questions