Jump to content

how to define connectionID in line with the function for mysqli_connect


AliG

Recommended Posts

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

Link to comment
Share on other sites

$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 by ginerjm
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by ginerjm
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
};

?>

 

Link to comment
Share on other sites

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.

  • Thanks 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.