AliG Posted December 9, 2021 Share Posted December 9, 2021 HIi, I have a the following function function dbk_connect($dbserver,$dbuser,$dbpasswort,$dbname) { $mysqli = mysqli_connect($dbserver,$dbuser,$dbpasswort,$dbname); return $mysqli; }; function dbk_close($mysqli) { mysqli_close($mysqli); return $mysqli; }; now I wish to use it to connect to my database and for some reasons I must use this function and I cannot use mysqi_connect directly. So this is what I have to conenct to the database <?php include_once '../included/database.php'; dbk_connect("locahost", "Username", "12345", "databse"), "connectionID"; dbk_close("connectionID"); ?> I am getting errors because of the connectionID , I do not know how to assign the session a connection ID with the syntax above and I must use the function above. I would appreciate your help. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/314289-how-to-define-connectionid-in-line-with-the-function-for-mysqli_connect/ Share on other sites More sharing options...
ginerjm Posted December 9, 2021 Share Posted December 9, 2021 (edited) $mysql = dbk_connect(......); Now you have the $mysql var as your connection for everything. Get rid of the close function. Just close it by yourself. PS - what is the point of this function? It's a 1 line 'block' of code that could easily be written into any script that you need a db connection. Why the overhead of calling a function? UNLESS you wanted to add some error handling to the function so that you didn't have to handle THAT everywhere you wanted to make a connection. Now there's an idea that you didn't think of it seems. Perhaps do a test on your connection call and if it fails get the error message and pass it back using a 5th parm on your function definition, return 'false' and let your caller check for that return and take the returned error message from the function and output it or something. Read up on using functions and how to add the 5th parm. Good learning opportunity. Edited December 9, 2021 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/314289-how-to-define-connectionid-in-line-with-the-function-for-mysqli_connect/#findComment-1592569 Share on other sites More sharing options...
AliG Posted December 9, 2021 Author Share Posted December 9, 2021 4 minutes ago, ginerjm said: $mysql = dbk_connect(......); Now you have the $mysql var as your connection for everything. Get rid of the close function. Just close it by yourself. I am still getting an error Parse error: syntax error, unexpected '$mysqli' (T_VARIABLE) in $mysqli = dbk_connect("locahost", "Username", "12345", "databse"); Is there really not a way to assign a connectionID ? Thanks alot for the answer. Quote Link to comment https://forums.phpfreaks.com/topic/314289-how-to-define-connectionid-in-line-with-the-function-for-mysqli_connect/#findComment-1592570 Share on other sites More sharing options...
ginerjm Posted December 9, 2021 Share Posted December 9, 2021 (edited) your error message says unexpected..... in . Where is this 'in' thing. Show us the function with your changes so and point out the line number that is giving you the error. AND - I don't buy that you 'have to use this (connect) function'. Your function does absolutely nothing. It calls one line of code using arguments that you had to prepare outside of the function for everywhere that you use this function. Kind of silly. One writes a function to cut down on code and to simplify things. Your function is doing none of that for you. Edited December 9, 2021 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/314289-how-to-define-connectionid-in-line-with-the-function-for-mysqli_connect/#findComment-1592571 Share on other sites More sharing options...
AliG Posted December 9, 2021 Author Share Posted December 9, 2021 2 minutes ago, ginerjm said: your error message says unexpected..... in . Where is this 'in' thing. Show us the function with your changes so and point out the line number that is giving you the error. Hi thanks , it is resolved, I used $mysqli , which is already defined in the function given above, I assigned $mysql and worked. Quote Link to comment https://forums.phpfreaks.com/topic/314289-how-to-define-connectionid-in-line-with-the-function-for-mysqli_connect/#findComment-1592572 Share on other sites More sharing options...
ginerjm Posted December 9, 2021 Share Posted December 9, 2021 Could you show us please? Quote Link to comment https://forums.phpfreaks.com/topic/314289-how-to-define-connectionid-in-line-with-the-function-for-mysqli_connect/#findComment-1592573 Share on other sites More sharing options...
AliG Posted December 9, 2021 Author Share Posted December 9, 2021 2 minutes ago, ginerjm said: Could you show us please? Yes sure <?php include_once '../included/datenbank.php'; $mysql=dbk_connect("locahost", "Username", "12345", "database"); dbk_close($mysql); dbk_close($mysql) dbk_query($mysql,$sql : string) // run sql querry dbk_fetch_row($result) : cells as array dbk_fetch_assoc($result) : cells as associative Array dbk_errno($mysql) : ErrorNr ?> and these are the complete functions <?php function dbk_connect($dbserver,$dbuser,$dbpasswort,$dbname) { $mysqli = mysqli_connect($dbserver,$dbuser,$dbpasswort,$dbname); return $mysqli; }; function dbk_close($mysqli) { mysqli_close($mysqli); return $mysqli; }; function dbk_query($mysqli,$sql) { if (strpos(strtolower($sql),"select") != FALSE) { $result = ""; } else { $result = mysqli_query($mysqli,$sql); }; return $result; }; function dbk_fetch_row($result) { $row = mysqli_fetch_row($result); return $row; }; function dbk_fetch_assoc($result) { $row = mysqli_fetch_assoc($result); return $row; }; function dbk_errno($mysqli) { return $mysqli->connect_errno; }; ?> Quote Link to comment https://forums.phpfreaks.com/topic/314289-how-to-define-connectionid-in-line-with-the-function-for-mysqli_connect/#findComment-1592575 Share on other sites More sharing options...
ginerjm Posted December 9, 2021 Share Posted December 9, 2021 You are new to all of this, aren't you? You are writing functions to 1 line of work. What is the point? And I don't believe that they are all even working for your. When you do the close function your don't capture the result of the close (RTFM) but you then try and return the value of the connection value that you called it with. That value is non-existent since you just closed it. You are doing a fetch of 1 row of data in another. Why? Just use a line in your current script - no need to use a function. And in your main body that calls all of these frivolous functions you make a connection and then you close it twice and then you try and do a query with a query that does not exist. I think I'll quit this topic while I still have hair left. Good luck with your growth. 1 Quote Link to comment https://forums.phpfreaks.com/topic/314289-how-to-define-connectionid-in-line-with-the-function-for-mysqli_connect/#findComment-1592576 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.