chiefboz Posted November 1, 2012 Share Posted November 1, 2012 Hello — This is probably one of the most common mysqli errors out there, but it has appeared on my webpage. The problem is on line 10. This file is used as a global functions file - so I can essentially use db_query(INSERT INTO...) in a different page and have the function run. For security reasons I have removed the actual usernames and such from the code, and it is pasted below. Any help would be greatly appreciated. <?php // Function to connect to the database. function db_connect() { $dbc = mysqli_connect('host','user','********','db'); } // Function to run database queries. function db_query($q) { global $dbc; $dbq = mysqli_query($dbc, $q); // ERROR OCCURS HERE if(!$dbq) { return false; } else { $r = mysqli_store_result($link); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/270140-warning-mysqli_query-expects-parameter-1-to-be-mysqli-null-given/ Share on other sites More sharing options...
haku Posted November 1, 2012 Share Posted November 1, 2012 (edited) You have your query and database connection backwards in your mysqli_query(). Edited November 1, 2012 by haku Quote Link to comment https://forums.phpfreaks.com/topic/270140-warning-mysqli_query-expects-parameter-1-to-be-mysqli-null-given/#findComment-1389163 Share on other sites More sharing options...
chiefboz Posted November 1, 2012 Author Share Posted November 1, 2012 The function parameter $q gives it a query. For example, in another file register.php, I have db_query("INSERT INTO user_accounts (firstname, lastname, email, username, reg_timestamp, verification_code, vcode_expire) VALUES ('$fn', '$ln', '$e1', '$un', $time, $v, $vcexp)"); where $fn, $ln, $e1 and $un are filled in by the user on a form. $time, $v and $vcexp are generated automatically. The code above is just a function for use in other files. Quote Link to comment https://forums.phpfreaks.com/topic/270140-warning-mysqli_query-expects-parameter-1-to-be-mysqli-null-given/#findComment-1389166 Share on other sites More sharing options...
haku Posted November 1, 2012 Share Posted November 1, 2012 I edited my original comment, probably while you were posting, as I saw the error after I posted. Quote Link to comment https://forums.phpfreaks.com/topic/270140-warning-mysqli_query-expects-parameter-1-to-be-mysqli-null-given/#findComment-1389167 Share on other sites More sharing options...
chiefboz Posted November 1, 2012 Author Share Posted November 1, 2012 Ok, switched them, but that's a string (not a mysqli) and so it's still throwing errors. Quote Link to comment https://forums.phpfreaks.com/topic/270140-warning-mysqli_query-expects-parameter-1-to-be-mysqli-null-given/#findComment-1389172 Share on other sites More sharing options...
haku Posted November 1, 2012 Share Posted November 1, 2012 Yeah? Quote Link to comment https://forums.phpfreaks.com/topic/270140-warning-mysqli_query-expects-parameter-1-to-be-mysqli-null-given/#findComment-1389173 Share on other sites More sharing options...
PFMaBiSmAd Posted November 1, 2012 Share Posted November 1, 2012 (edited) The 1st parameter is the mysqli link. The original error is because $dbc is not a global variable and doesn't exist outside of the db_connect(){} function. To get your existing code to 'work', you would need to return the connection resource from the db_connection() function and assign it to a $dbc variable in the main program's scope. Edited November 1, 2012 by PFMaBiSmAd Quote Link to comment https://forums.phpfreaks.com/topic/270140-warning-mysqli_query-expects-parameter-1-to-be-mysqli-null-given/#findComment-1389178 Share on other sites More sharing options...
haku Posted November 1, 2012 Share Posted November 1, 2012 Interesting, I didn't realize that mysqli_query() took the connection before the query. But yeah, pmfbsad spotted the problem. Quote Link to comment https://forums.phpfreaks.com/topic/270140-warning-mysqli_query-expects-parameter-1-to-be-mysqli-null-given/#findComment-1389182 Share on other sites More sharing options...
chiefboz Posted November 1, 2012 Author Share Posted November 1, 2012 Ok, awesome, I got it resolved by combining the functions. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/270140-warning-mysqli_query-expects-parameter-1-to-be-mysqli-null-given/#findComment-1389185 Share on other sites More sharing options...
PFMaBiSmAd Posted November 1, 2012 Share Posted November 1, 2012 ^^^ if by that you mean you are calling mysqli_connect() right before the mysqli_query() statement, don't. Creating a database connection is a fairly long process. You should not be making a database connection before each query. You should make ONE database connection and use it for the duration of the code on any one page. Quote Link to comment https://forums.phpfreaks.com/topic/270140-warning-mysqli_query-expects-parameter-1-to-be-mysqli-null-given/#findComment-1389192 Share on other sites More sharing options...
chiefboz Posted November 1, 2012 Author Share Posted November 1, 2012 Ok, cool, thanks for the tip. Problem solved. Quote Link to comment https://forums.phpfreaks.com/topic/270140-warning-mysqli_query-expects-parameter-1-to-be-mysqli-null-given/#findComment-1389193 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.