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
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:08mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
– Ian Jun 11 '14 at 16:11“ ”
. Beautiful, yet deadly. – Fred -ii- Jun 11 '14 at 16:14