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());
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());
}
?>
This question already has answers here:
Syntax error unexpected T_CONSTANT_ENCAPSED_STRING [closed]
(3 answers)
Closed 6 years ago.
<?php
$con = mysqli_connect("localhost" "root" "") or die("Unable to connect");
mysqli_select_db("logindb", $con);
?>
This is the configuration of the connecting.
<?php
require 'config.php';
?>
This is where is connect the database to a registration page.
Can you see something wrong? This is the error i get:
Parse error: syntax error, unexpected '"root"' (T_CONSTANT_ENCAPSED_STRING)
Thanks,
Realcookie
You are missing the commas bro. Here is the corrected one
<?php
$con = mysqli_connect("localhost", "root", "") or die("Unable to connect");
mysqli_select_db($con, 'logindb');
?>
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
<?php$host="localhost";
$db="profile";
$user="root";
$pass="";
$connection=mysql_connect($host,$user,$pass);
if(!$connection){
die("Database server connection failed.");
}else{
$dbconnect=mysql_select_db("profile",$connection);
if(!$dbconnect){
die("Unable to connect to the specified database!");
}else{
$username=$_GET["username"];
$password=$_GET["password"];
$query="SELECT id,profile_id,username,password from home where username='$username'and password='$password'";
$resultset=mysql_query($query,
$connection);
$records=array();
if($resultset===false){
die(mysql_error());
}
while($r=mysql_fetch_row($resultset)){
$records[]=$r;
}
echo json_encode($records);
}
}?>
and error is <?php$host="localhos^Expecting '{', '['
please explain me where is the problem in my script.
I m using $_get[] method to enter detail through url in my database but I m getting error. It works in localhost but not working in validator. I just want to run it in jsonlint. What I do I have to do to correct it ?
You are indeed missing a space between <?php and $host as the parser is saying it to you in your error.
please keep a space between
<?php$host="localhost"; sholud be corrected as `<?php $host="localhost";`
I'm trying to set a simple cron script to do some database updating, and I'm pretty worthless with MySQL without ActiveRecord (I use CodeIgniter). I keep getting the error message,
mysql_fetch_object(): supplied argument is not a valid MySQL result resource
with the following code:
mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("my_db") or die(mysql_error());
$query = "select visit_e_id, visit_e_type from visits";
$result = mysql_query($query)
or die("Query failed: ".mysql_error()." Actual query: ".$query);
while($row=mysql_fetch_object($result))
{
....
}
Like I said, I'm not that great with straight PHP and MySQL (would appreciate any advice on how to include some sort of framework or ActiveRecords that could be used as part of a cron job). Any thoughts?
What happens if you assign mysql_connect as a variable and pass it in as a link identifier as a second parameter to the mysql_select_db and query functions?
$db = mysql_connect("localhost", "user", "pass") or die(mysql_error());
// Check to see if valid connection
var_dump($db);
mysql_select_db("my_db", $db) or die(mysql_error());
$query = "select visit_e_id, visit_e_type from visits";
$result = mysql_query($query, $db)
or die("Query failed: ".mysql_error()." Actual query: ".$query);
while($row=mysql_fetch_object($result))
{
....
}
Also ensure that you query syntax is correct and doesn't have misspelling or typos
This error is pretty simple and self-explanatory - a $result variable has unexpected type.
Thus, you have to so some debugging. Add var_dump($result); before and inside loop and study the output.