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.
Related
This question already has answers here:
mysqli_query() expects at least 2 parameters, 1 given
(5 answers)
Closed 2 years ago.
I'm new with PHP and I'm trying to solve this problem but I don't know how.
<?php
session_start();
require "../config.php";
$retvaloff = mysqli_query($con);
session_destroy();
header("Location: index.php");
?>
PHP Warning: mysqli_query() expects at least 2 parameters, 1 given in logout.php on line 4
As it says, there is only 1 parameter and it must be 2. Which parameter could I add to mysqli_query?
Thanks!
You should send two parameters to mysqli_query:
db connection and
query.
for reference
At this moment you only send db connections to this function.
First parameter to the mysqli_query function is your connection resource (I assume $con in your case), second parameter is the query (string).
PS: Make sure to protect against SQL injection.
This question already has answers here:
Warning: mysqli_select_db() expects exactly 2 parameters, 1 given
(2 answers)
Closed 6 years ago.
Attempting to solve this issue. I'm trying to restore a extremely old version of Wordpress, back when it was known as B2, into a working state. After making a bunch of edits to the code in a attempt to get it working, I'm now getting this:
Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in /home/Redacted/public_html/b2-include/b2functions.php on line 31
Here's the segment of the code that's having issues:
<?php
/* functions... */
function get_currentuserinfo() { // a bit like get_userdata(), on steroids
global $HTTP_COOKIE_VARS,$user_login,$userdata,$user_level,$user_ID,$user_nickname,$user_email,$user_url,$user_pass_md5;
// *** retrieving user's data from cookies and db - no spoofing
$user_login = $HTTP_COOKIE_VARS["cafeloguser"];
$userdata = get_userdatabylogin($user_login);
$user_level = $userdata["user_level"];
$user_ID=$userdata["ID"];
$user_nickname=$userdata["user_nickname"];
$user_email=$userdata["user_email"];
$user_url=$userdata["user_url"];
$user_pass_md5=md5($userdata["user_pass"]);
$pref_usequicktags=$userdata["pref_usequicktags"];
$pref_postnavigator=$userdata["pref_postnavigator"];
$pref_showinactiveusers=$userdata["pref_showinactiveusers"];
$pref_textarearows=$userdata["pref_textarearows"];
$pref_confirm=$userdata["pref_confirm"];
$pref_usespellchecker=$userdata["pref_usespellchecker"];
// *** /retrieving
}
function dbconnect() {
global $connexion, $server, $loginsql, $passsql, $base;
$connexion = mysqli_connect($server,$loginsql,$passsql) or die("Couldn't connect! So sad :( <p>You should look into this!</p>");
$connexionbase = mysqli_select_db($base) or die("Couldn't connect! So sad :( <p>You should look into this!</p>");
return(($connexion && $connexionbase));
}
I am getting the error when I load the site at all. I can provide more code if necessary.
Posting as a community wiki. I don't want rep from this and there shouldn't.
$connexionbase = mysqli_select_db($base)
Just as the error states. You need to pass the db connection as the first argument:
$connexionbase = mysqli_select_db($connexion, $base)
Reference:
http://php.net/manual/en/mysqli.select-db.php
Example from the manual:
bool mysqli_select_db ( mysqli $link , string $dbname )
Sidenote:
return(($connexion && $connexionbase));
TBH, I've never seen this type of syntax for a return. Far as I know, you can return only once or using an array.
Pulled from this answer https://stackoverflow.com/a/3815243/1415724
You can only return one value. But you can use an array that itself contains the other two values:
return array($uid, $sid);
Instead of going through all that trouble, just use the 4 parameters:
$connexion = mysqli_connect($server,$loginsql,$passsql, $base)
as per the manual:
http://php.net/manual/en/function.mysqli-connect.php
then return with and if it's really needed.
return $connexion;
Plus, why are you intending to use MD5 to store passwords with? That hashing function is no longer considered safe to use.
You're better off using password_hash().
http://php.net/manual/en/function.password-hash.php
This is the 21st century after all.
and $HTTP_COOKIE_VARS, that's deprecated.
http://php.net/manual/en/reserved.variables.cookies.php
I've no idea why you're using that code or where you got it from.
You need to pass the database name into your mysqli_select_db() function. Right now you're passing the connection improperly, you need to have that second parameter or it doesn't know what to do. Try updating it to this:
$connexionbase = mysqli_select_db(connexion, $base) ...
I've searched the site for an answer but couldn't find any answer for my specific problem that worked.
I have two files, the first one is my index.php of-course, the second is my functions.php file, which obviously contains the functions, I made this function:
function sql_get($lang, $tabler, $rower){
if ($lang == "heb") {
$result = mysql_query("SELECT * FROM %s WHERE id = 0",
mysql_real_escape_string($tabler));
$row = mysql_fetch_array($result);
return $row['$rower'];
}
else if ($lang == "rus") {
$result = mysql_query("SELECT * FROM %s WHERE id = 0",
mysql_real_escape_string($tabler));
$row = mysql_fetch_array($result);
return $row['$rower'];
}
this code supposed to get an information about the language (from a get, it gets it, it's all fine with that), the sql table and the specific row from this table where the id is 0.
and return the information from the row inserted.
My warnings and errors when the language is "heb":
Warning: mysql_query() expects parameter 2 to be resource, string given in /home/elenbyin/public_html/elenby.co.il/vadim/functions.php on line 16
Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in /home/elenbyin/public_html/elenby.co.il/vadim/functions.php on line 17
and when the language is "rus":
Warning: mysql_query() expects parameter 2 to be resource, string given in /home/elenbyin/public_html/elenby.co.il/vadim/functions.php on line 23
Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in /home/elenbyin/public_html/elenby.co.il/vadim/functions.php on line 24
the function call in the index.php file looks like that:
I will be thankful for the people who will try to help and especially for those who will help me.
Thanks A lot!
Firstly, you haven't connected to the database inside your function. If you've opened your database somewhere then you can get away with this with mysql, but you shouldn't.
Secondly, you're passing your variable as a second parameter to mysql_query(), but mysql_query() expects the query to be complete as the first parameter, and the second parameter should be the connection resource tht connects to the database, which you haven't got.
From the structure of the query it looks like you intend to use sprintf() to create it.
This should create the query for you, assuming you have opend a databse connection:
$query = sprintf("SELECT * FROM %s WHERE id = 0",
mysql_real_escape_string($tabler));
$result = mysql_query($query) or die(mysql_error());
Note: mysql is deprecated. You shouldn't use it. Use mysqli or PDO instead. You'll need to be more rigorous about passing in your database connecions if you use mysqli.
As Amal suggested - better not use mysql_* functions cause not only they're deprecated but also vulnerable to sql-injection.
That said, in order to fix your code you should do:
$table = mysql_real_escape_string($tabler);
$sql = "SELECT * FROM $table WHERE id = 0";
$link=mysql_connect('host','user','pass');
$result = mysql_query($sql, $link);
...
as the second (and optional) parameter of mysql_query should be a resource - not a string!
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result
I am connected with my database and there seems to be an error appearing on this line of code:
while($br = mysql_fetch_assoc($brand))
and on my query I put this:
$brand = mysql_query("Select * from genratb");
The error says
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\videoshop\index.php on line 166
The first command is actually my line 166.
It looks like your query failed. The mysql_query call probably returned false, instead of the result resource.
$brand = mysql_query("Select * from genratb");
if (!$brand)
{
//error, query failed
}
else
{
while($br = mysql_fetch_assoc($brand))
{
//use row
}
}
If they query fails, then mysql_query() will return false. In that case, you need to look at mysql_error() to find out why the query failed.
The PHP documentation states:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
Most likely your query is failing for some reason, and setting $brand to FALSE. Are you sure your database connection is working, and your table exists?
You can add after your query line:
if (FALSE===$brand) { die(mysql_error()); }
This should tell you what is going wrong.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP Error: mysql_fetch_array() expects parameter 1 to be resource, boolean given
I am trying to build an array from a mysql object after querying.
The number or rows returned in the query is 68, this is why I thought a forloop would be good. However php does not like my code inside the forloop.
Here is the code:
$result = $db->query("SELECT * FROM `networktariff`");
$rows = mysql_num_rows($result);
for ($i= 0; $i<$rows; $i++)
{
$tariffs[$i] = mysql_fetch_assoc($result[$i]);
}
I am getting an error message saying :
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given in /usr/local/www/apache22/data/quote/index.php on line 58
There is no such thing like "mysql object".
Your $db object belongs to some user-defined class and there is no point in asking anyone about this class' behavior.
Assuming that query() method just utilizing ordinary mysql_query() function call, it does return a resource, not object.
If you have a strange idea of using some class to run the query yet bare API functions for the rest,
$tariffs = array();
$result = $db->query("SELECT * FROM `networktariff`");
while ($row = mysql_fetch_assoc($result) {
$tariffs[] = $row;
}
I dunno though why your class doesn't have some helper function to get the whole data at once, something like
$tariffs = $db->query2arr("SELECT * FROM `networktariff`");
Yup, read the docs on mysql_fetch_assoc().
A better construction might be:
while($row = mysql_fetch_assoc($result)) {
// Do stuff with $row
}
To elaborate: $result does not contain any data. It's a "resource" handle that points to a resultset that MySQL has created. To access the data of the resultset, you use mysql_fetch_* and pass it the resource handle. Each time you call a fetch function, the internal pointer is incremented one row. So you'll get fresh data each time until MySQL reaches the end of the results, at which point you'll get FALSE. This is why the while() loop works.
print $result as $db->query is not standard php function, it might return you result set array.
try same code with replacing $db->query to mysql_query