Jump to content

PHP 'MySQL Query' function - trouble with $result


deanes02

Recommended Posts

I call an external function to execute the sql query - which it does successfully.  The problem is that I want to then use the $result of the function to draw a table of results.  If i put all the code together in one file the code works perfectly.  If i have them seperated out like I do $result comes back as ==0 and "Error :" is printed to the screen but without any error nums or errors.

 

How can I use the $result of an external function in my code?

 

Many thanks to you all!!

 

In "call function" file

 

include("functions/SQLquery.php");

$query="select * from theTable";

sql_function($query);

 

  if($result == 0)

  {

    echo "<b>Error ".mysql_errno().": ".mysql_error().

          "</b>";

  }

  elseif (@mysql_num_rows($result) == 0)

  {

    echo("<b>Query completed. No results returned.

          </b><br>");

  }

  else

  {

  echo "<table border='1'>

          <thead>

          <tr>";

            for($i = 0;$i < mysql_num_fields($result);$i++)

            {

            echo "<th>".mysql_field_name($result,$i).

                  "</th>";

            }

  echo "  </tr>

          </thead>

        <tbody>";

          for ($i = 0; $i < mysql_num_rows($result); $i++)

          {

            echo "<tr>";

            $row = mysql_fetch_row($result);

            for($j = 0;$j<mysql_num_fields($result);$j++)

            {

              echo("<td>" . $row[$j] . "</td>");

            }

            echo "</tr>";

          }

  echo "</tbody>

        </table>";

  }  //end else

 

In the "function file"

 

function sql_function($passed_query)

{

/*Set up database variables*/

$host="xxx"; //obviously real values are put in here

$user="xxx";

$password ="xxx";

$database = "xxx";

 

/*set up server connection*/

$connection = mysql_connect($host,$user,$password)

or die ("<b><font color='red'>Sorry.  Could not connect to the server this time.</font></b>");

 

/*set up database connection*/

$db = mysql_select_db($database,$connection)

or die ("<b><font color='red'>Sorry.  Could not connect to the database this time.</font></b>");

 

/*Set up the Query*/

$query = $passed_query;

$result = mysql_query($query)

  or die ("<b><font color='red'>Sorry.  Could not execute your query this time.</font></b>".mysql_error());

 

mysql_close($connection);

}

I think you missed

return $result;

at the end of your function. That should say your function that it has to return result to caller.

 

 

Also in your "call function" file you chould make some changes.

 

change this line 
sql_function($query);
to 
$result = sql_function($query);

 

 

Please note $result is just a variable/object to hold result of executed function. I think you mess with that. To be more clear you could even make changes as follow:

 

change this line sql_function($query);
to 
$res = sql_function($query);

 

And than you would just use $res instead of $result. Your functions hasn't make any return to caller and that why your variable $result was always empty.

 

I think that should solve your problem. If that happend with your issue please use Solved button.

 

I hope this was usefull for you.

 

Cheers ;)

 

8.1.0178

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.