# Stuck on Function to Create MySQL Connection

**URL:** <https://forums.powershell.org/t/stuck-on-function-to-create-mysql-connection/21697>\
**Category:** PowerShell Help\
**Created:** [February 20, 2023, 5:00pm UTC](https://forums.powershell.org/t/stuck-on-function-to-create-mysql-connection/21697 "2023-02-20T17:00:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![joseph-monarch](https://avatars.discourse-cdn.com/v4/letter/j/a88e57/32.png) [@joseph-monarch](https://forums.powershell.org/u/joseph-monarch)\
**Post date:** [February 20, 2023, 5:00pm UTC](https://forums.powershell.org/t/stuck-on-function-to-create-mysql-connection/21697/1 "2023-02-20T17:00:56Z")

</div>

I am stuck at a point in my script to call a function to make a connection to a MySQL DB. Outside of the function, the script runs fine. As soon as I throw it into a function, it breaks. I am going to be calling this portion of script multiple times in the overall script, hence the reason for the function.

```auto
function MySQLConnect {
	$connStr = "server=$sqlserver;userid=$sqluser;password=$sqlpwd;database=$database"
	$conn = New-Object Mysql.Data.MySqlClient.MySqlConnection($connStr)
	$conn.Open()
}

```

Once I am outside of the function, the variable $conn is empty.

Here is where I first call the function:

```auto
MySQLConnect

$cmd = $conn.CreateCommand()
$cmd.CommandText = "Select * FROM assets WHERE name='$pcname' AND deleted_at IS NULL"
$reader = $cmd.ExecuteReader()
$pcfound = $false
while ($reader.Read()) {
	$pcfound = $true
}
$pcfound

```

Just to take the complexity out, I tried just open a connection to MySQL and then closing it.

```auto
MySQLConnect
$conn.Close()

```

Here is the error I receive:

> ERROR: You cannot call a method on a null-valued expression.  
> hwdinventory.ps1 (73, 2): ERROR: At Line: 73 char: 2  
> ERROR: + $conn.Close()  
> ERROR: + ~~~~~~~~~~~~~  
> ERROR: + CategoryInfo : InvalidOperation: (: ) , RuntimeException  
> ERROR: + FullyQualifiedErrorId : InvokeMethodOnNull  
> ERROR:

This error occurs because the variable $conn is now empty.

---

<div class="post-metadata">

**Author:** ![neemobeer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/neemobeer/32/4087_2.png) [@neemobeer](https://forums.powershell.org/u/neemobeer)\
**Post date:** [February 20, 2023, 5:13pm UTC](https://forums.powershell.org/t/stuck-on-function-to-create-mysql-connection/21697/2 "2023-02-20T17:13:06Z")

</div>

Define conn outside of the function. Looks like you have a scope issue.

---

<div class="post-metadata">

**Author:** ![mrfloppy](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@mrfloppy](https://forums.powershell.org/u/mrfloppy)\
**Post date:** [February 20, 2023, 5:14pm UTC](https://forums.powershell.org/t/stuck-on-function-to-create-mysql-connection/21697/3 "2023-02-20T17:14:04Z")

</div>

If the $conn variable is going to be consistent, put it outside of your function.  
Functions are wrapped cmdlets that unless you specify, only use whats in them and wont pass to other parts of the script.  
If theres going to be more than one $conn variable, you’ll need to add it as a parameter to your function.

---

<div class="post-metadata">

**Author:** ![joseph-monarch](https://avatars.discourse-cdn.com/v4/letter/j/a88e57/32.png) [@joseph-monarch](https://forums.powershell.org/u/joseph-monarch)\
**Post date:** [February 20, 2023, 6:32pm UTC](https://forums.powershell.org/t/stuck-on-function-to-create-mysql-connection/21697/4 "2023-02-20T18:32:18Z")

</div>

Sometimes I forget the basic things. This works.

```auto
function MySQLConnect {
	$connStr = "server=$sqlserver;userid=$sqluser;password=$sqlpwd;database=$database"
	$global:conn = New-Object Mysql.Data.MySqlClient.MySqlConnection($connStr)
	$conn.Open()
}

```

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [February 20, 2023, 6:43pm UTC](https://forums.powershell.org/t/stuck-on-function-to-create-mysql-connection/21697/5 "2023-02-20T18:43:22Z")

</div>

Global scope may work but this would be one of the least preferred ways to solve this issue. Perhaps use Script: scope or output the connection details from the function and capture it in a variable in the proper scope.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:15pm UTC](https://forums.powershell.org/t/stuck-on-function-to-create-mysql-connection/21697/6 "2024-05-16T20:15:07Z")

</div>

Closed on May 16, 2024, 8:15pm UTC


