PHP error after query in table "Call to undefined function mysql_error ()"

0

I have a query query of data made in PHP version PHP Version 5.2.4

Where 1 to each N query returns the error:

Ithinkit'saone-timeexception,buttheremustbesomewaytohandleit.

Thelineofcodethattheerrormentionsrightafterthequery:

$result_conhec=mssql_query($sql_conhec)ordie("E R R O R SQL_OCORR ". mssql_error());
    
asked by anonymous 18.05.2018 / 20:30

1 answer

2

First, PHP 5.2.4 is very old, it really would be better to update this, download a newer version of Xampp or Wamp.

Second, your title speaks mysql_error but in your code this mssql_error , so they are different functions, actually mssql_error does not exist.

If you want to connect with mySql then use mysqli or PDO with PDO_MYSQL .

If you want to connect with SqlServer, then use:

With functions sqlsrv_

<?php
$serverName = 'serverName\nomeDaInstancia';

// SE não definir UID e PWD no $connectionInfoa conexão irá usar a autenticação do Windows.
$connectionInfo = array(
   'UID' => 'usuario',
   'PWD' => 'senha',
   'Database' => 'nomedobanco'
);

$conn = sqlsrv_connect($serverName, $connectionInfo) or die( print_r( sqlsrv_errors(), true));

$sql = 'SELECT * FROM foo WHERE id = ? OR bar = ?';
$params = array(1, 'texto');

$stmt = sqlsrv_query($conn, $sql, $params) or die( print_r( sqlsrv_errors(), true));

sqlsrv_close($conn);

With functions obdc_ :

$driver = 'tipo do driver do servidor cliente';
$server = 'servidor';
$database = 'banco';
$user = 'usuario';
$password = 'senha';

$Conn = odbc_connect("Driver=$driver;Server=$server;Database=$database;", $user, $password) or die("E R R O R CONEXAO " . odbc_errormsg() );

$result = odbc_exec($conn, "SELECT * FROM foo WHERE id = 1 OR bar = 'texto'") or die("E R R O R SQL_OCORR " . odbc_errormsg($conn) );

while (odbc_fetch_row($result)) {
    for ($i=1;$i <= odbc_num_fields($result); $i++) {
        echo "Resultado: " . odbc_result($result, $i);
    }
}

odbc_close($);

With PDO ( PDO_ODBC ):

$driver = 'tipo do driver do servidor cliente';
$server = 'servidor';
$database = 'banco';
$user = 'usuario';
$password = 'senha';

try {
    $conn = new PDO("odbc:Driver=$driver;SERVERNAME=$server;DATABASE=$database",
          $user, $password);  
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(Exception $e) {
    die( print_r( $e->getMessage() ) );   
}

With PDO ( PDO_SQLSRV ):

If you are going to use this on a Windows hosting / server you will need to install < in> SQLSRV 3.0 . If you need support for PHP5.2 or compiled in VC6, use this driver SQLSRV 2.0 .

$server = 'servidor';
$database = 'banco';
$user = 'usuario';
$password = 'senha';

try {
    $conn = new PDO("sqlsrv:server=$server;Database=$database", $user, $password);  
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(Exception $e) {
    die( print_r( $e->getMessage() ) );   
}

Note that the value of the $driver variable in some of the examples will depend on which driver you have installed on your server / hosting and on your local computer (both can be different drivers) >

$driver = 'FreeTDS'; //Este é usado em algumas hospedagens Linux e Unix (como Mac OSX)
$driver = '{SQL Server}';
$driver = '{SQL Server Native Client 10.0}'; //versão 10
$driver = '{SQL Server Native Client 11.0}'; //versão 11
$driver = '{ODBC Driver 11 for SQL Server}'; //ODBC

Note that there is also an odbc driver for Linux: link

    
19.05.2018 / 07:56