PHP SQL Server Connection working or not working [duplicate] - php

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Reference - What does this error mean in PHP?
(38 answers)
Closed 4 years ago.
$serverName = 'servername';
$uid = 'username';
$pwd = 'password';
$conn = new mysqli($serverName, $uid, $pwd );
if (!$conn) {
echo "Connection failed: " ;
}
else
{
echo "Connected successfully";
}
This is my code. It gets connected to the database. I just want to confirm the code is right, because when i try doing this
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
it throws me connection failed error.
So, is my code connected to the server or not? because when i run a query it does not show me anything.
Code for the query:
$sql = "SELECT max([line_nbr]) FROM [dbo].[so_audit]";
$res = $conn->query($sql);
var_dump($res);
please advise

you are not trying to connect to sql server u should use sqlsrv_connect instead of mysqli
so u need to specify the server name and an array containin connection info for that your code should look like this :
$srv ="servername"
$info=array( "Database"=>"dbName", "UID"=>"userName", "PWD"=>"password");
$conn = sqlsrv_connect( $srv, $info);
if (!$conn) {
echo "Connection failed: " ;
}
else
{
echo "Connected successfully";
}

Related

PHP Error "mysqli::mysqli(): (HY000/2002)" [duplicate]

This question already has answers here:
mysqli::mysqli(): (HY000/2002): Can't connect to local MySQL server through socket 'MySQL' (2)
(5 answers)
Closed 3 years ago.
I'm having a problem running php code. The error only appears for this php file, other simple ones work fine. I'm using xampp. apache and mysql running green.
<?php
$username = filter_input(INPUT_POST, 'username');
$password = filter_input(INPUT_POST, 'password');
if (!empty($username)){
if (!empty($password)){
$host = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "test";
// Create connection
$conn = new mysqli ($host, $dbusername, $dbpassword, $dbname);
if (mysqli_connect_error()){
die('Connect Error ('. mysqli_connect_errno() .') '. mysqli_connect_error());
}
else{
$sql = "INSERT INTO account (username, password)
values ('$username','$password')";
if ($conn->query($sql)){
echo "New record is inserted sucessfully";
}
else{
echo "Error: ". $sql ." ". $conn->error;
}
$conn->close();
}
}
else{
echo "Password should not be empty";
die();
}
}
else{
echo "Username should not be empty";
die();
}
?>
Finally got the solution to this problem, I'm sharing it because I can't find a solution anywhere else. The $host should be linked to "127.0.0.1" but for an unknown reason my host only worked when it was "127.0.0.1:3307" or the name of your port for mysql at the end, don't forget the ":". PS I changed my db connection back to using the procedural way despite what the Ashu said and it worked.
When you are using Object-oriented mysqli connection then you have to use all Object-oriented method.
In below code you used procedural way:
if (mysqli_connect_error)
{
die('Connect Error ('. mysqli_connect_errno.') '. mysqli_connect_error);
}
Replace with :
if ($conn->connect_error)
{
die('Connect Error ('. $conn->connect_errno.') '. $conn->connect_error);
}
Use 127.0.0.1 instead of localhost

PHP coding got an error [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
<?php
$conn=mysql_connect(`localhost'," root","") or die("Could not connect");
mysql_select_db("bng_nov",$conn) or die("could not connect database");
?>
Parse error: syntax error, unexpected end of file, expecting '`' in C:\xampp\htdocs\display\db.php on line 4
Error in this line replace (`) with (') single quote. also remove space before root
$conn=mysql_connect('localhost',"root","") or die("Could not connect");
and best solution is use double quotes like this
$conn=mysql_connect("localhost","root","") or die("Could not connect");
also i mention that don't use mysql_*. you should used mysqli_*
i have add some DB connectivity codes. used this code
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
error in the below line
$conn=mysql_connect(`localhost'," root","")
please try like below
<?php
if (!$link = mysql_connect('localhost', 'root', '')) {
echo 'Could not connect to mysql';
exit;
}
if (!mysql_select_db('bng_nov', $link)) {
echo 'Could not select database';
exit;
}
?>

Connecting to mysql using php through xampp.... Database connection problems [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 6 years ago.
I've installed xampp and it seems to be up and running, I have tested php by executing the phpinfo() function and it works. I can create databases and manipulate them in phpmyadmin, and the localhost server works too
How ever when I attempt to actually connect through php....
<?php
$conn = mysqli_connect('localhost', 'root', '');
mysql_select_db("testskills");
if(!$conn) {
die("Connection Failed: ". mysqli_connect_error());
}
..... I don't get any errors but the code breaks and the browser just shows me the actual code from the file the form action called
I'm stumped
Lori
Try this
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
<?php
$host_name = "localhost";
$u_name = "root";
$u_pass = "";
try {
$dbh = new PDO("mysql:host=$host_name;dbname=personal_db", $u_name, $u_pass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $ex) {
echo "Connection failed: " . $ex->getMessage();
}
?>
I would suggest that you start learning to use PDO man.

cant show the results of query in php from mysql

Hello I am new at php and mysql and I don't know what is wrong.
I cant show the results from query and the connection with mysql is successfully connected.
I don't use wampserver I just install php,mysql and Apache separately.
Thanks in advance.
Code
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
$sql="select * from `books`;";
$result=mysqli_query($conn,$sql);
if (!$result){
echo "query cannot execute";
};
?>
its only show me "query cannot execute"
You need to pass fourth parameter database name in mysqli_connect()
It would be
$conn = mysqli_connect($servername, $username, $password,"YOUR_DATABASE");
Read http://php.net/manual/en/mysqli.error.php to check error in query.
Read http://php.net/manual/en/mysqli-result.fetch-array.php
To fetch data from query result

PHP error, help mee [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
When I try to insert mysql rows i get this error:
I found this code at: www.w3schools.com/php/php_mysql_insert.asp
Parse error: syntax error, unexpected T_VARIABLE in C:\xampp\htdocs\enviar.php on line 17
This is the code:
<?php
$servername = "localhost";
$username = "*****";
$password = "*****";
$dbname = "*******";
$produto = $_REQUEST['produto'];
$mesa = $_REQUEST['mesa'];
$conc = "fila"
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Send information to mysql
$sql = "INSERT INTO lanchesystem ( Produto, Mesa, conc)
VALUES ('$produto', '$mesa', '$conc')";
if ($conn->multi_query($sql) === TRUE) {
echo "Pedido ConcluĂ­do!";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
// Close connection
$conn->close();
?>
How to solve this??
Add a semicolon:
$conc = "fila";
I also recommend using a text editor with a linter. It'll save you some time and headache.

Categories