RE: [PHP-DB] Query in a function
take out the db connection and include it seperately in your page then make $connection global in your function. or do a if (!$connection) then ($connection=blah blah blah...) Steve > -Original Message- > From: Shahmat Dahlan [mailto:[EMAIL PROTECTED]] > Sent: 26 June 2001 07:55 > To: seriousj > Cc: [EMAIL PROTECTED] > Subject: Re: [PHP-DB] Query in a function > > > How about declaring some of the variables as global variables. > > regards > > > seriousj wrote: > > > Hello > > I have a query that I have placed in a function that is in > a file that I > > "include" in the page to be displayed. It doesn't work. I > know that the code > > works because when I place the code in the page to be > displayed directly it > > works fine. > > > > I can't figure it out. Thanks in advance for any help. > > John > > > > Here is the function contained in lib.inc: > > function type_select() > > { > >$connection = mysql_connect($server, $user, $pass); > >echo(""); > >$query1 = "SELECT * FROM type"; > >$result1 = mysql_db_query($db, $query1, $connection); > >echo(!isset($type_id)?"--Please select a > > Type--":""); > >if(mysql_num_rows($result1) > 0) > >{ > > while($row1 = mysql_fetch_array($result1)) > > { > > $result1_type_id = $row1["type_id"]; > > $result1_type_desc = $row1["type_description"]; > > echo(" > echo(isset($type_id)?($result1_type_id==$type_id ? > "selected " : > > ""):""); > > echo("value=\""); > > echo($result1_type_id); > > echo("\">"); > > echo( $result1_type_desc); > > echo(""); > > } > >} > >else > >{ > > echo("Error"); > >} > >echo(""); > > } > > > > And here is the call to that function in the page that is displayed: > > include("lib.inc"); > > ?> > > > > > //TYPE selection > > if(!isset($type_id)) > > { > >type_select(); > > } > > > > And here is the error message: > > Warning: MySQL Connection Failed: Can't connect to local > MySQL server > > through socket '/tmp/mysql.sock' (2) in > > > /web/sites/268/seriousj/www.mccartney.f2s.com/opus/tnzp/lib.in > c on line 21 > > > > -- > > PHP Database Mailing List (http://www.php.net/) > > To unsubscribe, e-mail: [EMAIL PROTECTED] > > For additional commands, e-mail: [EMAIL PROTECTED] > > To contact the list administrators, e-mail: > [EMAIL PROTECTED] > > > -- > PHP Database Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: > [EMAIL PROTECTED] > -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DB] Query in a function
How about declaring some of the variables as global variables. regards seriousj wrote: > Hello > I have a query that I have placed in a function that is in a file that I > "include" in the page to be displayed. It doesn't work. I know that the code > works because when I place the code in the page to be displayed directly it > works fine. > > I can't figure it out. Thanks in advance for any help. > John > > Here is the function contained in lib.inc: > function type_select() > { >$connection = mysql_connect($server, $user, $pass); >echo(""); >$query1 = "SELECT * FROM type"; >$result1 = mysql_db_query($db, $query1, $connection); >echo(!isset($type_id)?"--Please select a > Type--":""); >if(mysql_num_rows($result1) > 0) >{ > while($row1 = mysql_fetch_array($result1)) > { > $result1_type_id = $row1["type_id"]; > $result1_type_desc = $row1["type_description"]; > echo(" echo(isset($type_id)?($result1_type_id==$type_id ? "selected " : > ""):""); > echo("value=\""); > echo($result1_type_id); > echo("\">"); > echo( $result1_type_desc); > echo(""); > } >} >else >{ > echo("Error"); >} >echo(""); > } > > And here is the call to that function in the page that is displayed: > include("lib.inc"); > ?> > > //TYPE selection > if(!isset($type_id)) > { >type_select(); > } > > And here is the error message: > Warning: MySQL Connection Failed: Can't connect to local MySQL server > through socket '/tmp/mysql.sock' (2) in > /web/sites/268/seriousj/www.mccartney.f2s.com/opus/tnzp/lib.inc on line 21 > > -- > PHP Database Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DB] Query in a function
At 19.06.2001 09:21, you wrote: >Hello >I have a query that I have placed in a function that is in a file that I >"include" in the page to be displayed. It doesn't work. I know that the code >works because when I place the code in the page to be displayed directly it >works fine. > >I can't figure it out. Thanks in advance for any help. >John > >Here is the function contained in lib.inc: >function type_select() >{ >$connection = mysql_connect($server, $user, $pass); are $server, $user and $pass global variables? If not, they wouldn't be available inside the function and you'd either have to make them global, or pass them to the function. That could explain why you can't connect... -- Andreas D Landmark / noXtension Real Time, adj.: Here and now, as opposed to fake time, which only occurs there and then. -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DB] Query in a function
errr maybe you need to pass the function the values of the variables you are using either by declaring them global inside the function: global $server,$user,$pass,$type_id; or passing them to the function directly: function type_select($server,$user,$pass,$type_id) hope this helps :-) Jimmy Brake On Tue, 19 Jun 2001, seriousj wrote: > Hello > I have a query that I have placed in a function that is in a file that I > "include" in the page to be displayed. It doesn't work. I know that the code > works because when I place the code in the page to be displayed directly it > works fine. > > I can't figure it out. Thanks in advance for any help. > John > > Here is the function contained in lib.inc: > function type_select() > { >$connection = mysql_connect($server, $user, $pass); >echo(""); >$query1 = "SELECT * FROM type"; >$result1 = mysql_db_query($db, $query1, $connection); >echo(!isset($type_id)?"--Please select a > Type--":""); >if(mysql_num_rows($result1) > 0) >{ > while($row1 = mysql_fetch_array($result1)) > { > $result1_type_id = $row1["type_id"]; > $result1_type_desc = $row1["type_description"]; > echo(" echo(isset($type_id)?($result1_type_id==$type_id ? "selected " : > ""):""); > echo("value=\""); > echo($result1_type_id); > echo("\">"); > echo( $result1_type_desc); > echo(""); > } >} >else >{ > echo("Error"); >} >echo(""); > } > > And here is the call to that function in the page that is displayed: > include("lib.inc"); > ?> > > //TYPE selection > if(!isset($type_id)) > { >type_select(); > } > > And here is the error message: > Warning: MySQL Connection Failed: Can't connect to local MySQL server > through socket '/tmp/mysql.sock' (2) in > /web/sites/268/seriousj/www.mccartney.f2s.com/opus/tnzp/lib.inc on line 21 > > > > > -- > PHP Database Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] > > -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP-DB] Query in a function
Hello I have a query that I have placed in a function that is in a file that I "include" in the page to be displayed. It doesn't work. I know that the code works because when I place the code in the page to be displayed directly it works fine. I can't figure it out. Thanks in advance for any help. John Here is the function contained in lib.inc: function type_select() { $connection = mysql_connect($server, $user, $pass); echo(""); $query1 = "SELECT * FROM type"; $result1 = mysql_db_query($db, $query1, $connection); echo(!isset($type_id)?"--Please select a Type--":""); if(mysql_num_rows($result1) > 0) { while($row1 = mysql_fetch_array($result1)) { $result1_type_id = $row1["type_id"]; $result1_type_desc = $row1["type_description"]; echo(""); echo( $result1_type_desc); echo(""); } } else { echo("Error"); } echo(""); } And here is the call to that function in the page that is displayed: include("lib.inc"); ?> http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]