EricOnAdventure Posted June 19, 2016 Share Posted June 19, 2016 I've been working on moving everything my mysql to mysqli, and for the most part it has been going smoothly, one exception is an error I am having now while putting it into a function. It worked just fine outside of the function, but inside I am getting an error. Here let me show you. <?php function findit($column,$table) { require_once '../dbstart.php'; if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $query = "SELECT $column FROM $table"; $result = mysqli_query($conn, $query); $row = mysqli_fetch_array($result, MYSQLI_ASSOC); return $row[$column]; /* close connection */ mysqli_close($conn); } echo findit('familyname','test'); ?> I get Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\efun\unisearch\SQLi.php on line 15 keep in mind before making it into a function I got exactly what I wanted. Any idea what I am doing wrong? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/301362-new-to-mysqli-and-putting-it-into-a-function/ Share on other sites More sharing options...
kicken Posted June 19, 2016 Share Posted June 19, 2016 That means that your query failed. Add error checking/handling to your code. A few other things to note: - You cannot have any code after your return statement. Return causes the function to end right there, everything after it would be ignored. - You shouldn't be opening and closing a connection for each query. Open the connection once at the start of the script. Let PHP close it automatically at the end of the script. 1 Quote Link to comment https://forums.phpfreaks.com/topic/301362-new-to-mysqli-and-putting-it-into-a-function/#findComment-1533820 Share on other sites More sharing options...
EricOnAdventure Posted June 19, 2016 Author Share Posted June 19, 2016 Thanks Kicken, I figured out the issue but I want to ask you more about closing the connection, so you say that I should have mysqli_close($conn); in my script? I should just leave it open and let it be closed be ?> ? Quote Link to comment https://forums.phpfreaks.com/topic/301362-new-to-mysqli-and-putting-it-into-a-function/#findComment-1533823 Share on other sites More sharing options...
Jacques1 Posted June 19, 2016 Share Posted June 19, 2016 Like kicken already said, PHP automatically closes the connection. Closing it manually a few microseconds earlier serves no purpose. While we're at it: Remove the last “?>” tag as well. It's not only useless but harmful. The closing tag makes PHP switch back into output mode, so if there's just one space after the tag, PHP will in fact send it to the client. And as you probably know, once there's output, it's no longer possible to start a session, issue a cookie or set an HTTP header. This is a huge problem when an included script accidentally starts output (like the already mentioned space) and blocks the entire main script. So just get rid of the tag. It's only needed when you actually want to write HTML markup after the PHP code. 1 Quote Link to comment https://forums.phpfreaks.com/topic/301362-new-to-mysqli-and-putting-it-into-a-function/#findComment-1533824 Share on other sites More sharing options...
Solution ginerjm Posted June 19, 2016 Solution Share Posted June 19, 2016 Does $column contain any spaces? One should really wrap associative indices in quotes to ensure there is no problem in determining the correct element of the array it is used on. $row["$column"] would be correct. BTW - you are doing a query that selects all rows but only returning the first row's value. Is that what you want? Quote Link to comment https://forums.phpfreaks.com/topic/301362-new-to-mysqli-and-putting-it-into-a-function/#findComment-1533826 Share on other sites More sharing options...
Muddy_Funster Posted June 19, 2016 Share Posted June 19, 2016 You shouldn't really have a require_once inside a function - the point of functions is to be re-usable and if you try to re-use a require_once call it's just going to throw warnings up at you. It would be better to require_once your script at the top of the page and then pass the connection object into the function as part of the call. Quote Link to comment https://forums.phpfreaks.com/topic/301362-new-to-mysqli-and-putting-it-into-a-function/#findComment-1533828 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.