Php - Call to undefined function mssql_query() - php

I have used the following php code to show the number of rows from sql table but I am getting below error message.
Fatal error: Call to undefined function mssql_query()
in D:\wamp\www\thereport24\gp.php
on line 22
my code:
$conn = odbc_connect('gpcon','','');
if ($conn)
{
$query= "select * from SubscriberServices where SubscriptionGroupID like 'ms_gp_tr24bn_3333'";
$results = mssql_query($query);
$rows = mssql_fetch_array($results);
echo $rows[0];
mssql_close($con);
}

If you use sql server, use this code. This works for me:
<?php
$serverName = "serverName";
$options = array( "UID" => "sa", "PWD" => "Password", "Database" => "DBname");
$conn = sqlsrv_connect($serverName, $options);
if( $conn ) {
echo "Connection established.<br />";
$query="select * from SubscriberServices where SubscriptionGroupID like 'ms_gp_tr24bn_3333'";
$result = sqlsrv_query($conn,$query);
sqlsrv_close($conn);
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
if it returns "Connection established." it's mean you install MS SQL Drivers correct.

Related

How to connect sql server to php

Hi I had a question about connecting php to sql server I install odbc driver and confige php.ini file. But when execute I get error.
I change the computer but still get same error.
<?php
$serverName = "YASSERAHMMED\YASOFT";
$connectionInfo = array( "Database"=>"ELECTERC_DB", "UID"=>"sa", "PWD"=>"google");
$con = sqlsrv_connect( $serverName, $connectionInfo );
if( $con === false ) {
//echo sqlsrv_errors();
die( print_r( sqlsrv_errors(), true));
}
$sql = "SELECT * FROM ACCOUNT_CUSTMERS";
$stmt = sqlsrv_query( $con, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
}
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
echo $row['date'].", ".$row['id']."<br />";
}
sqlsrv_free_stmt( $stmt);
?>
the result is :
error 5701 and 5703
I finally use PDO string connection to resolve the problem..
thanks every one...
$serverName = "YASSERAHMMED\YASOFT";
try
{
$conn = new PDO( "sqlsrv:server=$serverName ; Database=ELECTERC_DB", "", "");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
if($conn){
// echo'ggggg';
} else{
echo'undone';
}
}
catch(Exception $e)
{
die( print_r( $e->getMessage() ) );
}

Sql server, php [duplicate]

So I keep getting this error when I want to query something to the ms sql server..
The connection is made with the database but the queries seem to fail.
The error log contains this:
PHP Fatal error: Call to undefined function mssql_query()
The code on the php:
session_start();
include_once("connect.php");
if (isset($_POST['username'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM test WHERE username='".$username."' AND password='".$password."'";
$res = mssql_query ($sql) or die(mssql_error());
if (mssql_num_rows($res) == 1) {
$row = mssql_fetch_assoc($res);
$_SESSION['uid'] = $row['id'];
$_SESSION['username'] = $row['Username'];
$_SESSION['afdeling'] = $row['Afdeling'];
$_SESSION['mail'] = $row['Mail'];
header("Location: test.php");
exit();
} else {
echo "Invalid login information. Please return to the previous page.";
exit(); } } ?>
Does anybody knows what the problem is?
Thanks in advance!
connect.php code:
<?php
$serverName = "MTN-TEST"; //serverName\instanceName
$connectionInfo = array( "Database"=>"PROCES_TEST", "UID"=>"blaaa", "PWD"=>"blooo");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "<span style='color:green;'>Connection established.</span><br />";
}else{
echo "<span style='color:red;'>Connection could not be established.</span><br />";
die( print_r( sqlsrv_errors(), true));
}
?>
You don't have the MS SQL Drivers installed.
You can check this with phpinfo();
On Linux you need mssql.so or sybase.so
With debian its apt-get install php5-sybase
For windows take a look here:
http://msdn.microsoft.com/en-US/library/cc793139%28v=SQL.90%29.aspx
Drivers need to be configured for PHP to find the function mssql_...
You could also look at PDO DB classes as they can connect to any DBS, you need the drivers installed tho.
If your connect.php code returns "Connection established.", it's mean you installed MS SQL Drivers correctly.
You have to use sqlsrv_queryfunction instead of mssql_query.
The correct form of this command is:
<?php
$serverName = "serverName";
$options = array( "UID" => "sa", "PWD" => "Password", "Database" => "DBname");
$conn = sqlsrv_connect($serverName, $options);
if( $conn ) {
echo "Connection established.<br />";
$query='select * from test';
$result = sqlsrv_query($conn,$query);
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
you can learn more here:
"PHP Fatal error: Call to undefined function mssql_select_db() in c:\...appscript.php on line 16"
you can use mssql_get_last_message() for mssql errors

Problems with getting Tables from Databases using sqlsrv_query in php

I am having problems with sqlsrv_query in PHP. I get the following warning:
Warning: sqlsrv_query() expects parameter 1 to be resource, null given in C:\xampp\htdocs\Status.php on line 76
Notice: Array to string conversion in C:\xampp\htdocs\Status.php on line 76
A error occured: Array
This is my code:
<?php
$conn = OpenConnectionMsSQL();
#var_dump($conn);
$sql_select = "Select state_desc from sys.databases";
$params = array();
$query = sqlsrv_query($conn, $sql_select, $params) or die('A error occured:' . sqlsrv_errors());
$res = sqlsrv_num_rows($query);
while($row = sqlsrv_fetch_assoc($res))
{ echo ($row['state_desc']); }
?>
and this is my connection code:
<?php
function OpenConnectionMsSQL()
{
try{
$serverName = "***";
$database = "***";
$uid = "***";
$pwd = "***";
$connectionOptions = array("Database"=>"$database","Uid"=>"$uid","PWD"=>"$pwd");
$conn = sqlsrv_connect($serverName, $connectionOptions);
if($conn == false)
die( print_r( sqlsrv_errors(), true));
}
catch(Exception $e)
{
exit("<h1>"."Verbindung fehlgeschlagen!". "</br>". "Server Stopped"."</h1>");#.$e->getMessage());
exit("<!--"."<h1>"."Serverstatus: Verbindung fehlgeschlagen!". "</br>". "Server Stopped"."</h1>"."-->");#.$e->getMessage();
}
}
?>
How do I fetch my results correctly?
I just forgot to use sql_fetch_array, so I couldn't fetch my results.
The following snippet fixes the problem
$sql_select = "Select state_desc,name from sys.databases where name='master'";
$sql_version = "Select ##version as Version";
$stmt = sqlsrv_query($conn, $sql_select) or die('A error occured: ' . print_r( sqlsrv_errors(), true));
$version = sqlsrv_query($conn, $sql_version) or die('A error occured: ' . print_r( sqlsrv_errors(), true));
if (sqlsrv_has_rows($version)) {
$data2 = sqlsrv_fetch_array( $version, SQLSRV_FETCH_ASSOC);
echo "Serverinfo: ".$data2['Version']."<br/>"."<br/>";
} else {
echo "No data found";
}

IIS, MS SQL and PHP - SQL select in PHP not working

I have a local MS SQL Database, and a web PHP application on IIS on my server.
On IIS I have successfully connected PHP and my MS SQL database (added connection strings and i see my tables)
But, when I use any SQL select in the PHP web application, it does not work. No data is displayed, or any erros, for example :
/* Connect to a MySQL database using driver invocation */
$dsn = 'mysql:dbname=dbname;host=localhost';
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$results = mysql_query("SELECT id FROM users");
while($row = mysql_fetch_array($results)) {
$name = $row['id']
?>
<tr>
<td><?php echo '$name'?></td>
</tr>
<?php
}
?>
</tbody>
</table>
follow like this for pdo connection
$sql = $dbh->prepare("SELECT id FROM users");
$sql->execute();
while($result = $sql->fetch(PDO::FETCH_ASSOC)){
?>
<tr>
<td><?php echo $result['name'];?></td>
</tr>
<?php } ?>
Please follow that code:
$host = '127.0.0.1';
$db = 'test';
$user = 'root';
$pass = '';
$charset = 'utf8';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$opt =
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
$pdo = new PDO($dsn, $user, $pass, $opt);
$stmt = $pdo->query('SELECT name FROM users');
while ($row = $stmt->fetch())
{
echo $row['name'] . "\n";
}
MS SQL (or SqlSrv) and MySql are not working on the sames drivers. You have to know which one you are using and the find PHP functions ables to deal with it.
Note: PHP Extension for using driver must be installed on your server and activated on php.ini file
For MySql do not use mysql_xxx() deprecated functions, prefer mysqli_xxx() to them.
You can find here docs and samples code for both mysql & mssql php functions :
MySql :
http://php.net/mysqli_connect
php.net/mysqli_fetch_array
SqlSrv :
http://php.net/sqlsrv_connect
php.net/sqlsrv_fetch_array
So what is your database engine ?
Hope that'll helps you, cheers
Mixing the apis would not work - use only PDO methods like this perhaps
/* Connect to a MySQL database using driver invocation */
try {
/* mysql server */
/* $dsn = 'mysql:dbname=dbname;host=localhost'; */
/* MS SQL Server */
$dsn = 'sqlsrv:Database=dbname;Server=localhost';
$user = 'dbuser';
$password = 'dbpass';
$dbh = new PDO($dsn, $user, $password);
$sql='select * from users';
$results=$dbh->query( $sql );
if( $results ){
while( $rs=$results->fetch( PDO::FETCH_OBJ ) ){
echo "<tr><td>{$rs->name}</td></tr>";
}
}
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
Now working great also with select, with this code :
<?php
$serverName = "AC-CLOUD"; //serverName\instanceName
$connectionInfo = array( "Database"=>"Data", "UID"=>"sa", "PWD"=>"Masterkey2010");
$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 Code, Name FROM StoreCards";
$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['Code'].", ".$row['Name']."<br />";
}
sqlsrv_free_stmt( $stmt);
?>
How now i set this two values into table? this is last point. thank you for your patience and time.

PHP Fatal error: Call to undefined function mssql_query()

So I keep getting this error when I want to query something to the ms sql server..
The connection is made with the database but the queries seem to fail.
The error log contains this:
PHP Fatal error: Call to undefined function mssql_query()
The code on the php:
session_start();
include_once("connect.php");
if (isset($_POST['username'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM test WHERE username='".$username."' AND password='".$password."'";
$res = mssql_query ($sql) or die(mssql_error());
if (mssql_num_rows($res) == 1) {
$row = mssql_fetch_assoc($res);
$_SESSION['uid'] = $row['id'];
$_SESSION['username'] = $row['Username'];
$_SESSION['afdeling'] = $row['Afdeling'];
$_SESSION['mail'] = $row['Mail'];
header("Location: test.php");
exit();
} else {
echo "Invalid login information. Please return to the previous page.";
exit(); } } ?>
Does anybody knows what the problem is?
Thanks in advance!
connect.php code:
<?php
$serverName = "MTN-TEST"; //serverName\instanceName
$connectionInfo = array( "Database"=>"PROCES_TEST", "UID"=>"blaaa", "PWD"=>"blooo");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "<span style='color:green;'>Connection established.</span><br />";
}else{
echo "<span style='color:red;'>Connection could not be established.</span><br />";
die( print_r( sqlsrv_errors(), true));
}
?>
You don't have the MS SQL Drivers installed.
You can check this with phpinfo();
On Linux you need mssql.so or sybase.so
With debian its apt-get install php5-sybase
For windows take a look here:
http://msdn.microsoft.com/en-US/library/cc793139%28v=SQL.90%29.aspx
Drivers need to be configured for PHP to find the function mssql_...
You could also look at PDO DB classes as they can connect to any DBS, you need the drivers installed tho.
If your connect.php code returns "Connection established.", it's mean you installed MS SQL Drivers correctly.
You have to use sqlsrv_queryfunction instead of mssql_query.
The correct form of this command is:
<?php
$serverName = "serverName";
$options = array( "UID" => "sa", "PWD" => "Password", "Database" => "DBname");
$conn = sqlsrv_connect($serverName, $options);
if( $conn ) {
echo "Connection established.<br />";
$query='select * from test';
$result = sqlsrv_query($conn,$query);
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
you can learn more here:
"PHP Fatal error: Call to undefined function mssql_select_db() in c:\...appscript.php on line 16"
you can use mssql_get_last_message() for mssql errors

Categories