Failure to connect to the database in the local host - php

<?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());
}
?>

Related

PHP return error when Alter Table Add Primary Key

10.1.15-MariaDB, PHP 5.4
Code used:
$table = 'abc';
mysql_query("ALTER TABLE `$table` ADD PRIMARY KEY (`col`)");
Error:
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 \'ALTER TABLE...
I have tried KEY `PRIMARY` and remove all backquotes to no avail.
Updates:
It has nothing to do with extensions whatsoever, tested with mysqli_ same error. However, repeated test using same script in different server environment is fine.
i have tried this code and it works for me
$table = 'abc';
$query="ALTER TABLE `".$table."` ADD PRIMARY KEY(`col`);";
/* Execute query */
$result=$mysqli->query($query);
/* Verify results */
if(!$result) {
$ErrMessage = "Error : " $mysqli->error . "\n";
$mysqli->close();
die($ErrMessage);
}
Do you have permissions to modify the table?
here a quik connection with mysqli
$mysqli = new mysqli($db_host, $db_user, $db_pass,$database);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
die ("<h1>can't use database !</h1>");
exit();
}
/* change character set to utf8 */
if (!$mysqli->set_charset("utf8")) {
printf("error loding 'character set utf8' : %s\n", $mysqli->error);
}
First of all, thank you all for trying to help. I have the answer after copy and paste the scripts to a different server environment which shows extensive error descriptions.
right syntax to use near '\xef\xbb\xbfALTER TABLE abc ADD PRIMARY KEY
The devil is \xef\xbb\xb
This happened as I have to deal with multi languages. However I do hope some suggestions given to me on how to avoid such silly mistakes in future.

what is the correct syntax for insert into php mysql. W or W/O session variables

I have tried a lot of syntax trials with single quotes and $session variables, now i decided to put the session variables in regular variables for less syntax complications.
$conn = mysql_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysql_error());
}
if (!mysql_select_db($dbname)) {
die('Could not select database: ' . mysql_error());
}
$this_email = $_SESSION['email'];
$this_password = $_SESSION['pw'];
$this_number = $_SESSION['phone'];
$sql = mysql_query("INSERT INTO user_accounts(Email, Password, Phone#) VALUES ('$this_email','$this_password','$this_number')");
if (!$sql) {
echo mysql_error();
}
mysql_close($conn);
This is the error i get:
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
Column names may have basic Latin letters, digits 0-9, dollar, underscore. All other characters will need to be in backticks.
http://dev.mysql.com/doc/refman/5.7/en/identifiers.html
so try:
INSERT INTO user_accounts(Email, Password, `Phone#`)
You also should update your driver and use prepared statements.
Because Phone# has a special character in it (#) you need to write it in backticks!
Furthermore I would recommend you to use the PDO or mysqli lib as well as prepared statements, because the mysql library is deprecated.

Not connect with mysql

I have 2 connection string in 2 file connect.php connect to 2 mysql database on 2 server.
File dbconnect1:
<?php
$conn = mysql_connect('sv1','root','123456') or dir("No connect");
mysql_select_db('db1')or dir("not connect database");
mysql_query("SET charactor_set_results=utf8",$conn);
mysql_query("SET NAMES 'utf8'");
?>
File dbconnect2:
<?php
$conn = mysql_connect('sv1','root','123456') or dir("No connect");
mysql_select_db('db2')or dir("not connect database");
mysql_query("SET charactor_set_results=utf8",$conn);
mysql_query("SET NAMES 'utf8'");
?>
When I include in file php and exercute query it not show result.
One of 2 connection string not work. Why?
I think it is because you use the same variable $conn for both DB connections. Replace it with $conn2 for example in the dbconnect2.php file. At the moment your first connection get overwritten by the second connection. This is because you do them one after another.
Plus you have to put TRUE for the fourth paramether in mysql_connect() function in order to start a new connection to the same DB server.
http://php.net/manual/en/function.mysql-connect.php
File dbconnect2:
<?php
$conn2 = mysql_connect('sv1','root','123456', TRUE) or die("No connect");
mysql_select_db('db2', $conn2)or die("not connect database");
mysql_query("SET charactor_set_results=utf8", $conn2);
mysql_query("SET NAMES 'utf8'", $conn2);
?>
Plus you have to point which MySQL connection you want to use in mysql_select_db() function(s) and mysql_query() function(s).
You have to change the second db connection variable say $conn2 and also use the connection statement as follows:
$conn2 = mysql_connect('sv1','root','123456', TRUE) or die("No connect");
Where the 4th parameter has to pass as TRUE.
Reference: http://php.net/manual/en/function.mysql-connect.php

Not connect to db on linux server

I have a connection string:
<?php
$conn = mysql_connect(localhost,root,abc#2014) or die(mysql_error());
mysql_select_db(db1)or die(mysql_error());
mysql_query("SET charactor_set_results=utf8",$conn);
mysql_query("SET NAMES 'utf8'");
?>
My database on linux server and when i connect to server. Error show:
Parse error: syntax error, unexpected '#' in /opt/lampp/htdocs/folder1/config/connect.php on line 2
Why?
$conn = mysql_connect("localhost","root","abc#2014") or die(mysql_error());

PHP connect SQL server 2008 - How set utf-8 using odbc_connect

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.

Categories