Jump to content


Photo

can't use mysql_query inside a function


  • Please log in to reply
5 replies to this topic

#1 mauri_gato

mauri_gato
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 26 May 2006 - 02:20 PM

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";
$rs = mysql_query($query, $Con) or die(mysql_error()); //Here is where the execution stops
$row = mysql_fetch_assoc($rs);
echo $row['Col1'];
}

?>
<!--Some html code...-->
<?php
fun(); //Here i call the function
?>

Please help me, is there another way to execute a query inside a function?, Thanks

mauricio

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 26 May 2006 - 03:14 PM

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:
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'];
}


#3 mauri_gato

mauri_gato
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 26 May 2006 - 04:17 PM

I think my query is good, because this code runs as i want:

<?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&iacute;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>

However when i use that code in a function, it doesn't work, here is the code:

<?php require_once('../Connections/Clinica.php'); ?>
<?php
function 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&iacute;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>

When i declare those variables as globals neither code runs, help me please...

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 26 May 2006 - 05:18 PM

so you are saying that this does not work?

<?php require_once('../Connections/Clinica.php'); ?>
<?php
function 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&iacute;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>

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 mauri_gato

mauri_gato
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 27 May 2006 - 03:27 PM

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.

#6 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 27 May 2006 - 03:31 PM

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]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users