Page 1 of 1

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

Posted: Tue Jun 10, 2003 1:41 pm
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++;		
		}


?>

Posted: Tue Jun 10, 2003 2:38 pm
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

Posted: Tue Jun 10, 2003 2:49 pm
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?

Posted: Tue Jun 10, 2003 3:25 pm
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();

?>

Posted: Tue Jun 10, 2003 3:27 pm
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 :?

Posted: Tue Jun 10, 2003 3:42 pm
by VisionsOfCody
lol - that WAS the problem - now it works fine :oops:
sorry for the bother
thanks anyway !!