I have a PHP code to get info from my Microsoft SQL server 2014, but it isnt working, the page it self works fine since it pops up as it should when i comment out the PHP code, but as soon as the PHP code isnt commented out, its just all white, so im assuming problem with the PHP code. I have to get the results from the query out into a drop down menu.
i use this code:
$servername = "VCCSQL03";
$username = "forecast";
$password = "Telefon2";
$dbname = "Forecast";
$connectionInfo = array("Database"=>$dbname, "UID"=>$username, "PWD"=>$password);
$conn = sqlsrv_connect($serverName, $connectionInfo);
if(!$conn) {
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
// Check connection
$result = sqlsrv_query($conn,"SELECT * FROM dbo.vw_BrandProduct");
if ($result->num_rows > 0) {
// output data of each row
while($row = sqlsrv_fetch_array($result)) {
echo "<option value='".$row['Brand_ProductID']."' name='".$row['Brand_ProductName']."'</option>";
}
} else {
echo "";
}
sqlsrv_close();
First and foremost, you do not have an open and closed select tag, and your option tags was missing a > to close it properly. Try the below revision, assuming connection is established on the page properly then this should work.
$connectionInfo = array( "Database"=>$dbname, "UID"=>$username, "PWD"=>$password);
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if(!$conn) {
//// Check connection
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
$result = sqlsrv_query($conn,"SELECT * FROM dbo.vw_BrandProduct");
if ($result->num_rows > 0) {
// output data of each row
echo "<select name='products'>";
while($row = sqlsrv_fetch_array($result)) {
echo "<option value='".$row['Brand_ProductID']."'>$row['Brand_ProductName']</option>";
}
echo "</select>";
} else {
echo ""; } sqlsrv_close(); ?>
Related
actually i try to write the program from get the ms sql data select and then selected data insert to the MySQL table.i write the program which one get the final value of the table. because insert query place out side the while loop.how do i get the all the value in to the insert query??
I think can store in the while loop execution data json array.can i retrieve the one by one?
MSSQL DB connection
<?php
$serverName = "servername"; //serverName\instanceName,
portNumber (default is 1433)
$connectionInfo = array( "Database"=>"Databasename",
"UID"=>"username", "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));
}
?>
test.php
<?php
require_once ('inc/MSSQL_connection.php');
//require_once ('inc/MYSQL_connection.php');
$query ="SELECT EnrolledID,Date,Time FROM dbo.view_attendance
ORDER BY Date,Time";
$result = sqlsrv_query($conn,$query);
if ($result == FALSE){
die(FormatErrors(sqlsrv_errors()));
}
else{
while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC))
{
$emp_no = $row['EnrolledID'];
$date = date_format($row['Date'], 'Y-m-d');
$time = date_format($row['Time'], 'H:i:s');
}
//echo $emp_no;
$conn = mysqli_connect("localhost", "root", "","kelaniya");
$query = "INSERT INTO attendancedata (EnrolledID,Date,Time) VALUES ('{$emp_no}', '{$date}', '{$time}')";
mysqli_query($conn, $query);
}
?>
IMHO you can just move insert into while loop, why not?:
<?php
require_once ('inc/MSSQL_connection.php');
//require_once ('inc/MYSQL_connection.php');
$query ="SELECT EnrolledID,Date,Time FROM dbo.view_attendance
ORDER BY Date,Time";
$result = sqlsrv_query($conn,$query);
if ($result == FALSE){
die(FormatErrors(sqlsrv_errors()));
}
else{
$conn_insert = mysqli_connect("localhost", "root", "","kelaniya");
while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC))
{
$emp_no = $row['EnrolledID'];
$date = date_format($row['Date'], 'Y-m-d');
$time = date_format($row['Time'], 'H:i:s');
//echo $emp_no;
// MySQLli
$stmt = $conn_insert->prepare('INSERT INTO attendancedata (EnrolledID,Date,Time) VALUES (?,?,?)');
// I insert as string as was in original question (table metadata was not given...)
$stmt->bind_param('sss', $emp_no, $date, $time ); // 's' specifies the variable type => 'string'
$stmt->execute();
}
}
?>
Whenever I try to run the below code, I get the passed variable ". $record . " as it is.
I am trying to populate a drop-down from a database, but I keep getting a value which I am passing in the while loop.. I know I am making a simple mistake.Can someone please help to solve this issue
<select name="c" multiple="multiple">
<option>--select--</option>
<?php
$dbname = "abc";
$serverName = "xyz"; //serverName\instanceName
try
{
$connectionInfo = array( "Database"=>$dbname);
$conn = sqlsrv_connect( $serverName, $connectionInfo);
$SQL_string= "Select distinct [CC] from [abc].[dbo].
[abcdefg] ";
$stmt = sqlsrv_query($conn, $SQL_string);
$records[] = array();
while ($row = sqlsrv_fetch_array($stmt,
SQLSRV_FETCH_ASSOC))
{
$records[] = $row['CC'];
}
foreach ($records as $record)
{
echo' <option value="'. $record . '">' .$record.
'</option>';
}
if( $stmt === false ) {
die( print_r( sqlsrv_errors(), true));
}
}
catch(Exception $ex)
{
echo "Connection failed: " . $ex->getMessage();
}
?>
Here's an attempt at simplifying this a bit. I can't guarantee it will work, but it may bring to light the error you are facing:
<select name="c" multiple="multiple">
<option>--select--</option>
<?php
$dbname = "abc";
$serverName = "xyz"; //serverName\instanceName
try
{
$connectionInfo = array( "Database"=>$dbname);
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false ) {
die( print_r( sqlsrv_errors(), true));
}
$SQL_string= "Select distinct [CC] from [abc].[dbo].[abcdefg];";
$stmt = sqlsrv_query($conn, $SQL_string);
if( $stmt === false ) {
die( print_r( sqlsrv_errors(), true));
}
while ($row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC))
{
echo '<option value="'.$row['CC'].'">'.$row['CC'].'</option>';
}
sqlsrv_free_stmt( $stmt);
}
catch(Exception $ex)
{
echo "Caught exception error: " . $ex->getMessage();
}
?>
</select>
I'm trying to make a search bar to search my db but it always returns 0 results,
Here is the Connect and search code,
<?php
$serverName = "server";
$connectionInfo = array( "Database"=>"server");
$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));
}
$output ='';
if(isset($_POST['PartSelect'])){
$searchq = $_POST['PartSelect'];
$query = sqlsrv_query($conn, "SELECT * FROM Parts WHERE PartNumber LIKE '%$searchq%'") or die ("Could not search");
$count = sqlsrv_num_rows($query);
if($count == 0){
$output = 'There are no results';
----- This Section is new error Code -----
$rows = sqlsrv_has_rows( $query );
if ($rows === true)
echo "\nthere are rows\n";
else
echo "\nno rows\n";
---- End Section ------
This prints saying it has rows so it it picking up the data,
}
else {
while ($row = sqlsrv_fetch_array($query)){
$Partno = $row['PartNumber'];
$output .= '<div>'.$PartNo. '</div>';
}
}
?>
Here is the search bar and submit button (I am using materialize as a framework), and the output at the end.
<form action="Tests.php" method="post">
<div class="form-group">
<br/><input name="PartSelect" type="text" class="form-control" id="PartSelect" aria-describedby="nameHelp" placeholder="Enter Part">
<br/><button name="SelectPartbtn" id="Submitbtn" type="Submit" class="waves-effect waves-light btn">Find</button>
</div>
<?php print ("$output"); ?>
</form>
try with this code check connection is made or not
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = sqlsrv_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . sqlsrv_errors());
}
$conn = sqlsrv_connect( "192.168.1.6",
array('Database' =>"epromis_test",
"UID"=>"php_test",
"PWD"=>"phptest#123") );
if ($conn)
{
echo "sucess";
}
else
{
echo "failed";
}
Why I failed to connect the database?
thanks in advance
Here is link to very good php documentation http://php.net/manual/en/function.sqlsrv-connect.php. I think you need to escape your # symbol in password. # becomes &commat. link on how to pass special chars https://brajeshwar.github.io/entities/
<?php
$serverName = "serverName\\sqlexpress"; //serverName\instanceName
$connectionInfo = array( "Database"=>"epromis_test", "UID"=>"php_test", "PWD"=>"phptest&commat123");
$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));
}
?>
I'm trying pull back data from MS SQL via a php page. I have got a valid connection, and am trying a simple SELECT * FROM MyTable but sqlsrv_num_rows is just blank no matter what I do!!!
Here is my code:
function connect() {
$serverName = DB_HOST; //serverName\instanceName
$connectionInfo = array( "Database"=>DB_NAME, "UID"=>DB_USER, "PWD"=>DB_PASSWORD);
$this->connection = sqlsrv_connect( $serverName, $connectionInfo);
sqlsrv_connect( $serverName, $connectionInfo);
if( $this->connection ) {
echo "<br>Connection established.<br />";
}else{
echo "<br>Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
}
The echo for Connection established is working so all AOK there!
Now when I do a simple query:
function query($sql) {
if ($this->debug) {
echo $sql . "<br />";
}
$this->sql = $sql;
$this->recordset = sqlsrv_query($this->connection, $sql);
if (!$this->recordset) {
die('<br><br>Invalid query :<br><br><bold>' . $this->sql . '</bold><br><br>' . sqlsrv_errors());
}
echo "<br>rows = " . sqlsrv_num_rows($this->recordset);
I get absolutely nothing from the above echo? Any reason why? Or can you suggest a new echo I can try to debug this?
All my code in my DB class is converted from mysql so there may be a few bits wrong that is doing the damage!
I've even tried a super simple version, all the code together and it's still blank/false:
$server = DB_HOST;
$conn = sqlsrv_connect( $server, array( "Database"=>DB_NAME, "UID"=>DB_USER, "PWD"=>DB_PASSWORD) );
$stmt = sqlsrv_query( $conn, "SELECT * FROM MyTable");
$row_count = sqlsrv_num_rows($stmt);
echo "<br>row count = " . $row_count;
if ($row_count === false)
echo "\nerror\n";
else if ($row_count >=0)
echo "\n$row_count\n";
die;
Try this.....
Replace below statement
sqlsrv_query( $conn, "SELECT * FROM MyTable");
as
sqlsrv_query( $conn, "SELECT * FROM MyTable", array(), array("Scrollable"=>"buffered"));