Error populating XML with PHP from SQLServer - php

I have a database in SQL Server, and I want to populate the XML document from my database, using PHP. But when I open the populate.php file I have the error below...
Call to undefined function sqlsrv_connect() in C:\xampp\htdocs\populate.php on line 6
My php file is like below:
<?php
$server = "USER-PC\SQLEXPRESS";
$options = array("UID"=>"", "PWD"=>"", "Database"=>"RESTORANTET");
$conn = sqlsrv_connect($server, $options);
$sql = "SELECT Emri_Pjates, Cmimi, Pershkrimi FROM dbo.MENU FOR XML PATH";
$stmt = sqlsrv_query($conn, $sql);
sqlsrv_fetch($stmt);
$xml = sqlsrv_get_field($stmt, 0);
?>
Please can you help me? Why sqlsrv_connect() is unidentified? I am not sure about the username and password.. When I enter the SQL Server ,I enter with Windows Authentification, and I dont use any username or password...That is why I have leave UID amd PWD empty here..Am I wrong?

It seems that your php does not have the neccessary driver to connect to sql server.
download sql server driver for php here

Related

Query doesn't work on my Raspberry Pi 2B

I'm trying to get my raspberry working like a webserver.
It all works, except the queries of my mysql database. The values are correct, the query is correct and my database works fine.
But when I want to get information from my database with PHP it gives an empty result.
The code:
<?php
$dbCon = new mysqli($servername, $username, $password, $database);
$Query = "SELECT * FROM `machineuptime`";
if($stmt = $dbCon->prepare($Query))
{
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc())
{
$ArrayData2[] = $row;
}
}
echo "this is de first row of the array: " . $ArrayData2[0]['ID'] . ". Thats nice!";
?>
the result:
this is de first row of the array: . Thats nice!
Even when I use the query in phpmyadmin, it gives the values back, so the Query isn't wrong.
So how can I solve this issue?
Edit:
The error that I get is.
Fatal error: Call to undefined method mysqli_stmt::get_result() in /var/www/html/Stramit/index.php on line 59
you don't close the query at:
$Query = "SELECT * FROM `machineuptime`;
change it to:
$Query = "SELECT * FROM `machineuptime`";
also for the error
Fatal error: Call to undefined method mysqli_stmt::get_result() in /var/www/html/Stramit/index.php on line 59
look at this answer.
Look at the user notes for http://php.net/manual/en/mysqli-stmt.get-result.php
It requires mysqlnd driver.Make sure you have that driver installed. If you do not have them installed use BIND_RESULT & FETCH
uncommented the extension=php_mysqli_mysqlnd.dll in php.ini; and restarted Apache2.2 and MySQL services. says it in the comment of the answer.
Here is the link to the driver: http://php.net/manual/en/book.mysqlnd.php
after you have installed it you should be able to use $stmt->get_result(); ofcourse reboot your raspberry pi.
Note: your php version must be above php 5.3.0

PHP call to SQL server stored procedure doesn't work when ported from Windows to Linux

This is pretty similar to this question, but I need to capture output. It seems to be basically an issue with getting the correct syntax, but using the syntax suggested in the answer didn't work for me.
I have a php application that calls a stored procedure on a SQL Server database which I developed on my Windows laptop running xampp.
My stored procedure looks like this:
CREATE Procedure [dbo].[check_for_barcode] #Sample_Id nvarchar(20), #Barcode_In_table int OUTPUT
AS
SELECT #Barcode_In_table = count(*) FROM dbo.sample_info
WHERE sample_name = #Sample_Id
RETURN #Barcode_In_table
GO
The php code that works on Windows xampp looks like this:
// use a sample Id known to be present in database
$sample_yes = '11335577';
// use a sample Id known NOT to be in database
$sample_no = '0011223344';
$host = 'hostIP';
$db = 'db';
$user = 'user';
$password = 'pw';
try {
$conn = new PDO("sqlsrv:Server=$host;Database=$db", $user, $password);
$sql = "{CALL dbo.check_for_barcode(#Sample_Id=:barcode, #Barcode_In_table=:isInDatabase)}";
$stmt = $conn->prepare($sql);
$stmt-> bindParam('barcode', $sample_yes, PDO::PARAM_STR);
$stmt->bindParam('isInDatabase', $isInDatabase, PDO::PARAM_INT|PDO::PARAM_INPUT_OUTPUT, 4);
$stmt->execute();
echo 'for sample id 11335577 $isInDatabase = '.$isInDatabase."\n";
$stmt-> bindParam(':barcode', $sample_no);
$stmt->execute();
echo 'for sample id 0011223344 $isInDatabase = '.$isInDatabase;
$stmt->closeCursor();
}
catch (Exception $e) {
echo $e->getMessage();
}
The output is:
for sample id 11335577 $isInDatabase = 1
for sample id 0011223344 $isInDatabase = 0
Since moving it to the Linux (Ubuntu 4.24) server where it will be deployed, it no longer works.
I installed the dblib drivers as described here.
I adapted the code to as required to use the dblib driver so that it would run without errors. This only involved changing the first two lines inside the try statement.
I have changed the first line to:
...
$conn = new PDO("dblib:host=$host;dbname=$db", $user, $password);
...
I have tried all of the following for the second line:
$sql = "{CALL dbo.check_for_barcode(#Sample_Id=:barcode, #Barcode_In_table=:isInDatabase)}";
$sql = "{CALL dbo.check_for_barcode #Sample_Id=:barcode, #Barcode_In_table=:isInDatabase}";
$sql = "CALL dbo.check_for_barcode(#Sample_Id=:barcode, #Barcode_In_table=:isInDatabase)";
$sql = "CALL dbo.check_for_barcode :barcode, :isInDatabase";
As well as all of the above using EXEC instead of CALL
The output in all cases is:
for sample id 11335577 $isInDatabase =
for sample id 0011223344 $isInDatabase =
If I add ...
while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
var_dump($result);
}
... this generates no output.
There are no error message at all, so I can't see where the problem is. I would welcome any suggestions on what to try next.
It's been a while since I had to do this, but I used ODBC to connect to SQL Server from PHP running on a linux box.
Drivers - http://msdn.microsoft.com/en-gb/data/ff657782.aspx
ODBC docs - http://www.php.net/manual/en/ref.pdo-odbc.connection.php
EDIT: The ODBC drivers above are for Windows, the Linux installation instructions and drivers can be found at www.php.net/pdo_odbc

Cannot get my php to process sqlsrv_connect on my GoDaddy Server

I have an android app that i am trying to get connected to my SQL Server.
I have tried countless ways of getting it to work by get a error on my sqlsrv_connect.
Here is the php im tyring to use. I have replaces my read server/credentials with *
<?php
$myServer = "***";
$myUser = "***";
$myPass = "***";
$myDB = "***";
//connection to the database
$conn = sqlsrv_connect($myServer, array('UID'=>$myUser, 'PWD'=>$myPass, 'Database'=>$myDB));
$_GET['search'];
//declare the SQL statement that will query the database
$query = "SELECT * FROM dbo.JD";
$data = sqlsrv_query($conn, $sql);
$result = array();
do {
while ($row = sqlsrv_fetch_array($data, SQLSRV_FETCH_ASSOC)){
$result[] = $row;
}
}while ( sqlsrv_next_result($data) );
echo(json_encode($result));
sqlsrv_free_stmt($data);
mssql_close($dbhandle);
?>
When processing on a php online test i get this error
Fatal error: Call to undefined function sqlsrv_connect() in [...][...]on line 7
Here my my php info
http://www.bakerabilene.com/phpinfo.php
It shows sqlsrv as a Registered PHP Streams so I don't understand why its not working.
I have also stripped my php down to where it just makes the connection to see if anything was causing the issue, but it still gives me that same error.
I am positive my server/user/pass/db are correct because i use the exact same credentials on my aspx webpage to access SQL server.
Check if the Native SQL Driver (Microsoft Drivers 3.0 for PHP for SQL Server) is installed . Have you asked the Godaddy Team about this ?

having problems connecting to mysql db with php

I have a test db on godaddy, I have had a similar issue before but with the help of stackoverflow it was solved.. So I have actually used the same solution however now I am having a host of errors when I try to load my php file from the browser.
Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/content/97/8603797/html/countryQuery.php on line 14
Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/content/97/8603797/html/countryQuery.php on line 14
Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /home/content/97/8603797/html/countryQuery.php on line 16
Warning: mysql_close(): no MySQL-Link resource supplied in /home/content/97/8603797/html/countryQuery.php on line 18
Database Output
I have read what the problem is and some of the things I have read suggest I am not connecting to the DB correctly.. but I am only doing what I have read/been told and dont really know what else I can do to solve the issue I am having...
this is my php code, if anyone has and suggestions or solutions I would greatly appreciate hearing from you.
<?
$hostname = 'mydatabasename.db.6666666.hostedresource.com';
$database = 'mydatabasename';
$username = 'myusername';
$password = 'secretsecret';
// establish a connection
$db = new PDO("mysql:host=$hostname;dbname=$database",$username,$password);
$query="SELECT * FROM `Countries`";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
echo "<b><center>Database Output</center></b><br><br>";
$i=0;
while ($i < $num) {
$country=mysql_result($result);
echo "<b>$country<br>";
$i++;
}
?>
the out put should just be a list of the country names I have in the countrys table of my database.. which has values in it.
You are mixing PDO and mysql_* functions.
Try something like this:
$db = new PDO("mysql:host=$hostname;dbname=$database",$username,$password);
$db->setAttribute(PDO::ATTR_AUTOCOMMIT, true);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql="SELECT * FROM `Countries`";
$numRows=$dbh->exec($sql);
echo "There were $numRows selected with:<b> $sql </b><br><br>";
$db=null;
unset($db);
When it comes to getting data from the PDO connection, I normally do something like this using the FETCH_INTO option:
$stmt = $db->query($sql);
$obj = $stmt->setFetchMode(PDO::FETCH_INTO, new userStructure);
// I do normally have a class matching the row I wan:
foreach($stmt as $userStructure)
{
$someObject->year=$userStructure->year;
$someObject->month=$userStructure->month;
unset($stmt);
}
Make sure that you include the port as well when using a PDO connection. This is looking like you are doing this in a hosted environment. Check the installation guide regarding the port used.

Connecting to MSSQL database with ADO and PHP

I have a MSSQL database. I wrote a php page to connect and retreive data from it. The code works on my localhost.
$strCon = "Provider=SQLOLEDB;Data Source=local;database=test;uid=sa;pwd=go;";
$strAlias = "GEORGE";
$strSql = "SELECT LINK FROM LINKS WHERE ALIAS = '" . $strAlias . "'";
$Con = new COM ("ADODB.Connection") or die("Cannot start ADO");
$Rs = new COM ("ADODB.Recordset") or die("Cannot start ADO");
$Con->open($strCon);
$Rs->open($strSql,$Con,1,3);
if (!$Rs->EOF && !$Rs->BOF) {
$strTargetLink = $Rs->Fields['LINK'];
echo $strTargetLink;
}
$Rs->Close();
$Con->Close();
$Rs = null;
$Con = null;
When I run this code on the server, I receive some error? when echo $strTargetLink; row is executed, word
Object
is received on the sent html page and also in the source code of that page.
I am running PHP on IIS as a FastCGI application. Both PHP 4.4.7 and 5.2.6 are supported.
Any ideas? What does this Object text mean?
Thanks.
Instead of using echo, I now tried
var_dump($strTargetLink);
and received the information
object(COM)(1) { [0]=> resource(3) of type (COM) }
try using var_dump instead of echo. it will at least give you more information about the object contained in $strTargetLink.
I have found a solution and now it works. So just in case you would like to connect with ADO, I am writing what I have done.
Instead of
$strTargetLink = $Rs->Fields['LINK'];
I wrote
$strTargetLink = $Rs->Fields(0);

Categories