Jump to content

can't use mysql_query inside a function


mauri_gato

Recommended Posts

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?, Thanks

mauricio
Link to comment
https://forums.phpfreaks.com/topic/10509-cant-use-mysql_query-inside-a-function/
Share on other sites

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]
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&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>
[/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'); ?>
<?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>
[/code]

When i declare those variables as globals neither code runs, help me please...
so you are saying that this does not work?

[code]
<?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>
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.