The online racing simulator
Weird PHP/MySQL problem
(6 posts, started )
Weird PHP/MySQL problem
I've come across a problem that I don't know how to fix. I've tried changing stuff around but it still isn't working. Basically I am trying to read something from the database and output it to a string (it's a template system so I can't just print() it.

My code is:

<?php 
php
function template($data$type) {
    switch(
$type) {
        case 
"tpl_news":
            
$sql "SELECT * FROM `news`";
            
$sql mysql_query($sql);
            
//$row = mysql_fetch_array($sql);
            
$num mysql_num_rows($sql);
            
//return $num;
            
$return_data "";
            while(
$row mysql_fetch_array($sqlMYSQL_ASSOC)) {
                foreach(
$row as $row2=>$row3) {
                    
$new_data str_replace("{headline}"$row3["headline"], $new_data);
                    
$new_data str_replace("{content}"$row3["content"], $new_data);
                    
$new_data str_replace("{poster}"$row3["poster"], $new_data);
                    
$new_data str_replace("{date}"$row3["date"], $new_data);
                    
$return_data $new_data;
                    
$new_data $data;
                    
                    
//printf("Headline: %s; Content: %s; Poster; %s; Date: %s;", $row2["headline"], $row2["content"], $row2["poster"], $row2["date"]);
                
}
            }
        break;
    }
    
//print "<br />";
    //print $return_data;
    
return $return_data;
}
?>

The output is here: www.lfsdemoleague.cjb.net

I have checked the database and the record is correct, but it just shows one letter on the page

Any ideas?
#2 - filur
I don't really know what's wrong, but running str_replace on $new_data before $new_data has any contents seems odd.

Perhaps try a simpler function, also removes the need for hardcoding template replacements.


<?php 
php
    
function template($template_source$table) {
        
$output "";
        
$query mysql_query("SELECT * FROM $table");
        while(
$row mysql_fetch_assoc($query)) {
            
$tran = array();
            foreach (
$row as $var => $value) {
                
$tran["{{$var}}"] = $value;
            }
            
$output .= strtr($template_source$tran);
        }
        return 
$output;
    }
    echo 
template($template_source"news");

?>

It's probably not the prob, but I'll say it JIC. Isn't the table name supposed to be used without quotes of any kind in the query?, i.e. instead of

<?php 
$sql 
"SELECT * FROM `news`";
?>

one would use

<?php 
$sql 
"SELECT * FROM news";
?>

Quote from filur :[...]

Hmm, I didn't spot that, maybe that's the problem I'll try the new function.

Quote from NotAnIllusion :It's probably not the prob, but I'll say it JIC. Isn't the table name supposed to be used without quotes of any kind in the query?, i.e. instead of

<?php 
$sql 
"SELECT * FROM `news`";
?>

one would use

<?php 
$sql 
"SELECT * FROM news";
?>


I've always used ` on table names, maybe it's an old way of doing it, I'll try changing it nothing happens with filur's function.

Thanks for your help.
the backticks are official means of encapsulating table and cell names in mysql, so that won't be the problem.
I tried filur's function and it's outputting an empty string, I'll poke around a bit more and see if I can find out what's wrong

EDIT: Fixed, I think it was filur's function that made it work, I'd just forgotten to change one of the variables i was putting into the function. Thanks everyone!

Weird PHP/MySQL problem
(6 posts, started )
FGED GREDG RDFGDR GSFDG