Forum Moderators: coopster

Message Too Old, No Replies

Are mysql includes possible?

How do I include mysql recordsets

         

fingers

3:48 pm on Aug 4, 2005 (gmt 0)

10+ Year Member



I use the php include function regularly for headers and other common page elements.
What I would like to do is "include" my mysql queries too.
i.e.

mysql_select_db($database_name, $mydb);
$query_rsCatdept = "SELECT category.name FROM category WHERE category.categoryID = $pagevar";
$rsCatdept = mysql_query($query_rsCatdept, $mydb) or die(mysql_error());
$row_rsCatdept = mysql_fetch_assoc($rsCatdept);
$totalRows_rsCatdept = mysql_num_rows($rsCatdept);
$catname = $row_rsCatdept['name'];

The $pagevar variable would change depending on what page my mysql query was "included".

This way I can create a "commonmysql.php" page that holds all my queries, and if i decide to change them, I only have to do it once.
Does this make any sense?
Many thanks in advance.

jatar_k

4:27 pm on Aug 4, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



makes absolute sense and could be done just as you say

fingers

5:51 pm on Aug 4, 2005 (gmt 0)

10+ Year Member



Thanks jatar_k,
I can't get it to work. It seems my commonmysql.php page is processing the mysql before it gets included on my page. The $pagevar value is set on the destination page, what I would like to do is include the php mysql code, but get it to parse on my destination page.
Any ideas? Thanks again.

ergophobe

7:17 pm on Aug 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



How are you determining the $pagevar value? You need to do it further up the processing stream. A good way to think of it is

- gather all information from the incoming request (URL, post, get, session values)

- process all that information as needed (clean it, manipulate it, sort it).

- output the results (no output before all processing except perhaps some presentational processing such as including admin links or not)

fingers

8:45 am on Aug 5, 2005 (gmt 0)

10+ Year Member



Thanks ergophobe.
The $pagevar is set to a number, say 7 which is hard coded into the page that calls the include data. So what I would like to do is load my central mysql query into the page, then run the queries. That way my $pagevar will be set, and the query should work ok.

What I am having trouble with is including the mysql query as a string, and then getting it to run after it has loaded.

ergophobe

3:11 pm on Aug 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Can you include the mysql query *after* you define $pagevar. Define $pagevar right at the very top and then do your include.

There are other ways that would work, but all of them are going to require adding some code after $pagevar gets defined.

timster

3:45 pm on Aug 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm...am I missing something here?

Why not put your mysql queries into functions on the commonmysql.php page. Include that at the top of the page, and just call the functions (and pass them arguments, e.g., $pagevar) as needed?

ergophobe

6:58 pm on Aug 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That's sort of what I meant by "other possibilities".

The main thing, though, is that whether a function call or an include, $pagevar needs to be defined first, then call or include or eval as needed.

fingers

10:28 am on Aug 6, 2005 (gmt 0)

10+ Year Member



Using functions seems like a good idea. So if I add this to my commonmysql.php page.

function myquery($pagevar ) {
mysql_select_db($database_name, $mydb);
$query_rsCatdept = "SELECT category.name FROM category WHERE category.categoryID = $pagevar";
$rsCatdept = mysql_query($query_rsCatdept, $mydb) or die(mysql_error());
$row_rsCatdept = mysql_fetch_assoc($rsCatdept);
$totalRows_rsCatdept = mysql_num_rows($rsCatdept);
$catname = $row_rsCatdept['name'];
}

How do I i call it on my "destination" page? Like below?

$pagevar= 27;
myquery($pagevar);

Many thanks.

fingers

10:55 am on Aug 6, 2005 (gmt 0)

10+ Year Member



Hmmm can't get it to work. Any suggestions? Thanks again.

maxi million

11:48 am on Aug 6, 2005 (gmt 0)

10+ Year Member



looking at your function it appears to me that its not returning anything yet

how about -
function myquery($pagevar) {
mysql_select_db($database_name, $mydb);
$query_rsCatdept = "SELECT category.name FROM category WHERE category.categoryID = $pagevar";
$rsCatdept = mysql_query($query_rsCatdept, $mydb) or die(mysql_error());
$row_rsCatdept = mysql_fetch_assoc($rsCatdept);
$totalRows_rsCatdept = mysql_num_rows($rsCatdept);
$rowdataarray = array(
"rowresult" => $row_rsCatdept,
"totalrows" => $totalRows_rsCatdept)
}

now you can use this function for anything and everything to do with $pagevar

ergophobe

4:44 pm on Aug 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Whoops! I think maxi million forgot a line too, since it's still not returning anything. Now that he's put your values in an array, you still have to return it with

return $rowdataarray;

Another problem with your function is that it probably has many undefined variables since you have not passed $database_name or $mydb etc to the function. Variables in functions are local to the functions.

See the thread on "Understanding Custom Functions and Scope [webmasterworld.com]", especially my long post (#7 in the thread). That might help.

maxi million

5:55 pm on Aug 6, 2005 (gmt 0)

10+ Year Member



Whoops! Whoops!
ergophobe my face hangs in shame. dont kick me out of here please for that :(

ergophobe

8:03 pm on Aug 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yeah well straighten up! Just kidding. If an offense like that could get you banned, I'm pretty sure there wouldn't be any mods left in this forum.

fingers

11:01 am on Aug 8, 2005 (gmt 0)

10+ Year Member



I am still grappling with this. The linked article is fantastic ergophobe - thanks very much.

I guess I have a little more learning to do with variablescope etc...