Ben P. Dorsi-Todaro
Ben P. Dorsi-Todaro

Reputation: 321

Connect to PHP MySQL with Javascript from external server

Okay here is the problem. I have the following PHP code.

    <?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM Persons");

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysqli_close($con);
?>

Which I got off of W3Schools and works fine when on the server that the database is on. However what I want to do is display that on another site using the following Javascript.

    function displaydata()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("datalist").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","phpscripthere.com/myphp.php",true);
xmlhttp.send();
}

The above javascript works but only when it is on the server that the database is on. What I need is to get the Javascript to work on another location while the PHP can still be stored on the main server.

Upvotes: 1

Views: 5205

Answers (4)

Volkan
Volkan

Reputation: 2210

Please do not mark this as answer, since it is not.

But this is rather recommendations.

This is w3Schools virus spreading....

$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

In this script processing continues even when the connection fails.

You should incorporate more failure checking and exit on them like this:

if (mysqli_connect_errno())
{
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
   exit(0);
}

Also you do not know if

$result = mysqli_query($con,"SELECT * FROM Persons");

query is successfull.

Apart from these pitfalls

You should better collect your data for the page and encode it into JSON and send to the client.. as Follows:

$r = array();
while(true){
    $row = mysqli_fetch_array($result);
    if(!$row)break;
    $r[] = json_encode((object)$row);
}
echo json_encode($r);

forming [  {"firstName":"John", "lastName": "Smith"}, 
           {"firstName":"Abraham", "lastName":"Lincoln"}, ...etc ]

then upon receiving decode the data in js and inject to your page.

And of course use ajax cross domain requests (less secure) or make your server talk to the others (more secure) to solve your problem.

Upvotes: 4

Zlatko
Zlatko

Reputation: 19578

If you're learning this stuff off of W3Schools, it may sound just a little little bit complicated, but don't be discouraged :)

Anyway, like @LoneWOLFs said in a comment, you could probably also set up another PHP page on the same server where your JavaScript comes from, and make that page call on the other remote server.

Basically, your new PHP script would do the similar as the old one - call some data from some remote location. And then it would pass that data back to the browser that called it.

Upvotes: 1

Gurmeet
Gurmeet

Reputation: 3314

Try to make a request using $.ajax with cross domain true like this:

$.ajax({
type: "GET",
crossDomain:true,
url: "phpscripthere.com/myphp.php"
});

Upvotes: 2

Victor
Victor

Reputation: 1479

You need make cross domain request https://stackoverflow.com/search?q=javascript+cross+domain (some of them AJAX cross domain call )

Upvotes: 3

Related Questions