mauri_gato Posted May 26, 2006 Share Posted May 26, 2006 I need to execute a query inside a function in php. Then i used this instruction: mysql_query(), which works fine outside a function. Nevertheless when i use it inside a funcion it just doesn't execute the query and the page loading stops there.Here is a simple example to show you what is going on:<?php require_once('./Connections/Con.php');function fun() { mysql_select_db($database, $Con); $query = "SELECT enfAP FROM Enfermera where enfCod=1"; [b]$rs = mysql_query($query, $Con) or die(mysql_error()); //Here is where the execution stops[/b] $row = mysql_fetch_assoc($rs); echo $row['Col1']; }?><!--Some html code...--><?php [b] fun(); //Here i call the function [/b]?>Please help me, is there another way to execute a query inside a function?, Thanksmauricio Quote Link to comment https://forums.phpfreaks.com/topic/10509-cant-use-mysql_query-inside-a-function/ Share on other sites More sharing options...
wildteen88 Posted May 26, 2006 Share Posted May 26, 2006 You can use mysql_*() functions inside of any other function! If mysql_query is failing you most probably have an error within your query.Also within your function you are using variables such as $database and $Con which seemt to be variables set in the Con.php file which is being required. You will need to make these variables global in order for PHP to access them from your function.So change your code to the following:[code]function fun() {global $database, $Con;mysql_select_db($database, $Con);$query = "SELECT enfAP FROM Enfermera where enfCod=1";$rs = mysql_query($query, $Con) or die(mysql_error()); //Here is where the execution stops$row = mysql_fetch_assoc($rs);echo $row['Col1'];}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10509-cant-use-mysql_query-inside-a-function/#findComment-39208 Share on other sites More sharing options...
mauri_gato Posted May 26, 2006 Author Share Posted May 26, 2006 I think my query is good, because this code runs as i want:[code]<?php require_once('../Connections/Clinica.php'); ?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><title>Documento sin título</title></head><body> <table width="95%" border="0" cellspacing="3" align="center"> <tr> <td><div align="center"><strong>Apellido Paterno </strong></div></td> </tr> <tr> <td><div align="center"><?php //fun(); mysql_select_db($database_Clinica, $Clinica); $query = "SELECT enfAP FROM Enfermera"; $rs = mysql_query($query, $Clinica) or die(mysql_error()); $row = mysql_fetch_assoc($rs); echo $row['enfAP']; ?></div></td> </tr> </table> </body></html>[/code]However when i use that code in a function, it doesn't work, here is the code:[code]<?php require_once('../Connections/Clinica.php'); ?><?phpfunction fun() { mysql_select_db($database_Clinica, $Clinica); $query = "SELECT enfAP FROM Enfermera"; $rs = mysql_query($query, $Clinica) or die(mysql_error()); $row = mysql_fetch_assoc($rs); echo $row['enfAP'];}?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><title>Documento sin título</title></head><body> <table width="95%" border="0" cellspacing="3" align="center"> <tr> <td><div align="center"><strong>Apellido Paterno </strong></div></td> </tr> <tr> <td><div align="center"><?php fun(); ?></div></td> </tr> </table> </body></html>[/code]When i declare those variables as globals neither code runs, help me please... Quote Link to comment https://forums.phpfreaks.com/topic/10509-cant-use-mysql_query-inside-a-function/#findComment-39221 Share on other sites More sharing options...
.josh Posted May 26, 2006 Share Posted May 26, 2006 so you are saying that this does not work?[code]<?php require_once('../Connections/Clinica.php'); ?><?phpfunction fun() { global $Clinica; global $database_Clinica; mysql_select_db($database_Clinica, $Clinica); $query = "SELECT enfAP FROM Enfermera"; $rs = mysql_query($query, $Clinica) or die(mysql_error()); $row = mysql_fetch_assoc($rs); echo $row['enfAP'];}?>HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><title>Documento sin título</title></head><body> <table width="95%" border="0" cellspacing="3" align="center"> <tr> <td><div align="center"><strong>Apellido Paterno </strong></div></td> </tr> <tr> <td><div align="center"><?php fun(); ?></div></td> </tr> </table></body></html>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10509-cant-use-mysql_query-inside-a-function/#findComment-39227 Share on other sites More sharing options...
mauri_gato Posted May 27, 2006 Author Share Posted May 27, 2006 Thank you guys, that was my mistake. I don't completely understand why it must be declared as global, but i really appreciated your help. Quote Link to comment https://forums.phpfreaks.com/topic/10509-cant-use-mysql_query-inside-a-function/#findComment-39465 Share on other sites More sharing options...
wildteen88 Posted May 27, 2006 Share Posted May 27, 2006 Then you might want to read up on [a href=\"http://uk.php.net/manual/en/language.variables.scope.php\" target=\"_blank\"]variable scope[/a] Quote Link to comment https://forums.phpfreaks.com/topic/10509-cant-use-mysql_query-inside-a-function/#findComment-39467 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.