I have yet problem with combobox and query.
COMBOBOX PAGE
This page work, I have just one problem:
With this code : " $sql ="select name from [db]..sysobjects where
xtype = 'U'order by
name";" in my combobox appear the name of all the tables of the db
but I want only few tables. how i can change this code?
//----------------------------------------------------------------------------------------------------------------
Code: Select all
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$dbname = "[aa]";
$link = mssql_connect("aa", "ll", "ll")or die("Could not connect: " .
mssql_error());
mssql_select_db($dbname) or die('Could not select database');
?>
<html>
<head></head>
<body>
<?php
// SELECT FUNZIONANTE MA ESTRAE TUTTI I CAMPI
$sql ="select name from [db]..sysobjects where xtype = 'U'order by
name";
// processi la stringa $result = mssql_query($sql);
//verifichi il risultato se diverso da 0. Se fallisce significa che
non ci sono tabelle nel db
if (!$result) {
echo "DB Error, could not list tables\n";
echo ('MsSQL Error: ' . mssql_error());
exit;
}
?>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="171">Select table </td>
<td width="620">codice</td>
</tr>
<tr>
<td><form name="myform" method="post" action="page2.php">
Tables: <select name="category" onChange="submit()">
<option value=""></option>
<?php
// cicli per estrarre tutti i nomi delle tabelle e crei direttamente
il form di scelta.
while ($row = mssql_fetch_row($result)) {
echo "<option value=\"".$row[0]."\">".$row[0]."</option>";
}
mssql_free_result($result);
?>
</select>
</form>
</td><td> </td>
</tr>
<tr><td> </td><td> </td>
</tr>
</table>
</body>
</html>
PAGE 2.PHP
-----------------------------
Code: Select all
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$dbname = '[DB]';
$link = mssql_connect(dd, aa, aa)or die("Could not connect: " .
mssql_error());
mssql_select_db($dbname) or die('Could not select database');
if(isset($_POST['nome'])) {
$azienda = $_POST['nome'];
if($azienda == '[Telephony Daily A]'){
$sql = "SELECT campo1, campo2, campo3, campo4, campo5 from
$azienda";
}else if($azienda == "[Telephony Daily B]"){
$sql = "SELECT campo1, campo3, campo7, campo9, campo11 from
$azienda";
}else{
echo "errore nella definizione del nome dell'azienda'";
}
echo $sql;
}
?>
<html>
<head>
</head>
<body>
<?php
$result = mssql_query($sql);
//verifichi il risultato se diverso da 0. Se fallisce significa che
non ci sono tabelle nel db
if (!$result) {
echo "DB Error, could not list tables\n";
//echo 'MsSQL Error: ' . mssql_error();
exit;
}
?>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr><td><div align="center">name/address</div></td><td><div
align="center">department/surname</div></td><td><div
align="center">field/history</div></td></tr>
<?php
// cicli l'array dei record per ircavare i dati
while($row = mssql_fetch_row($result)){
echo "<tr><td><div>s".$row[0]."</div></td><td><div>".$row[1]."</
div></
td><td><div>
".$row[2]."</div></td></tr>";
}
mssql_free_result($result);
msysql_close($link);
?>
</table>
</body>
</html>
?>
Those Are The Errors for the page2.php
Notice: Undefined variable: sql in C:\Program Files\xampp\htdocs
\intranet\page2.php on line 35
Warning: mssql_query() [function.mssql-query]: Query failed in C:
\Program Files\xampp\htdocs\intranet\page2.php on line 35
DB Error, could not list tables
The line 35 is this: $result = mssql_query($sql);
Thanks Lidia