display data from a selectbox on sql server

1

As my question says, I need to know how to show data when selecting an option of a selectbox.

My code is as follows and I use SQL SERVER (it's worth saying, since most tutorials are for mysql)

<?php
define('DB_SERVER','x.x.x.x');
define('DB_NAME','mydb');
define('DB_USER','sa');
define('DB_PASS','');

$conectar = mssql_connect(DB_SERVER,DB_USER,DB_PASS);
$conn = mssql_select_db(DB_NAME);


if( !$conn ) {
    echo "No se puede conectar a la Base de Datos.<br />";
    die( print_r( sqlsrv_errors(), true));
}

$query = 'SELECT EntregaRendirCodigo FROM EntregasRendir';

$result = mssql_query($query);
if (!$result) 
{
    $message = 'ERROR: ' . mssql_get_last_message();
    return $message;
}
else
{
    $i = 0;
    echo '<html><body><select name="listbox" size="20">';
    while ($i < mssql_num_fields($result))
    {
        $meta = mssql_fetch_field($result, $i);
        echo '<td>' . $meta->EntregaRendirCodigo . '</td>';
        $i = $i + 1;
    }
    echo '</tr>';

    while ( ($row = mssql_fetch_row($result))) 
    {
        $count = count($row);
        $y = 0;
        echo '<tr>';
        while ($y < $count)
        {
            $c_row = current($row);
            echo '<option>' . $c_row . '</option>';
            next($row);
            $y = $y + 1;
        }
        echo '</tr>';
    }
    mssql_free_result($result);

    echo '</select></body></html>';
}
?>

I am new to this, and if you could give me a tutorial, it would be very helpful.

    
asked by Eduardo Leon 28.12.2018 в 01:31
source

1 answer

1

If you are new do not complicate things much. Use something simple and as you go forward, go modifying and updating the code.

Tabla articulos
id          nombre
------------------------
1           Soldador de Estaño
2           PC
3           Teclado

<?php
try {
   $conn = new PDO('mysql:host=localhost;dbname=pruebas', 'root', '');
   $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
   catch(PDOException $e){
      echo "ERROR: " . $e->getMessage();
}

if(isset($_POST['estado'])){
$estado = $_POST['estado'];
echo 'Usted eligio: '. $estado.'<br><br>';
}
?>
<form action="select.php" method="post">
<select name="estado">
<option value="">Articulos</option>
<?php
$sql = $conn->query("SELECT * FROM articulos");
while ($row = $sql->fetch()) {
?>
<option value="<?=$row['nombre'];?>"><?=$row['nombre'];?></option>

<?php
}
?>
</select>
<input type="submit" value="Consulta">
</form>

as a result:

I hope I helped you

    
answered by 28.12.2018 / 01:55
source