I have 2 servers connected to the same network. A web server hosted on Linux Fedora
that contains the form that is accessed through the internet and another server that contains Windows Server 2008 R2
the SQL Server 2012
.
I need from a field to <select>
select a name and that name from the data it is connected to in the base de datos
.
With the code I have now it doesn't open the page, it just gives me a error 500
.
File: testsql.php
<?php
$q = intval($_GET['q']);
<?php
$q = intval($_GET['q']);
$serverName = "192.168.9.10:1433";
$conInfo = array ( "Database"=>"logytel", "UID"=>"sa", "PWD"=>"log159159");
$con = sqlsrv_connect($serverName, $conInfo);
if ($con) {
echo "Conexión establecida.<br>";
}else{
echo "La conexión no se pudo establecer.<br>";
die( print_r(sqlsrv_errors(), true));
}
sqlsrv_select_db($con,"ajax_demo");
$sql="SELECT * FROM prueba1 WHERE codigo = '".$q."'";
$getResults= sqlsrv_query($con, $sql);
if ($getResults == FALSE)
die(FormatErrors(sqlsrv_errors()));
while ($row = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC)){
echo ($row['SQL_VERSION']);
echo ("<br/>");
}
sqlsrv_free_stmt($getResults);
function FormatErrors( $errors );
{
/* Display errors. */
echo "Error information: <br/>";
foreach ( $errors as $error );
{
echo "SQLSTATE: ".$error['SQLSTATE']."<br/>";
echo "Code: ".$error['code']."<br/>";
echo "Message: ".$error['message']."<br/>";
}
}
$cantidad_encontrado = sqlsrv_num_rows($getResults);
sqlsrv_close($con);
if ($cantidad_encontrado > 0) {
// solo se pone los ; (punto y comas) al finalizar la sentencias, no entre lineas de tu echo.
echo "<table>
<tr>
<th>Código</th>
<th>Nombre</th>
<th>Apellidos</th>
<th>Proyecto</th>
</tr>"; //<- aqui termina la sentencia, lo que esta arriba es solo texto plano que el compilador interpreta como STRING y no como sentencia
while($row = sqlsrv_fetch_array($result)) {
echo "<tr>"; // en cada uno de estos puntos, debes de colocar el ;
echo "<td>" . $row['codigo'] . "</td>";
echo "<td>" . $row['nombre'] . "</td>";
echo "<td>" . $row['apellidos'] . "</td>";
echo "<td>" . $row['proyecto'] . "</td>";
echo "</tr>";
}
echo "</table>";
}else{
echo "<h1>NO ENCONTRE DATOS </h1>";
}
?>
<!DOCTYPE html>
<html>
<head>
<script>
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
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 (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Luis</option>
<option value="2">Nora</option>
<option value="3">Aron</option>
</select>
</form>
<br>
<div id="txtHint"><b>Selecciona tu nombre</b></div>
</body>
</html>
I tried another one, this time it lets me in, but when I select a name I get this error:
(index):20 GET http://logytel.es/formulario_sql/getuser.php?q=1 500 (Internal Server Error)
File: getuser.php
<!DOCTYPE html>
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
padding: 5px;
}
th {text-align: left;}
</style>
</head>
<body>
<?php
$q = intval($_GET['q']);
$serverName = "192.168.9.10:1433";
$conInfo = array ( "Database"=>"logytel", "UID"=>"sa", "PWD"=>"log159159");
$con = sqlsrv_connect($serverName, $conInfo);
if ($con) {
echo "Conexión establecida.<br>";
}else{
echo "La conexión no se pudo establecer.<br>";
die( print_r(sqlsrv_errors(), true));}
sqlsrv_select_db($con,"ajax_demo");
$sql="SELECT * FROM prueba1 WHERE codigo = '".$q."'";
$result = sqlsrv_query($con,$sql);
$cantidad_encontrado = sqlsrv_num_rows($result); // la funcion mysqli_num_rows() me dira el nro de lineas encontrada por tu QUERY
// quiere decir que encontro al menos una fila
sqlsrv_close($con); // una vez que termine de ejecutar TODO EL QUERY, debes de cerrar imendiatamente la conexion
if ($cantidad_encontrado > 0) {
// solo se pone los ; (punto y comas) al finalizar la sentencias, no entre lineas de tu echo.
echo "<table>
<tr>
<th>Código</th>
<th>Nombre</th>
<th>Apellidos</th>
<th>Proyecto</th>
</tr>"; //<- aqui recien termina la sentencia, lo que esta arriba es solo texto plano que el compilador interpreta como STRING y no como sentencia
while($row = sqlsrv_fetch_array($result)) {
echo "<tr>"; // en cada uno de estos puntos, debes de colocar el ;
echo "<td>" . $row['codigo'] . "</td>"; // en cada uno de estos puntos, debes de colocar el ;
echo "<td>" . $row['nombre'] . "</td>"; // en cada uno de estos puntos, debes de colocar el ;
echo "<td>" . $row['apellidos'] . "</td>"; // en cada uno de estos puntos, debes de colocar el ;
echo "<td>" . $row['proyecto'] . "</td>";
echo "</tr>";
}
echo "</table>";
}else{
echo "<h1>NO ENCONTRE DATOS </h1>";
}
?>
</body>
</html>
File: index.php
<script>
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
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 (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
}
</script>
<body>
<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Luis</option>
<option value="2">Nora</option>
<option value="3">Aron</option>
</select>
</form>
<br>
<div id="txtHint"><b>Selecciona tu nombre</b></div>
</body>
</html>
All the code is supposed to be fine. The sa user is the one used for remote connections from SQL Server
. It has the correct password, and the IP corresponds to that of the server. The port is the default (1433), with the ports open and with the TCP/IP protocol activated. It also has the Firewall exception enabled on both the port and the executable.
The error is that you use mysqli variables, you must use the sql server's own ones that start like this: sqlsrv
To make the connection I leave you an example of how you should do it:
In addition to @Lorenzo's answer, you probably need to install the sq SQL Server extension: https://www.php.net/manual/es/sqlsrv.installation.php
On the other hand, I recommend you to enable the error output in your PHP configuration to know why it generates a 500 error, and always develop with the error output enabled in your local environment.
You have syntax errors in the files and that is why you are getting 500 errors.
Try this new version of
testsql.php
. I don't have sql server installed, so I can't guarantee that it will work logically, but at least it shouldn't give you 500 errors anymore.