Jump to content

[SOLVED] PHP Query Function


jjacquay712

Recommended Posts

I am trying to make a php function that does an sql query, then assigns the result to a variable that is global in the script. Heres an example:

 

<?php
myfunction("My SQL Query", $the_variable_that_contains_my_query);
echo $the_variable_that_contains_my_query;
?>

 

how would i go about making such a function?

 

                  Thanks, John

Link to comment
https://forums.phpfreaks.com/topic/123178-solved-php-query-function/
Share on other sites

I wouldn't do this, but you could...Here's how:

 

function do_query($query, &$result) {
    $result = mysql_query($query) or die(mysql_error());
}

 

Whatever variable you pass into the function will be modified in the space in which it was declared.

Im trying to make a function so my code will be less "dirty".

 

hers what i usually do:

 

$resource = mysql_query("blah blah blah") or die("");
$username = mysql_result($resource, 0, 'username');

 

but that takes up to much space, and if there's an error, it prints it on the page. with a function you could do a error_reporting(0); and suppress the error in only one part of the 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.