Can't run a query inside a function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phpBever
Forum Commoner
Posts: 42
Joined: Fri Aug 07, 2009 10:23 am

Can't run a query inside a function

Post by phpBever »

I've got a MySQL query that runs fine when I put it directly into the code, but won't run when I put it in a function and call the function.

Code: Select all

   $sql1 = "Insert into tblPublicSignings (ID,SigningTransID, campaignNo, SignerName, SignerPhone, SignerEmail, Date) Values (NULL,'".$SigningTransID."','".$campaignNo."','".$SignerName."','".$SignerPhone."','".$SignerEmail."','". $Date."')";

    $res1 = mysqli_query($mysqli, $sql1) or die("sql1 didn't work");
This works nicely. The following does not:

Code: Select all

function testStuff($campaignNo,$SignerName, $SignerPhone, $Date,$SignerEmail,$SigningTransID)
{
   $sql1 = "Insert into tblPublicSignings (ID,SigningTransID, campaignNo, SignerName, SignerPhone, SignerEmail,Date) Values (NULL,'".$SigningTransID."','".$campaignNo."','".$SignerName."','".$SignerPhone."','".$SignerEmail."','". $Date."')";

    $res1 = mysqli_query($mysqli, $sql1) or die("sql1 didn't work");

}

testStuff($campaignNo,$SignerName, $SignerPhone, $Date,$SignerEmail,$SigningTransID);


I keep getting the "sql1 didn't work" message.
I've done this (run queries from within a function) lots of times before without problems. The queries also work fine directly in MySQL. What could possibly be going wrong for me that I haven't thought of?

Thanks.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Can't run a query inside a function

Post by requinix »

Code: Select all

mysqli_query($mysqli, $sql1)
Nowhere in that function do you define $mysqli.
phpBever
Forum Commoner
Posts: 42
Joined: Fri Aug 07, 2009 10:23 am

Re: Can't run a query inside a function

Post by phpBever »

$mysqli is defined as a global in my DB connecting function. I tried including it in the list of parameters for the function, but that didn't seem to make any difference.

I'm unclear as to whether it needs to be there or not. At any rate, I seem to be getting some results now. Maybe some other typo that somehow got fixed.

Thanks.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Can't run a query inside a function

Post by requinix »

Unlike some/many other languages, variables defined outside a PHP function are not normally available inside the function.

Also, "date" is a special term in MySQL that you can occasionally use without problems. Of course that means occasionally you'll try to use it but with problems.
So:

Code: Select all

$res1 = mysqli_query($mysqli, $sql1) or die("sql didn't work: " . mysqli_error($mysqli));
phpBever
Forum Commoner
Posts: 42
Joined: Fri Aug 07, 2009 10:23 am

Re: Can't run a query inside a function

Post by phpBever »

Is there something that requires that the $mysqli resource be listed first in the parameters of a function that will use it? I seem to have had success moving it there on one particular function, but don't know for sure if that was the reason.

Thanks.
phpBever
Forum Commoner
Posts: 42
Joined: Fri Aug 07, 2009 10:23 am

Re: Can't run a query inside a function

Post by phpBever »

I still haven't got this sorted out. Let me try a clearer example of my issue. If I run this code, my query runs fine:

Code: Select all

<?php

function doDB() {
	//connect to server and select database; you may need it
	global $mysqli;
	include("misc.php"); //where $host etc are defined
	$mysqli = new mysqli($host,$user,$password,$database); 

	//if connection fails, stop script execution
	if (mysqli_connect_errno() ) {
		printf("Connect failed: %s\n", mysqli_connect_error() );
		exit();
	}
}

//function testDB()
//{
 doDB();
 $sql = "select * from tblFaculty";
 $res = mysqli_query($mysqli, $sql);
 $numRows = mysqli_num_rows($res);
 echo $numRows;
//}

//testDB();
?>
But when I try to put it all in a function and call the function (the same code with the function-defining rows un-commented), it doesn't run. The error log at apache2 says:
[Mon Jul 05 09:24:39 2010] [error] [client 127.0.0.1] PHP Notice: Undefined variable: mysqli in /srv/www/htdocs/AdvSched/testDoDB.php on line 20
[Mon Jul 05 09:24:39 2010] [error] [client 127.0.0.1] PHP Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /srv/www/htdocs/AdvSched/testDoDB.php on line 20
[Mon Jul 05 09:24:39 2010] [error] [client 127.0.0.1] PHP Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /srv/www/htdocs/AdvSched/testDoDB.php on line 21
I'm obviously missing something but can't tell what. I'm not passing any variables--they're all provided by the include("misc.php") in the db connection function.

Can anyone explain?

Thanks.
Post Reply