error on mysqli::connect_errno - php

I'm trying to start working with mysqli and OOP, but it isn't working out that well. I can't get my mysqli methods working starting with a simple connect_errno method.
Can anyone help me out?
The connection to the database seems to be made, since I tried it with an other Password (access denied). With the correct Password I got the following error:
Fatal error: Call to undefined method mysqli::connect_errno() in
C:\xampp2\htdocs\badeendenrace\db\db.php on line 7
<?php
$dbhost='localhost';
$dbuser='Tjerk';
$dbdata='badeendenrace';
$dbpw='123test';
$db = new mysqli($dbhost,$dbuser,$dbpw,$dbdata);
if ($db->connect_errno())
{
echo "Connection failed: ".$db->connect_error()."\n";
exit();
}
?>
I'm using the latest version of Xampp (Reïnstalled it today) with PHP version 5.5.6

As Rocket Hazmat said, connect_errno isn't a function. The same to connect_error. So just take away the round brackets after both in your code.
if ($db->connect_errno)
{
echo "Connection failed: ".$db->connect_error."\n";
exit();
}

It seems that you have confused the object oriented function with the procedural. You mention OOP so I assume you want the OOP version.
$db = new mysqli($dbhost,$dbuser,$dbpw,$dbdata);
if ($db->connect_errno) //no parenthesis
{
echo "Connection failed: ".$db->connect_error."\n"; //no parenthesis
exit();
}
For procedural
$db = new mysqli($dbhost,$dbuser,$dbpw,$dbdata);
if (!$db)
{
echo "Connection failed: ".mysqli_connect_error(); //parenthesis
exit();
}

Related

PHP Exits When I Try to Connect to a Database Using MySQLi

I have been developing a website recently, and I came across an error. When I tried to do print_r(error_get_last()); It output some text about mysql not being supported anymore, and to use MySQLi. I was all for it. When I edited my database class to support MySQLi, it exits every time it tries to connect. I have put echo 'hi<br />'; before and after the mysqli function, and a or trigger_error("Error: " . print_r(error_get_last())); at the end of it, but all it outputs is hi, and nothing else. My database is created in phpMyAdmin, and has all the correct permissions, but it just cannot connect. This is my connection code:
echo 'hi<br />';
$this->db = mysqli($host, $dbuser, $dbpass, $dbname) or trigger_error("Error: " . print_r(error_get_last()));
echo 'hi<br />';
$this->connect_start = microtime(true);
if ($this->db->connect_errno > 0) die ($this->debug(true));
It is in the constructor of a class that looks like this:
function db($host, $dbname, $dbpass, $dbuser)
and it is called like this:
$db = new db($host, $dbname, $dbpass, $dbuser);
Change this:
$this->db = mysqli()
to
$this->db = new mysqli()
mysqli is not a function, it's a class. If you had error reporting on you'd get a message like this:
PHP Fatal error: Call to undefined function mysqli()
That's the reason why the rest of your code isn't executed, the execution stops right there.
Also note that new mysqli() will never return false, so your or ... part becomes useless. If you want to check for connection errors you have to check $this->db->connect_error after the connection attempt.

Access denied for user ''#'localhost' (using password: NO) using AWS and EC2

I am getting the following error when using SHIFTEDIT IDE to try connect to my amazon EC2 instance running LAMP server and mysql server.
The code I am writing in PHP to connect to my sql server is as followed:
<?php
function connect_to_database() {
$link = mysqli_connect("localhost", "root", "test", "Jet");
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
mysqli_close($link);
}
?>
OUTPUT: Success: A proper connection to MySQL was made! The my_db database is
great. Host information: Localhost via UNIX socket Access denied for
user ''#'localhost' (using password: NO)
I am definitely using the right password for root as I can successfully login when using Phpmyadmin, I just cannot make a connection with PHP for some reason.
Currently, I have a single Amazon ec2 instance with LAMP server and a MySQL server installed. Any help will be much appreciated.
EDIT: I am using Php 5.6.17
When you create a mysqli instance (either by new mysqli(...) or mysqli_connect(....) within a function/method, you have to take php's variable scope into account. Return the mysqli instance from the function and let the caller use and/or assign that instance.
<?php
/*
builds and throws an exception from the error/errno properties of a mysqli or mysqli_stmt instance
#param useConnectError true: use connect_error/connect_errno instead
#throws mysqli_sql_exception always does
*/
function exception_from_mysqli_instance($mysqli_or_stmt, $useConnectError=false) {
// see http://docs.php.net/instanceof
if ( !($mysqli_or_stmt instanceof mysqli) && !($mysqli_or_stmt instanceof mysqli_stmt) {
// see docs.php.net/class.mysqli-sql-exception
throw new mysqli_sql_exception('invalid argument passed');
}
else if ($useConnectError) {
// ok, we should test $mysqli_or_stmt instanceof mysqli here ....
throw new mysqli_sql_exception($mysqli_or_stmt->connect_error, $mysqli_or_stmt->connect_errno);
}
else {
throw new mysqli_sql_exception($mysqli_or_stmt->error, $mysqli_or_stmt->errno);
}
}
/* creates a new database connection and returns the mysqli instance
#throws mysqli_sql_exception in case of error
#return valid mysqli instance
*/
function connect_to_database() {
$link = new mysqli("localhost", "root", "test", "Jet");
// see http://docs.php.net/mysqli.quickstart.connections
if ( $link->connect_errno) {
// a concept you might or might not be interested in: exceptions
// in any case this is better than to just let the script die
// give the other code components a chance to handle this error
exception_from_mysqli_instance($link, true);
}
return $link;
}
try { // see http://docs.php.net/language.exceptions
// assign the return value (the mysqli instance) to a variable and then use that variable
$mysqli = connect_to_database();
// see http://docs.php.net/mysqli.quickstart.prepared-statements
$stmt = $mysqli->prepare(....)
if ( !$stmt ) {
exception_from_mysqli_instance($stmt);
}
...
}
catch(Exception $ex) {
someErrorHandler();
}
and a hunch (because of the actual error message; trying to use the default root:<nopassword> connection, that's the behaviour of the mysql_* functions, not of mysqli):
Do not mix mysqli and mysql_* functions.

MSSQL connection using PHP

Iam so fresh with PHP so this problem so big to me. I dont know why and where my mistake is. Based on my research to connect to MSSQL database by using this code:
<?php
$run = mssql_connect('dev-svr05','sa','P#55w0rd', 'orlig_sm_dev');
if ($run)
{
echo "Connection OK";
}
else
{
echo "Connection Failed";
}
?>
But when i run this code i got this error message:
PHP Fatal error: Call to undefined function mssql_connect() in C:\Inetpub\wwwroot\phpscript\save_mssql.php on line 5
I using the same code to connect to MYSQL and its success but not with MSSQL. Can anybody please tell me why this is happen? Thank You
I would advice you download the sql server binaries from windows and use PDO.
try {
//In some occasions you only need to define IP/Hostname and you can forgo the \SQLEXPRESS part
$db = new PDO( "sqlsrv:Server=HOSTNAME\SQLEXPRESS;Database=DATABASENAME","USERNAME","PASSWORD");
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
// set this to your primary database
$db->query("USE DATABASENAME");
}
catch( PDOException $e ) {
die( "Error connecting to SQL Server".$e );
}
Fourth parameter is expecting a true/false. Refer the below link and try:
http://php.net/manual/en/function.mssql-connect.php
Try "sqlsrv_connect" instead of "mssql_connect"

Connecting to Oracle database from PHP

I have an Oracle database that I am trying to connect to.
For some reason when I try the following code:
<?php
include "header.php";
// simply attempt to connect to the database
/* If you are connecting to the Oracle database, the credentials are as follows:
* Username: ********
* Password: ********
* Hostname: **********
* Port: 1521
* Service name: ***********
*/
$oracleConnect = true;
if ($oracleConnect)
{
echo 'Attempting connection...<br>';
$connection = null;
try
{
$connection = oci_connect('user',
'pass',
'user#//hostname:1521/dbname');
}
catch (Exception $e)
{
echo $e->getMessage();
}
if (!$connection)
{
echo '<p>Something is wrong.</p>';
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
// if the connection has been established
else
{
// tell the user and close it (this is a test)
echo 'Connection established!!';
oci_close($connection);
}
}
else
{
$connection = new mysqli('host', 'user', 'password', 'database');
echo ($connection) ? 'Database connection successful!' : 'Could not connect.';
}
include "footer.php";
?>
When I try the above code, I get the "Attempting connection..." to print, but nothing else. It is supposed to print something else regardless. What could possibly be going wrong?
I think the problem is the user# part of your connection string. I dont think thats necessary as oci_connect has a user name parameter. I could be wrong, ive never used oracle from php before, but the docs on oci connections would also seem to indicate that:
To use the Easy Connect naming method, PHP must be linked with Oracle 10g or greater Client libraries. The Easy Connect string for Oracle 10g is of the form: [//]host_name[:port][/service_name]. From Oracle 11g, the syntax is: [//]host_name[:port][/service_name][:server_type][/instance_name]. Service names can be found by running the Oracle utility lsnrctl status on the database server machine.
Also oci_connect does not throw an exception as far as i can tell so your try/catch is useless unless you planned on throwing your own when it returns false.

Undefined function, MYSQL error

When submitting the following form I am getting this error :
Fatal error: Call to undefined function mysqli_connect() in .... mailing_list_include.php on line 7
Here's the mailing_list_include.php file - the real thing includes the correct credentials for accessing the db
<?php
function doDB() {
global $mysqli;
// connect to server and select database; you may need it
$mysqli = mysqli_connect("localhost", "username",
"password", "db");
// if connection fails, stop script execution
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
}
function emailChecker($email) {
global $mysqli, $check_res;
//check that email is not already in list
$check_sql = "SELECT id FROM SUBSCRIBERS
WHERE email = '".$email."'";
$check_res = mysqli_query($mysqli, $check_sql)
or die(mysqli_error($mysqli));
}
?>
This means your copy of PHP was not compiled with mysqli support. This doesn't mean there is anything wrong with your code, you are simply trying to use a function that PHP does not have available.
See Installation - Mysqli on PHP.net for more information on configuring it. You will have to ask your host if they can rebuild PHP and include mysqli support.
Installation of the mysqli extension

Categories