I have spent the last 2 hours trying to solve this problem. I keep getting this error when trying to connect my php script with mySqli that is all hosted on a WAMP local host.
https://pastebin.com/HrJsnspG
;extension_dir = "./"
; On windows:
extension_dir = "C:\PHP7\ext"
I have added my pastebin above that shows my php.ini file. I changed the extensions as people have suggested on stack overflow and removed the ';' but nothing has worked, i still get the error. I have also reinstalled my WAMP server.
Any further suggestions?
<?php
$servername = "localhost";
$username = "root";
$password = "";
//create connection
$conn = new sqli($servername, $username, $password);
//check fann_get_total_connections
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected Successfully";
?>
To connect properly with MySQLi you need to create an instance of mysqli class, enable error reporting and set the correct charset.
<?php
// Connection code
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = new \mysqli('localhost', 'testuser', 'password', 'dbname');
$conn->set_charset('utf8mb4');
For more information see https://phpdelusions.net/mysqli/mysqli_connect#oop
Related
This question already has answers here:
PHP 7 cannot find MySQLi
(2 answers)
Closed 3 years ago.
I have php7 installed and working
I'm using the Built-in HTTP server
Uncommented "extension_dir="C:\php\ext" and extension="php_mysqli.dll" in the php.ini files development and production (did production too just to be safe)
When I used the phpinfo() method, I found that it says "Loaded Configuration file: none"
I've installed php7 therefore mysqli is supposed to already be installed. Looking online I found that you have to edit the php.ini file (mentioned how I did so in bullet points above) however when I run:
$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";
?>
I get the error:
Fatal error: Uncaught Error: Class 'mysqli' not found in C:\Users\Calvin\try\learningMySQL.php:17 Stack trace: #0 {main} thrown in C:\Users\Calvin\try\learningMySQL.php on line 17
Thank you, any help is appreciated.
$servername = "localhost";
$username = "username";
$password = "password";
$database = "database";
$con = mysqli_connect("$servername","$username","$password","$database");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
I've been trying to upload my PHP MySQL(in Dreamweaver) project to a free web-hosting site.
When I logged in, there is an error that appear in dbconn.php file.
The error is shown below:
and here's the code in my dbconn.php file:
<?php
/* php& mysqldb connection file */
$user = 1350048; //mysqlusername to db
$pass = "password"; //mysqlpassword to db
$host = "eskl.freeoda.com"; //server name or ipaddress
$dbname= 1350048; // db name in server freeoda
$dbconn= mysql_connect($host, $user, $pass);
if(isset($dbconn)){
mysql_select_db($dbname, $dbconn) or die("<center>Error: " . mysql_error() . "</center>");
}
else{
echo "<center>Error: Could not connect to the database.</center>";
}
?>
I would really appreciate if anyone can teach me how to solve this.. thanks in advance!
As Kerbholz already stated, don't use mysql_* functions, they are really outdated.
Instead use mysqli:
$servername = "eskl.freeoda.com";
$username = "1350048";
$password = "password";
$database = "1350048";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
For your error it got mostly something to do your host doesn't allow remote connections. Try to change the serverhost to localhost or 127.0.0.1
I am trying to learn php on my own. Things were OK till i reach DB connection.
I am using NetBeans 8.1 and had installed xampp to set up mysql and apache server. But when i try to connect to DB using
$dbPassword = "PHPpassword";
$dbUserName = "PHPuser";
$dbServer = "localhost";
$dbName = "PHPFirstDB";
$connection = new mysqli($dbServer, $dbUserName, $dbPassword, $dbName);
print_r($connection);
Ideally i should be seeing $connection details but I am not able to see anything in the script output window. I have set runas : script(run in command line)
Can anyone help me here...
Try this code, it could be possible that the error is occurring but not being displayed.
ini_set('display_errors',1);
error_reporting(E_ALL ^ E_NOTICE);
$connection = new mysqli($dbServer,$dbUserName,$dbPassword,$dbName);
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
}
print_r($connection);
I am trying to connect to a mssql server on my mac through pdo_dblib. I have the freetds.conf updated to the host I want to connect. My phpinfo tells me that I have all the driver hooked up and good to go. Below is the code that I wrote to test if I can connect to the server.
<?php
$servername = "IP";
$port = "port";
$username = "username";
$password = "password";
$myDB = "database";
try {
$conn = new PDO("dblib:dbname=$myDB;host=$servername:$port", "$username", "$password");
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
However I get an error:
Connection failed: SQLSTATE[] (null) (severity 0)
I tried using SQL Developer to connect to the mssql server and it worked. I have been trying to solve this problem for a whole day. What might be the problem? Thanks!
Remove the quotes from the variables in the connection string -
$conn = new PDO("dblib:dbname=$myDB;host=$servername:$port", $username, $password);
I found out the answer!
You have to uncomment TDS version protocol in /usr/local/Cellar/freetds/0.91/etc/freetds.conf.
Found the solution in Why won't my server connect to a remote MSSQL server using PHP mssql_connect?
but why do I have to do this...no idea...wish some one could give some explanation.
I feel like a total n00b for not understanding what I'm doing wrong, but here goes.
I'm writing a simple web form that's storing information in a MySQL database. I'm getting this error:
mysqli_stmt_init() expects parameter 1 to be mysqli, null given in /myfile.php
Here's my code:
$server = 'localhost';
$username = 'myusername';
$password = 'mypassword';
$db = 'mydb';
$link = mysqli_connect($server, $username, $password, $db);
.
.
.
$stmt = mysqli_stmt_init($link); /*This line throws the error*/
Any ideas? Thanks in advance.
Are you 100% sure you are successfully connecting to the database?
Add at the top of your script:
error_reporting(E_ALL);
ini_set('display_errors', 1);
Also add right below your connection line:
if (mysqli_connect_errno()) {
printf("Connect failed: %s", mysqli_connect_error());
exit;
}
If this is your dev server you should probably set the following lines in your php.ini file rather than in each script.
error_reporting = E_ALL
display_errors = on
if that doesn't solve you problem than you should print our $link to see what it is.