This question already has answers here:
How to change mysql to mysqli?
(12 answers)
Closed 3 months ago.
I have some existing PHP code that is now throwing a fatal error since we migrated to PHP 7.0. Using this Stackoverflow Question I've altered this line:
$link = mysql_connect($host.':'.$port, $user, $pass) or die("Can not connect." . mysql_error());
to this:
$link = new mysqli($host.':'.$port, $user, $pass);
I am now throwing this error which I guess is some progress. The syntax seems fine? What am I missing?
Warning: mysqli::__construct(): (HY000/2002): Connection timed out
The constructor class looks like this:
__construct (
[ string $host = ini_get("mysqli.default_host")
[, string $username = ini_get("mysqli.default_user")
[, string $passwd = ini_get("mysqli.default_pw")
[, string $dbname = ""
[, int $port = ini_get("mysqli.default_port")
[, string $socket = ini_get("mysqli.default_socket") ]]]]]] )
So the order is host, username, password, database, port, socket. You'll need to pass in the port in a separate variable:
$link = new mysqli($host, $user, $pass, null, $port);
Edited because I copied the wrong bit of code..
Try just using the function.
$link = mysqli_connect($host,$user,$pass,$db).
The order is host, user, pass, db
http://php.net/manual/en/function.mysqli-connect.php
Related
This question already has an answer here:
Can't connect to GoDaddy mysqli database via PHP script
(1 answer)
Closed 2 years ago.
I am trying to connect a database to a website. I am using GoDaddy and I am finding it difficult to input the following things in php.
$host = "Where do I find this? Is this a number? Do I put the IP Address: Port Number?";
$dbusername = "Does it contain quotes?" Or is it just the username?;
$dbpassword = "Does it contain quotes?" Or is it just the password;
$dbname = "Does it contain quotes?" Or is it just the database name;
// Create connection
$conn = new mysqli ($host, $dbusername, $dbpassword, $dbname);
I am assuming the port number is right next to the localhost.
$host => Specifies a hostname or an IP address(For example: localhost).
$username => Specifies the MySQL username(For example: my_user).
$password => Specifies the MySQL password(For example: my_password ).
$dbname : => Specifies the default database to be used(For example: my_db).
All these contain a name with quotes on them.
For example:
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 3 years ago.
I have this block of PHP code that is supposed to connect to a PostgreSQL database in heroku
Code Below:
function __construct() {
$host = 'hostname';
$user = 'username';
$password = 'password';
$dbname = 'db_name';
$port = '5432';
try{
$this->db = new PDO('pgsql:host=$host;dbname=$dbname;user=$user;port=$port;password=$password');
}
catch (PDOException $e){
echo 'Connection failed: ' . $e->getMessage();
}
}
The connection throws an arror as follows:
Connection failed: SQLSTATE[08006] [7] invalid port number: "$port"
Why do I get an invalid port number?
Everything seems okay when I connect via the heroku cli but the php doesn't seem to cooperate.
Please help me figure out what I may have overlooked.
Variables are only expanded in double quoted strings.
$this->db = new PDO("pgsql:host=$host;dbname=$dbname;user=$user;port=$port;password=$password");
It probably complains about $port specifically because it is trying to convert that one to an int before trying to make the connection. If it would have gotten that far it would also have complained about the hostname.
I'm running my program on PHP 5.5.24,when I simply use :
$dblink = mysql_connect($dbhost, $dbuser, $dbpass);
But the function return NULL, I try to use mysql_error() to find out what's wrong, but it also returns an empty string. I know mysql_* is deprecated after 5.5.0, but I'm dealing with a very old repository. It will cost a lot of time to change into mysqli. The function suppose to refurn false if the connection failed, no NULL. Why does it happen?
-----Update-----
Now I find that $dblink can not shown by var_dump(), var_dump($dblink) would get NULL but it's actually not null.
seems it can return null, search the source for cases when it can happen
I got the answer.
To show the result of mysql_connect(), pls use var_dump() instead of var_export(). I use var_export() and get NULL as return, but var_dump() shows the result is not null. That's strange.
mysql_connect ([ string $server = ini_get("mysql.default_host") [, string $username = ini_get("mysql.default_user") [, string $password = ini_get("mysql.default_password") [, bool $new_link = FALSE [, int $client_flags = 0 ]]]]] )
Returns a MySQL link identifier on success or FALSE on failure.
I think U have to try:
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
To see what happened.
I hope it is usefull.
This question already has answers here:
Warning: mysqli_select_db() expects exactly 2 parameters, 1 given
(2 answers)
Closed 1 year ago.
I am using an Ajax Search form which upon connecting to the database, will show search terms upon typing.
But I am getting database connection issue on my site.
This is the code I have in my connection file (db.inc):
<?php
$username = "xxxx";
$password = "xxxx";
$hostname = "xxxx";
$database = "xxxx";
mysqli_connect($hostname, $username, $password) or die(mysql_error());
mysqli_select_db($database) or die(mysqli_error());
?>
But on the frontend, I am getting errors as below:
Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in xxxx/db.inc.php on line 9
Warning: mysqli_error() expects exactly 1 parameter, 0 given in xxxx/db.inc.php on line 9
Can anyone please check what I am doing wrong? I have pasted the complete db.inc file above.
Thank you in advance for helping.
As the error is saying, mysqli_select_db() expects 2 parameters.
The first one is a link returned by mysqli_connect() and the second parameter should be the database name.
The second error means that you have to add the link to the connection as a parameter to mysqli_error.
So you'll have to write it like this:
$link = mysqli_connect($hostname, $username, $password);
if (!$link) {
die('Connection error: ' . mysqli_connect_error());
}
mysqli_select_db($link, $database));
$username = "xxxx";
$password = "xxxx";
$hostname = "xxxx";
$database = "xxxx";
$conn=mysqli_connect($hostname, $username, $password) or die(mysql_error());
mysqli_select_db($conn, $database) or die(mysqli_error());
Here, the second parameter is missing. So, the correct syntax will be
mysqli_select_db($link, $database) or die(mysqli_error());
You are using Procedural Style and in this style, two parameters will be passed in mysqli_select_db().So, the correct syntax will be
bool mysqli_select_db ( mysqli $link , string $dbname )
link
Procedural style only: A link identifier returned by mysqli_connect() or
mysqli_init()
dbname
The database name.
For more details, you can find all the details https://dev.mysql.com/doc/apis-php/en/apis-php-mysqli.select-db.html
You can also add the database name in the single line which will be
mysqli_connect($hostname, $username, $password, $database);
I'm having all sorts of trouble...
Here is the code I'm using:
$c = OCILogon('user', 'pass', 'host');
I get the following error:
PHP Warning: ocilogon(): ociopen_server: Error while trying to retrieve text for error ORA-12514 in D:\Inetpub\wwwroot**\oracle.php on line 26
Anyone know what the hell I'm doing wrong?
It's PHP4, IIS6 btw. I've tried this on PHP5, IIS7 as well, no luck.
Thanks for any help I can get... :(
You must have correctly configured TNSNAMES.ora file, where is stored information about connection to database. Oracle errorr ORA-12514 says:
TNS:listener does not currently know
of service requested in connect
descriptor
Function OCILogon have this syntax (I'am not PHP developer, so excuse me if I was not right):
resource oci_connect ( string
$username , string $password [,
string $connection_string [, string
$character_set [, int $session_mode
]]] )
In your example is on third position parametr "host". But manual says "connectin string".
This "connection string" must be coonfigured throught file $ORACLE_HOME/network/admin/tnsnames.ora file ($ORACLE_HOME is folder where is Oracle client installed).
TNSNAMES.ORA look like this(example):
TEST_DB = (DESCRIPTION =(ADDRESS_LIST
=(ADDRESS = (COMMUNITY = tcp.world)(PROTOCOL = TCP)(Host =
127.0.0.1)(Port = 1521)))(CONNECT_DATA = (SID = TESTDB_SID)))
Instead:
$c = OCILogon('user', 'pass', 'host');
You should use:
$c = OCILogon('user', 'pass', 'TEST_DB');
...TEST_DB is service name from tnsnames.ora file
And yet for complementing (my file $ORACLE_HOME/network/admin/sqlnet.ora look like this):
SQLNET.AUTHENTICATION_SERVICES= (NTS)
NAMES.DIRECTORY_PATH= (TNSNAMES, EZCONNECT)
NAME.DEFAULT_ZONE = world
NAMES.DEFAULT_DOMAIN = world
And finally PHP manual example (connection string can be inserted directly into variables in PHP):
<?php
$db ="(DESCRIPTION =
(ADDRESS =
(PROTOCOL = TCP)
(HOST = HOSTNAMEHERE)
(PORT = 1521)
)
(CONNECT_DATA = (SID = SIDNAMEHERE))
)";
$odbc = ocilogon ('user', 'pass', $db) or die( "Could not connect to Oracle database!") or die (ocierror());
?>
try using persistant connection oci_pconnect()... worked for me