Database cant connect [duplicate] - php

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);

Related

Warning: pg_query() expects parameter 1 to be resource, object given in C:\xampp\htdocs\grafig\read.php on line 6

What's wrong in the code??
<?php
$host = "localhost";
$dbuser = "tesdb";
$dbpass = "123456";
$dbname = "tesdb";
// script koneksi php postgree
$dbcon = new PDO("pgsql:dbname=$dbname;host=$host", $dbuser, $dbpass);
//$query ="SELECT * FROM air_tanah.pembayaran";
$query ="select * from air_tanah.pembayaran";
$result = pg_query($dbcon, $query) or die('Query failed');
// output result
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
echo " Denda: " . $line['denda'] ." Penyimpan: " . $line['Penyimpan'] . "<br/>";
}
// free result
pg_free_result($result);
// close connection
pg_close($dbcon);
?>
and error like this
Warning: pg_query() expects parameter 1 to be resource, object given in C:\xampp\htdocs\grafig\read.php on line 12
Query failed
The code which you have entered does not allow us to really answer to your problem, we would need to know what is assigned to $dbcon variable.
pg_query expects there to be an instance of a Resource which holds the connection. Such resource is created using pg_connect or pg_pconnect method so we would need to see the contents of your db_con.php file: be sure to remove any credentials (hide them with * symbols for example).
It seems that something else actively sets the variable: are you sure that you have created a connection using pg_connect and not PDO for example (which would answer why you have there an object and not resource).

Storing an SQL value as a PHP variable ("Object of class mysqli_result could not be converted to int")

At the moment, I'm trying to store a value from a MySQL table as a variable in PHP, so running some basic tests to make sure that I can access the variable.
I've managed to store the varaible, which will either be a 1 or a 0 (1 = server is up and running, 0 = server down).
<?php
$servername = "localhost";
$username = "*****";
$password = "*****";
$dbname = "scicomservers";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT nccpm FROM web_servers WHERE time_checked='2016-02-16 11:44:17.212126'";
$nccpm = $conn->query($sql);
if($nccpm==1){
echo("NCCPM Server is running");
}
$conn->close();
?>
When I run this code, it reads in that $nccpm is 1, and it echos the statement, however, I get the error:
Notice: Object of class mysqli_result could not be converted to int in
/Applications/XAMPP/xamppfiles/htdocs/SciCom_admin_servers/files/connect2.php
on line 17
Line 17 being the if statement: "if($nccpm==1){".
I've had a look around on here, and I think this may be because it is trying to print an array of the answers, however it will only ever be one value that I will retrieve. The column of the DB is an int.
I was wondering, what would be a better way of coding this? It clearly isn't the best practice!
Thank you very much.
$sql = "SELECT nccpm FROM web_servers WHERE time_checked='2016-02-16 11:44:17.212126'";
$ncc = $conn->query($sql);
$nccpm = $conn->fetch_array($ncc);
if ($nccpm['nccpm'] == 1)
{
// Rest of script
}
You need to fetch your query or find how many rows are returned

Object returned on mysql_num_rows() function

I am working on a login script with prepared statements in PHP procedural mysqli syntax. Here is my current code:
<?php
include "/ssincludes/functions.php";
$host = HOST;
$username = USER;
$password = PASSWORD;
$db_name = DATABASE;
$table = TABLEU;
//These includes and constants are fine I checked them all
$con = mysqli_connect($host, $username, $password, $db_name);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$myusername='test';
$mypassword='password1';
$sql="SELECT * FROM $table WHERE user_name=? and password=?";
$result=mysqli_prepare($con, $sql);
mysqli_stmt_bind_param($result, 'ss', $myusername, $mypassword);
mysqli_execute($result);
mysqli_stmt_fetch($result);
$row_cnt = mysqli_num_rows($result);
echo $row_cnt;
?>
The error returned is: Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, object given
I thought I took out all instances of OO PHP in my script? Also I understand that this may mean my query is incorrect so I ran it on MySQL in the database and all seems to be fine there:
So I am lost as to what the problem could be. I read many similar posts (maybe I'm missing one that is exactly similar to mine) and none seem to handle the problem. I appreciate your time and help.
P.S. I understand the security issues with plain text passwords and using "password1". I plan to use better security practices as I build this but I just want to get prepared statements down first.
You should use
mysqli_stmt_execute
mysqli_stmt_num_rows
Instead of the mysqli_execute and mysqli_num_rows.

Mysql_query() expects parameter 1 to be string [duplicate]

This question already has answers here:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource
(31 answers)
Closed 9 years ago.
I am pulling my hair out here. I have an html form for data entry and posting to insert_ac.php and I get this error:
Warning: mysql_query() expects parameter 1 to be string, resource given in /home/content/52/11733052/html/admin/insert_ac.php on line 24
Here is the code from insert_ac.php:
<?php
$username = "nlladmin";
$password = "password";
$hostname = "localhost";
$link = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db("nlladmin",$link)
or die("Could not select Admin Database");
// Get values from form
$make=$_POST['make'];
$model=$_POST['model'];
// Insert data into mysql
$sql="INSERT INTO assets (make, model)VALUES('$make', '$model')";
if (!mysql_query($link,$sql))
{
die('Error: ' . mysql_error($link));
}
echo "1 record added";
mysql_close();
?>
Any suggestion will be helpful.
Besides that you should not be using mysql_ functions,
you swapped the parameters in
resource mysql_query ( string $query [, resource $link_identifier = NULL ] )
That's what the error msg says.
if (!mysql_query($sql, $link))
....
works.
The first parameter is the query, the second the resource.
mysql_query($sql, $link);
the function mysql_query() just takes the query as a parameter , like the following :
mysql_query($sql);

Access denied for user ''#'localhost' (using password: NO) [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 4 years ago.
Attempting to connect to localhost sql db using the following code (Not doing anything with query at this point just want the query to execute):
<?php
$host = "localhost";
$port = 3306;
$socket = "";
$user = "root";
$password = "Password1";
$dbname = "CIIP_WIKI";
$con = new mysqli($host, $user, $password, $dbname, $port);
if(!$con)
{
echo ("db connection failed!");
die ('Could not connect to the database server' . mysqli_connect_error());
}
else {
echo ("db connection established.");
echo ("<br/>");
}
$query = sprintf("SELECT first_name FROM actor WHERE actor_id='1'");
$result = mysql_query($query);
$con->close();
?>
I keep getting the following...
Welcome
db connection established.
Warning: mysql_query(): Access denied for user ''#'localhost' (using password: NO) in C:\Program Files (x86)\EasyPHP-12.1\www\Cisco Wiki\index.php on line 31
Warning: mysql_query(): A link to the server could not be established in C:\Program Files (x86)\EasyPHP-12.1\www\Cisco Wiki\index.php on line 31
Why does this not work?
This is because you create a connection using mysqli_ and then use mysql_ to try to fetch your result. They are different API's.
<?php
/* You should enable error reporting for mysqli before attempting to make a connection */
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
/* Set the desired charset after establishing a connection */
mysqli_set_charset($mysqli, 'utf8mb4');
printf("Success... %s\n", mysqli_get_host_info($mysqli));
Example taken from the PHP manual
This warning will be logged anytime you try to execute a query without a valid connection object being available.
In your case you thought you had a valid connection object available, but you created it using mysqli while your query is being executed using mysql. Those are two different APIs and so the mysql_query function was looking for a mysql connection object and didn't find it.

Categories