This question already has an answer here:
PHP connection error with the database [closed]
(1 answer)
Closed 8 years ago.
I keep getting this error each time I run my .php page
It is supposed to show images taken from the server.
This page does show all the images but also error which is:
"Warning: mysqli_select_db() expects parameter 1 to be mysqli, string
given in F:\users\1203158\httpdocs\Adventure_Sports\gallery.php on
line 79 "
I do not understand how to eliminate this error
You presumably have:
$link = mysqli_connect( ... );
And my guess is you're doing:
mysqli_select_db("database_name");
That's how it was done with mysql_select_db, but with mysqli_, you have to pass the connection handler.
mysqli_select_db($link, "database_name");
The error is telling you that it's expecting a mysqli object (the connection handler) but is getting a string (the database name).
Related
This question already has an answer here:
Should we ever check for mysqli_connect() errors manually?
(1 answer)
Closed 2 years ago.
I keep getting this error"
Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\php-contact-form\index.php on line 8
this is the connection i used:
$conn = mysqli_connect("localhost","root","Guyle4u2021","newDolce") or die("Connection Error: " . mysqli_error($conn));
By the looks of it, you need to remove the "or die("Connection Error: " . mysqli_error($conn))" and see what happens.
Also, id recommend switching to PDO because it's more consistent and is more widely used by the community, therefore additional support.
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.
So I took a website which is currently live and transferred everything to my computer so I can work on it locally. I am in the process of getting everything working locally but can't get my MySQL queries to work. I am connected to a database which is identical to the original database that the website was running on.
The error I'm getting is:
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in...
(yes I am going to move everything to mysqli once I get this working)
I've tested very simple queries and had no luck. Here is one I tried:
<?php
$con = mysql_connect('localhost', 'root', null, 'database_name') or die("Could not connect.");
echo mysql_result(mysql_query("SELECT `data` FROM `test-table` WHERE `id` = 0"), 0);
?>
Does this have something to do with the fact that I am running locally?
I'm running this on WAMP, Apache/2.4.2 (Win64) PHP/5.4.3
You need to use mysql_select_db('database_name') instead of incorrectly passing the database name to the mysql_connect function.
See the manuals:
mysql_connect
mysql_select_db
If I were in the process of updating from mysql_* to mysqli_* I would first change my functions then debug as it will save from doing debugging only to change it and debug again. Just a thought...
This question already has answers here:
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in
(3 answers)
Closed 8 years ago.
I'm sure this is a duplicate, but I can not find an answer to this. For whatever reason, I can not query my database.
Simply I am trying to call a function but I get the error...
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in ... on line 12
My dbConnect.php file has
$adConn = mysqli_connect("myHost","myUser","myPword","myDB");
My functions.php files has
require "dbConnect.php";
// Check connection
if (mysqli_connect_errno($adConn))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//-------------------------------------------------//
// Gets total number of games for a given week //
//-------------------------------------------------//
function GetGamesCount($wID){
$sql = "SELECT * FROM schedule WHERE weekID=$wID";
$gamesRS = mysqli_query($adConn, $sql);
$games = mysqli_fetch_array($gamesRS);
}
I get no error from the connection check, I have tried putting it within the funcation as well. I know I can use the mysqli_num_rows function, which I will. I just need to figure out why it won't query the database. I have tried querying with other functions with the same problem. However, if I query within the page or using ajax from a separate file, I have no problems.
I just don't see what I'm missing. Any help is greatly appreciated.
Because $adConn is declared outside of the function is not in scope. That means you do not have access to it inside the function. To have access to it you need to pass it as aparameter of the function.
function GetGamesCount($wID, $adConn){
$adConn is in the global scope and you cannot access it from the function, if you change $adConn to $GLOBALS['adConn'] it should work.
This question already has an answer here:
Should we ever check for mysqli_connect() errors manually?
(1 answer)
Closed 2 years ago.
How do you prevent a connection error if, for example, the database name is invalid, for the given code below?
$mysql = new mysqli('localhost', 'user', 'pass', 'wrongdatabase');
if($mysql->connect_errno)
die($mysql->connect_error);
The if statement will output the right error message, but there will still be a warning sent from the first line, which says
Warning: mysqli::mysqli() [<a href='mysqli.mysqli'>mysqli.mysqli</a>]: (42000/1049): Unknown database 'wrongdatabase' in C:\wamp\www\example.php on line 14
I know with PDO you would simply wrap it in a try/catch block, but how to do it with MySQLi?
The answer here was incorrect and outdated. A more recent answer to this question can be found here
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Warning: mysql_query(): 3 is not a valid MySQL-Link resource
I don't know why the $GUconnection resource isn't working on queries since I set it up as a global variable. The db connection is
$GUconnection = mysql_connect(serverip, username, password);
#mysql_select_db(dbname, $GUconnection) or die('Cannot connect to the database.');
The following query is located in an include file that is included in the file containing the mysql connection above:
global $GUconnection;
$GUresult = mysql_query("SELECT field FROM `tablename` WHERE field = 'hey' LIMIT 1", $GUconnection);
if(mysql_num_rows($GUresult)) {
$GUfile = mysql_fetch_assoc($GUresult);
}
The errors I got where
Warning: mysql_query(): 3 is not a valid MySQL-Link resource in /home/ on line 49
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/ on line 50
If I don't have the include and just paste the query directly underneath the connection then it works. What seems to be the problem?
Feel the power of google.
While writing your question on SO brings you no help, exact error message pasted into google's search box finds you complete solution. In much less time.
The connection may fail, so you should check for connection errors.