I using ODBC to connect sql server 2008 like
$virtual_dsn = 'DRIVER={SQL Server};SERVER=MyServerName;DATABASE=myDatabase';
$conn = odbc_connect($virtual_dsn,'sa','mypass') or die('ODBC Error:: '.odbc_error().' :: '.odbc_errormsg().' :: '.$virtual_dsn);
if (!$conn){
if (phpversion() < '4.0'){
exit("Connection Failed: . $php_errormsg" );
}
else{
exit("Connection Failed:" . odbc_errormsg() );
}
}
// This query generates a result set with one record in it.
$sql="SELECT TOP 10 * FROM Mytable";
# Execute the statement.
$rs=odbc_exec($conn,$sql);
// Fetch and display the result set value.
if (!$rs){
exit("Error in SQL");
}
while (odbc_fetch_row($rs)){
$col1=odbc_result($rs, "name");
echo "$col1 <br>";
}
// Disconnect the database from the database handle.
odbc_close($conn);
But i get text not correct like
b?�o c?�o việc sử dụng
i try to using odbc_exec($conn, "SET names utf8");
but get error
Warning: odbc_exec(): SQL error: [Microsoft][SQL Server Native Client 10.0][SQL Server]'names' is not a recognized SET option., SQL state 37000 in SQLExecDirect in C:\xampp\htdocs\sql\index.php on line 32
How set utf-8 using odbc_connect thanks
odbc_exec doesn't accept 'SET NAMES utf8' as second parameter. the second parameter must be the query.
to set utf8 for variables only use utf8_decode or iconv
$col1=utf8_decode(odbc_result($rs, "name"));
or
$col1=odbc_result($rs, "name");
iconv("UTF-8", "CP1252", $col1);
and
Warning: odbc_exec(): SQL error: [Microsoft][SQL Server Native Client 10.0][SQL Server]'names' is not a recognized SET option., SQL state 37000 in SQLExecDirect in C:\xampp\htdocs\sql\index.php on line 32
this is not an error, is a WARNING. but check odbc_exec manual to ensure all.
Related
<?php
$server="localhost";
$user="root";
$pass="";
$dbname="expert";
$dsn="mysql:host=$server;dbname=$dbname";
try {
$connect=new PDO($dsn,$user,$pass);
$connect->exec("SET character_set_connection ='utf8mb4'");
$connect->exec("SET NAMES ='UTF8'");
}
catch (PDOException $error){
echo ("unable to connect".$error->getMessage());
}
?>
unable to connectSQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1
NAMES is not a variable, SET NAMES is a SQL command.
According to the documentation of SET NAMES, the command requires a character set and optional a collation. Quotes are optional for the character set or collation clauses, e.g.
SET NAMES utf8mb4
However, you should avoid sending extra commands, since client can send the character set already during connection handshake.
This can be done by specifying the character set in your DSN:
$connect = new PDO("mysql:host=$server;dbname=test_db; charset=utf8mb4",$user,$pass);
This Worked for me you can try this
<?php
$server="localhost";
$user="root";
$pass="";
try {
$connect=new PDO("mysql:host=$server;dbname=test_db",$user,$pass);
$connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$connect->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND , "SET NAMES utf8");
$connect->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND , "SET character_set_connection =utf8mb4");
echo "connected Successfully";
}
catch (PDOException $error){
echo ("unable to connect".$error->getMessage());
}
?>
I am using odbc_connect() to connect php to .dbf files, i tried adding "Microsoft dBase Drive (*.dbf)" to my System DNS. although the connection was successful from my code I cannot run the odbc_exe() to get the data, it show "External table is not in the expected format"
//Storing DSN(Data Source Name created)
$dsn="myDBFdbDSNConnection";
$user="";
$password="";
$conn=odbc_connect($dsn,$user, $password);
if (!$conn)
{
echo (die(odbc_error()));
}
else
{
echo "Connection Successful !";
$sql="SELECT * FROM table1";
$rs=odbc_exec($conn,$sql);
}
//Resource releasing
odbc_close($conn);
I am new to PHP. I want to use the database stored in pgSQL from my PHP file. Please tell how to integrate pgSQL and PHP, so that I can use pgSQL in PHP using pg_connect(). My php.info() is showing MySQL, pgSQL, SQLite enabled under PDO support. But when I use pg_connect() it says:
Warning: pg_connect(): Unable to connect to PostgreSQL server: FATAL: role "postgres" does not exist in /Users/username/Sites/index.php on line 3
Warning: pg_last_error(): No PostgreSQL link opened yet in /Users/username/Sites/index.php on line 4
Warning: pg_last_error(): supplied resource is not a valid PostgreSQL link resource in /Users/username/Sites/index.php on line 4
Could not connect:
I have used this tutorial to run PHP on Mac. But I couldn't find a tutorial clearly showing how to integrate PHP and pgSQL.
https://www.dyclassroom.com/howto-mac/how-to-install-apache-mysql-php-on-macos-mojave-10-14
<?php
// Connecting, selecting database
$dbconn = pg_connect("host=localhost dbname=ipl user=postgres password=password")
or die('Could not connect: ' . pg_last_error());
// Performing SQL query
$query = 'SELECT * FROM player';
$result = pg_query($query) or die('Query failed: ' . pg_last_error());
// Printing results in HTML
echo "<table>\n";
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
// Free resultset
pg_free_result($result);
// Closing connection
pg_close($dbconn);
?>
I got the problem. My username is not postgres but my name. I must have set it while installing Postgres. Therefore it is saying no role as 'postgres' exist.
I have connect Ms Access Database to PHP File. PHP file Give Error
"Warning: odbc_connect(): SQL error: [Microsoft][ODBC Driver Manager]
Data source name not found and no default driver specified, SQL state IM002 in SQLConnect in C:\wamp\www\PI\Connection.php on line 3".
Connection.php
<?php
$con = odbc_connect("PIInstitute","","");
if($con){
echo "Connected";
}else{
echo "Failed";
}
?>
You need to specify your driver when calling odbc_connect() like so:
$conn = odbc_connect ( "Driver={SQL Server};Server=$servername;Database=$dbname;", $username, $password ) or die ( "Connection failed: " . $conn );
You can find more info on odbc_connect()here: http://php.net/manual/en/function.odbc-connect.php
The connection id returned by this functions is needed by other ODBC functions. You can have multiple connections open at once as long as they either use different db or different credentials.
resource odbc_connect ( string $dsn , string $user , string $password [, int $cursor_type ] )
<?php
// Microsoft SQL Server using the SQL Native Client 10.0 ODBC Driver - allows connection to SQL 7, 2000, 2005 and 2008
$connection = odbc_connect("Driver={SQL Server Native Client 10.0};Server=$server;Database=$database;", $user, $password);
// Microsoft Access
$connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$mdbFilename", $user, $password);
// Microsoft Excel
$excelFile = realpath('C:/ExcelData.xls');
$excelDir = dirname($excelFile);
$connection = odbc_connect("Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=$excelFile;DefaultDir=$excelDir" , '', '');
?>
I'm trying to connect to my database via cron job. However i keep receiving an error messages. After many frustrating hours im posting here for help.
My cron script php file:
<?php
define("HOST","localhost");
define("USERNAME","user_muser");
define("PASSWORD","*********");
define("DB_DATABASE","databasename");
$conn = mysqli_connect('HOST', 'USERNAME', 'PASSWORD','DB_DATABASE');
// Check connection
if (mysqli_connect_errno())
{
"Failed to connect to MySQL: " . mysqli_connect_error();
}
// Check if server is alive
if (mysqli_ping($conn))
{
"Connection is ok!";
}
else
{
"Error: ". mysqli_error($conn);
}
mysqli_close($conn);
?>
This is the error i received:
mysqli_connect(): (HY000/2005): Unknown MySQL server host 'HOST' (0)
mysqli_ping() expects parameter 1 to be mysqli
mysqli_error() expects parameter 1 to be mysqli
mysqli_close() expects parameter 1 to be mysqli
Any help? Thanks!
You're quoting constants, which makes them strings.
define("HOST","localhost");
define("USERNAME","user_muser");
define("PASSWORD","*********");
define("DB_DATABASE","databasename");
$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DB_DATABASE);
You should write constants out of quotes as following. Otherwise they are usual strings.
$conn = mysqli_connect(HOST, USERNAME, PASSWORD,DB_DATABASE);