Noob - a simple way to turn mysql results into variables?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
VisionsOfCody
Forum Commoner
Posts: 30
Joined: Sat Mar 01, 2003 1:19 pm
Location: France

Noob - a simple way to turn mysql results into variables?

Post by VisionsOfCody »

HI!
I'm trying to write a php page that formats MySQL results as XML - making all the files and stuff is quite straightforward but i need a way of chopping my $result array up into variables - the problem is that there will be 4 table rows in each $result, so there will be 4 PageNum and 4 PageID etc each time - how do i assign them to separate named variables?
Any help would be very gratefully received!!
thanks



here's the relevant bit of code

Code: Select all

<?php
$req = mysql_query("SELECT PageNum,PageID,PagePathName FROM schoolmasterpages WHERE LessonID='$L' ");

while ($L<=10){
	
	touch("D:\Program Files\xmlFiles\lesson".$L.".xml");
	$fp = fopen("D:\Program Files\xmlFiles\lesson".$L.".xml", "w") or die ("no go"); 
	fwrite($fp, "<?xml version="1.0" standalone="yes"?>\n");
	fwrite($fp, "<games>\n"); 
	$i=0;
	while ($result = mysql_fetch_array($req))
		{ 
    	                echo $result[PagePathName]."<br>";	
		$i++;		
		}


?>
VisionsOfCody
Forum Commoner
Posts: 30
Joined: Sat Mar 01, 2003 1:19 pm
Location: France

Post by VisionsOfCody »

.... i've just found this bit of code to create variable names on the fly but i can't work out how to implement it

Code: Select all

<?php

while (list($key,$val) = each(${$category."_sub"}) ) { 
                print $val; 
        }

?>
any ideas ?
thanks
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post by liljester »

mysql_fetch_array() should be what you want to use.... but the code you posted is missing a closing brace for the first while statement. also there is no place in your code where $L is incremented... are you getting an infinate loop? does the script output anything?
VisionsOfCody
Forum Commoner
Posts: 30
Joined: Sat Mar 01, 2003 1:19 pm
Location: France

Post by VisionsOfCody »

hi
sorry - in fact i only posted a small chunk of code - i left out the stuff before and after to save space ! I'm painfully inching forward to where i need to be - i've now found a way of making dynamic variable names but i need to write them to the xml file (4 variable values in each of the ten xml files) and it isn't happening

here's the whole thing so far:

Code: Select all

<?php

$L=1; 
$host="localhost";
$user="root";
$pass="";
mysql_connect($host,$user,$pass);
mysql_select_db("test")or die("sorry, couldn't connect");

while ($L<=10){
	$req = mysql_query("SELECT PageNum,PageID,PagePathName FROM schoolmasterpages WHERE 

LessonID='$L' ");
	touch("D:\Program Files\xmlFiles\lesson".$L.".xml");
	$fp = fopen("D:\Program Files\xmlFiles\lesson".$L.".xml", "w") or die ("no go"); 
	fwrite($fp, "<?xml version="1.0" standalone="yes"?>\n");
	fwrite($fp, "<games>\n"); 
	$i=0;
	while ($result = mysql_fetch_array($req))
		{ 
    		${"PagePathName".$L.$i} = $result[PagePathName]."<br>";
		fwrite($fp, ${"PagePathName".$L.$i});
		$i++;		
		}
	fwrite($fp, "</games>\n");
	fclose($fp);
	$L++;
	}
mysql_close();

?>
VisionsOfCody
Forum Commoner
Posts: 30
Joined: Sat Mar 01, 2003 1:19 pm
Location: France

Post by VisionsOfCody »

just realised that this

Code: Select all

<?php
while ($result = mysql_fetch_array($req))
?>
probably shouldn't be that at all - it's a leftover :?
VisionsOfCody
Forum Commoner
Posts: 30
Joined: Sat Mar 01, 2003 1:19 pm
Location: France

Post by VisionsOfCody »

lol - that WAS the problem - now it works fine :oops:
sorry for the bother
thanks anyway !!
Post Reply