Join the Stack Overflow Community
Stack Overflow is a community of 6.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

This question already has an answer here:

I'm trying to link an SQL database into an app, and the tutorial I was following had this SQL code in it. When I replaced the placeholders and ran it, it gives me the above error. Here is the code (the database name, username and table name have been replaced, except the relevant part). The real password does end in an exclamation mark too.

<?php

// Create connection
$con=mysqli_connect(“localhost”, ”databasename”, ”password!”, ”tablename”);

// Check connection
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// This SQL statement selects ALL from the table ‘Recipes’
$sql = "SELECT * FROM Recipes";

// Check if there are results
if ($result = mysqli_query($con, $sql))
{
    // If so, then create a results array and a temporary one
    // to hold the data
    $resultArray = array();
    $tempArray = array();

    // Loop through each row in the result set
    while($row = $result->fetch_object())
    {
        // Add each row into our results array
        $tempArray = $row;
        array_push($resultArray, $tempArray);
    }

    // Finally, encode the array to JSON and output the results
    echo json_encode($resultArray);
}

// Close connections
mysqli_close($result);
mysqli_close($con);
?>

The full error is:

Parse error: syntax error, unexpected '!' in /home/content/93/9076293/html/babyfood/service.php on line 4

Edit: The below answer worked, but it opened up a new error:

Warning: mysqli_connect() [function.mysqli-connect]: (28000/1045): Access denied for user     'databasename'@'184.168.46.96' (using password: YES) in     /home/content/93/9076293/html/babyfood/service.php on line 4
Failed to connect to MySQL: Access denied for user 'databasename'@'184.168.46.96' (using     password: YES)
Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in /home/content/93/9076293/html/babyfood/service.php on line 16

Warning: mysqli_close() expects parameter 1 to be mysqli, null given in /home/content/93/9076293/html/babyfood/service.php on line 36

Warning: mysqli_close() expects parameter 1 to be mysqli, boolean given in /home/content/93/9076293/html/babyfood/service.php on line 37
share|improve this question

marked as duplicate by deceze, FuzzyTree, Fred -ii-, mario php Jun 11 '14 at 16:18

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3  
You're using funny quotes in mysqli_connect(). Change them to "normal" ones. This usually happens when you copy and paste code from a website. – John Conde Jun 11 '14 at 16:08
    
Your parameters are misguiding as well mysqli_connect('localhost', 'my_user', 'my_password', 'my_db'); – Ian Jun 11 '14 at 16:11
2  
Smart/curly quotes “ ”. Beautiful, yet deadly. – Fred -ii- Jun 11 '14 at 16:14
up vote 3 down vote accepted

You're using smart/curly quotes “ ” instead of standard quotes " in the connection section.

It should be as follows:

$con=mysqli_connect("localhost", "databasename", "password!", "tablename");
share|improve this answer
    
This got rid of that problem, but it opened up a new one. Same code, just this change (replacing databasename, password and table name). It's too long to put into a comment, so I'm editing it back into the question. – Nick Martino Jun 11 '14 at 16:49
    
Nick Marino your database can't connect due to incorrect password. – Ian Jun 11 '14 at 18:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.