0

I'm trying to host this php file on my website to connect to a MySQL database.

<?php

// Create connection
$con=mysqli_connect("localhost","username","pass","database");

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

// This SQL statement selects ALL from the table 'Locations'
$sql = "SELECT * FROM Locations";

// 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($con);
?>

And when I try to validate the code using http://writecodeonline.com/php/ it says

Fatal error: Call to undefined function mysqli_connect() on line 2

Searching around said I needed to change the php.ini file but I don't even have one hosted on my site. Is there something wrong with the code?

When I try to access it at www.mydomain.com/service.php it says file not found error... but it's definitely there. I'm working with this tutorial - http://codewithchris.com/iphone-app-connect-to-mysql-database/

1
  • 1
    Testing php on a website that does not belong to you will not allow you to connect to a/the/their database. If you want to test server-side scripts I would recommend installing wamp server. I'm finding it hard to understand your question as you seem to be answering it in the question. I hope this post helps. Commented Jan 30, 2015 at 18:19

1 Answer 1

1

You need to install the php-mysql dependency on your server.

yum install php-mysql -y

or your equivalent on your os.

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

2 Comments

If you look at that tutorial, they didn't install anything and it worked. Which is why I'm confused. I'm using the same website for hosting the database (Bluehost). Shouldn't it already have that?
There is nothing really wrong with your code. but that's what that error means.

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.