How do I make this php code a dropdown menu in html? - php

This is what my php code looks like:
<html>
Client list:<br><br>
<?php
$serverName = "NEPTUNE\SQLEXPRESS";
$connectionOptions = array("Database"=>"tcd", "UID"=>"tcd", "PWD"=>"technician");
$conn = sqlsrv_connect( $serverName, $connectionOptions);
if( $conn === false ) {
die( FormatErrors( sqlsrv_errors() ) );
}
$sql = "SELECT client_name FROM client";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
}
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC) ) {
echo $row[0]."<br />";
}
sqlsrv_free_stmt( $stmt);
?>
</html>
This returns a successful query. How do I select one of the query options from a drop down menu using the select tag in HTML?

If there's nothing wrong with the fetching, just combine it with HTML select + option tags as you normally would do
<html>
Client list:<br><br>
<?php
$serverName = "NEPTUNE\SQLEXPRESS";
$connectionOptions = array("Database"=>"tcd", "UID"=>"tcd", "PWD"=>"technician");
$conn = sqlsrv_connect( $serverName, $connectionOptions);
if( $conn === false ) {
die( FormatErrors( sqlsrv_errors() ) );
}
$sql = "SELECT client_name FROM client";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
}
?>
<select name="whatever">
<?php while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC) ): ?>
<option value="<?php echo $row[0]; ?>"><?php echo $row[0]; ?></option>
<?php endwhile; ?>
</select>
<?php sqlsrv_free_stmt( $stmt); ?>
</html>
Note: Just make sure you wrap this in a form (with the usual, form method post / get, buttons, etc.). The semantics is yours to decide.

Related

MS SQL statements work in DBeaver but not in PHP (with configuration and setup correct)

I have the following code to verify that my PHP-MS SQL connection and configuration are setup properly. I have about 100 getter functions that are all set up the same to retrieve info from this database:
function ms_getClientLN($repEnvKey) {
$repCustKey = '';
$serverName = "1.1.1.1\\EDGESQL"; //serverName\instanceName
$connectionInfo = array( "Database"=>"EDGESQL", "UID"=>"userid", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
//echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
$sql = "SELECT repCustKey FROM repair WHERE repEnvKey LIKE '$repEnvKey';";
//echo $sql . "<br>";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
} else {
#echo "0 results";
}
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
$repCustKey = $row['repCustKey'];
}
sqlsrv_close( $conn );
//----------===== Get ClientLast from customer table =====----------
$serverName = "1.1.1.1\\EDGESQL"; //serverName\instanceName
$connectionInfo = array( "Database"=>"EDGESQL", "UID"=>"userid", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
$clast = '';
if( $conn ) {
//echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
$sql = "SELECT cuLastName FROM customers WHERE cuKey LIKE '$repCustKey';";
//echo $sql . "<br>";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
} else {
#echo "0 results";
}
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
$clast = $row['cuLastName'];
}
sqlsrv_close( $conn );
return $clast;
}
I then have the following function formatted exactly like the other working hundred that produces no results:
function ms_getRepairHistory($rhRepKey) {
$type = '';
$results[] = array();
$serverName = "1.1.1.1\\EDGESQL"; //serverName\instanceName
$connectionInfo = array( "Database"=>"EDGESQL", "UID"=>"userid", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
//echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
$sql = "SELECT rhWhen, rhWhereNext, rhNotes FROM RepairHistory WHERE rhRepKey LIKE '$rhRepKey';";
echo $sql . "<br>";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
} else {
echo "0 results<br>";
}
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
$results[] = $row;
}
sqlsrv_close( $conn );
return $row;
}
I'm even echoing the SQL command and can copy and paste it into DBeaver:
echo $sql . "<br>";
outputs:
SELECT rhWhen, rhWhereNext, rhNotes
FROM RepairHistory
WHERE rhRepKey LIKE '001-167896-001';
Which works in DBeaver with the
Databse.dbo.
prefix attached:

how to print counted sql query using php

I try to count the event-code. What are the changes I need to do to achieve it?
Please help me with that.
My code:
$serverName = "IE3PDT1QJ67P4";
$connectionInfo = array( "Database"=>"TestEventsDB");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}
else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
$sql = "SELECT count(EventCode) FROM AlarmPointEvent where AlarmPoint=24001";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
}
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
echo $row['EventCode']."<br>";
}
sqlsrv_free_stmt( $stmt);
?>
Do some change here
$sql = "SELECT count(EventCode) as total_event FROM AlarmPointEvent where AlarmPoint=24001";
and here
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
echo $row['total_event']."<br>";
}
You will set a alies name for count,
$serverName = "IE3PDT1QJ67P4";
$connectionInfo = array( "Database"=>"TestEventsDB");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}
else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
$sql = "SELECT count(EventCode) as eventcount FROM AlarmPointEvent where AlarmPoint=24001";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
}
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
echo $row['eventcount']."<br>";
}
sqlsrv_free_stmt( $stmt);
?>

SELECT* mssql + php

I wanna to show the results from this:
require 'connection.php';
$connectionInfo = array("UID" => $usr, "PWD" => $pwd, "Database" => $db);
$conn = sqlsrv_connect($serverName, $connectionInfo);
$tsql = "SELECT * FROM tadatable";
/* Execute the query. */
$stmt = sqlsrv_query( $conn, $tsql);
if ( $stmt )
{
$id = $stmt['id'];
echo"<td>".$stmt['name']."</td>";
} echo"<td>".$stmt['name2']."</td>";
else
{
echo "Error in statement execution.\n";
die( print_r( sqlsrv_errors(), true));
}
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
No data executed. Just blank page, can you please see what is wrong?
MSSQL/PHP/ works fine...
sqlsrv_query
Returns a statement resource on success and FALSE if
an error occurred.
After you've successfully executed the query with sqlsrv_query you can fetch the results, by using sqlsrv_fetch_array or use sqlsrv_fetch_array to get first result directly.
$stmt = sqlsrv_query( $conn, $tsql);
if($stmt === false) {
die( print_r( sqlsrv_errors(), true) );
}
// loop results with `sqlsrv_fetch_array`
while( $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC) ) {
echo $row['id'].", ".$row['name'].", ".$row['name2']."<br />";
}
I feel you missed scrollable array.
Alter the following line.
$stmt = sqlsrv_query( $conn, $tsql, array(), array( "Scrollable" => 'static' ));
<?php
require 'connection.php';
//$serverName = "(local)";
$connectionInfo = array("UID" => $usr, "PWD" => $pwd, "Database" => $db);
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false ){
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
$tsql = "SELECT * FROM tadatable";
if( sqlsrv_query( $conn, $tsql))
{
//echo "Statement executed.";
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
echo $row['id'].", ".$row['name'].", ".$row['name2']."<br />";
}
}
else
{
echo "Error in statement execution.\n";
die( print_r( sqlsrv_errors(), true));
}
sqlsrv_close($conn);
?>
For Reference : http://php.net/manual/en/function.sqlsrv-fetch-array.php
Example #1 Retrieving an associative array.

Webservice Not Displaying

I have this piece of code the function bit works fine but immediately from line
require('nusoap/lib/nusoap.php'); makes it to display an error XML declaration allowed only the start of the document when i try to view the wsdl on the browser kindly help
<?php
function getStockQuote($symbol)
{
$serverName = "MYSERVER"; //serverName\instanceName
$connectionInfo = array( "Database"=>"projects", "UID"=>"MYUSERNAME", "PWD"=>"MYPASSWORD");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( !$conn ) {
echo "Connection not established.<br />";
}
if( $conn === false ) {
die( print_r( sqlsrv_errors(), true));
}
$sql = "SELECT stock_symbol, stock_price FROM stockprices";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
}
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
return $row['stock_price'];
}
sqlsrv_free_stmt( $stmt);
}
require('nusoap/lib/nusoap.php');
$server = new soap_server();
$server->configureWSDL('stockserver', 'urn:stockquote');
$server->register("getStockQuote",
array('symbol' => 'xsd:string'),
array('return' => 'xsd:decimal'),
'urn:stockquote',
'urn:stockquote#getStockQuote');
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA)
? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA); ?>
</pre>

How to read from Firebird database and insert the data into MSSQL database?

I want to select some columns from Firebird database and insert them in MSSQL database tables.
When I read the table from Firebird it represent exactly what I need in the localhost, then when I want to insert the data into MSSQL it only inserts the last record. I don't know what cause this, and it's my first time to try this idea.
Would someone tell me what goes wrong?
This is my PHP code:
<?php
$host = 'localhost:c:\firebird.fdb';
$username='';
$password='';
$dbh = ibase_connect($host, $username, $password);
$stmt = "SELECT * FROM TWEEKDAY";
$sth = ibase_query($dbh, $stmt);
while ($row = ibase_fetch_object($sth)) {
$R1 = $row->CODE;
$R2 = $row->DAYNAME;
echo $R1 ." ". $R2 . "\n";
echo "</br>";
}
ibase_close($dbh);
/**********************************************/
$host = "servername\instancename";
$connectionInfo = array( "Database"=>"MSSQLdatabase", "UID"=>"", "PWD"=>"");
$conn = sqlsrv_connect( $host, $connectionInfo);
if( $conn === false ) {
die( print_r( sqlsrv_errors(), true));
}
echo "</br>";
$tsql = "INSERT INTO [AccessCard].[dbo].[WEEKDAY] (DAYID,DAYN) VALUES ('$R1','$R2')";
$stmt = sqlsrv_query( $conn, $tsql);
if ( $stmt === false ) {
die( print_r( sqlsrv_errors(), true));
}
while( $obj = sqlsrv_fetch_object( $stmt , SQLSRV_FETCH_ASSOC) ) {
echo $obj->DAYID ."&nbsp ". $obj->DAYN . "<br />";
}
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>
When i run my code, it shows in local host as :PIC1
and when i open the mssql database that what the table have:PIC2
it seems like it insert the last value of $R1 $R2.
and when i use this code:
<?php
$host = 'localhost:c:\FIREBIRD.fdb';
$username='';
$password='';
$dbh = ibase_connect($host, $username, $password);
$stmt = "SELECT * FROM TWEEKDAY";
$sth = ibase_query($dbh, $stmt);
while ($row = ibase_fetch_object($sth)) {
$R1 = $row->CODE;
$R2 = $row->DAYNAME;
echo $R1." ". $R2 . "\n";
echo "</br>";
}
ibase_close($dbh);
/**********************************************/
$host = "SERVERNAME\INSTANCENAME";
$connectionInfo = array( "Database"=>"AccessCard", "UID"=>"", "PWD"=>"");
$conn = sqlsrv_connect( $host, $connectionInfo);
if( $conn === false ) {
die( print_r( sqlsrv_errors(), true));
}
echo "</br>";
for ($RIdx = 0; $RIdx < count($R1); $RIdx++) { // each $R array value
$tsql = "INSERT INTO [AccessCard].[dbo].[WEEKDAY] (DAYID,DAYN) VALUES ('$R1[$RIdx]','$R2[$RIdx]')";
$stmt = sqlsrv_query( $conn, $tsql);
if ( $stmt === false ) {
die( print_r( sqlsrv_errors(), true));
}
} // end of insert loop
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>
This is what show in the database:PIC3
$R1 should be an array, as in $R1[] = $row->CODE; Also $R2' obviously. Change your sqlserver insert accordingly. I don't have sql server installed here so i cannot provide tested code.
Here is the sql server code...
$host = "servername\instancename";
$connectionInfo = array( "Database"=>"MSSQLdatabase", "UID"=>"", "PWD"=>"");
$conn = sqlsrv_connect( $host, $connectionInfo);
if( $conn === false ) {
die( print_r( sqlsrv_errors(), true));
}
echo "</br>";
for ($RIdx = 0; $RIdx < count($R1); $RIdx++) { // each $R array value
$tsql = "INSERT INTO [AccessCard].[dbo].[WEEKDAY] (DAYID,DAYN) VALUES ('$R1[$RIdx]','$R2[$RIdx]')";
$stmt = sqlsrv_query( $conn, $tsql);
if ( $stmt === false ) {
die( print_r( sqlsrv_errors(), true));
}
} // end of insert loop
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>
Finaly the problem solved:
here is the solution code
<?php
$host = 'localhost:c:\firebird.fdb';
$username='';
$password='';
$dbh = ibase_connect($host, $username, $password);
$stmt = "SELECT * FROM TWEEKDAY";
/**********************************************/
$host = "servername\instancename";
$connectionInfo = array( "Database"=>"AccessCard", "UID"=>"", "PWD"=>"");
$conn = sqlsrv_connect( $host, $connectionInfo);
if( $conn === false ) {
die( print_r( sqlsrv_errors(), true));
}
echo "</br>";
/**********************************************/
$sth = ibase_query($dbh, $stmt);
while ($row = ibase_fetch_object($sth)) {
$R1 = $row->CODE;
$R2 = $row->DAYNAME;
echo $R1." ". $R2 . "\n";
$tsql = "INSERT INTO [AccessCard].[dbo].[WEEKDAY] (DAYID,DAYN) VALUES ('$R1','$R2')";
$stmt = sqlsrv_query( $conn, $tsql);
if ( $stmt === false ) {
die( print_r( sqlsrv_errors(), true));
}
while( $obj = sqlsrv_fetch_OBJECT( $stmt) ) {
echo $obj->$R1."&nbsp ". $obj->$R2. "<br />";
$obj++;
}
echo "</br>";
}
ibase_close($dbh);
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>
thank you Ryan for your help, it seems that the solution came to my mind while i'm editing my question.
thanks again.

Categories