Convert Mysql result to HTML

Discussion in 'PHP' started by doodnom, Apr 13, 2007.

  1. #1
    <?php
    $mysql_host = 'myhost';
    $mysql_user='root';
    $mysql_password='';
    $mysql_db='customer';

    if (!$link = mysql_connect($mysql_host,$mysql_user,$mysql_password)) {
    echo 'Could not connect to mysql';
    exit;
    }
    if (!mysql_select_db($mysql_db, $link)) {
    echo 'Could not select database';
    exit;
    }
    $sql = "SELECT * FROM customer"
    $result = mysql_query($sql, $link);
    if (!$result) {
    echo "DB Error, could not query the database\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
    }
    while ($row = mysql_fetch_assoc($result)) {
    echo $row['id'];
    echo "<br>";
    echo $row['customer'];
    echo "<br>";
    echo $row['addr'];
    echo "<br>";
    }
    mysql_free_result($result);
    ?>
     
    doodnom, Apr 13, 2007 IP
  2. doodnom

    doodnom Guest

    Messages:
    47
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi ,

    How can i convert $result to HTML file ( one row to one html file )

    anyone suggestion me please

    Thank you,
     
    doodnom, Apr 13, 2007 IP
  3. tschrock

    tschrock Peon

    Messages:
    527
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I am NOT a PHP guy, but I employ one who has done something like this in the past. I believe PHP has a command to write to a file. As long as your server allows for files to be created by scripts on the server, you should be able to use it.

    A word of caution though - most cut rate hosting services like Godaddy for example, prohibit writing files because of the security implications. If one sloppy person on one server writes a bad script, it can compromise all of the websites on that server.
     
    tschrock, Apr 13, 2007 IP
  4. themole

    themole Peon

    Messages:
    82
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Here you go, the changes are in bold:

    
    <?php
    $mysql_host = 'myhost';
    $mysql_user='root';
    $mysql_password='';
    $mysql_db='customer';
    
    if (!$link = mysql_connect($mysql_host,$mysql_user,$mysql_password)) 
    {
    	echo 'Could not connect to mysql';
    	exit;
    }
    if (!mysql_select_db($mysql_db, $link)) 
    {
    	echo 'Could not select database';
    	exit;
    }
    
    $sql = "SELECT * FROM customer"
    
    $result = mysql_query($sql, $link);
    
    if (!$result)
    {
    	echo "DB Error, could not query the database\n";
    	echo 'MySQL Error: ' . mysql_error();
    	exit;
    }
    
    while ($row = mysql_fetch_assoc($result)) 
    {
    	echo $row['id'];
    	echo "<br>";
    	echo $row['customer'];
    	echo "<br>";
    	echo $row['addr'];
    	echo "<br>";
    	
    	[b]$text_output = "$row[id] $row[customer] $row[addr]";[/b]
    	
    	[b]
    	$fp = @fopen("/path/to/file/$row[id].txt", 'w');
    	@fwrite($fp, $text_output);
    	@fclose($fp);
    	[/b]
    }
    
    
    mysql_free_result($result);
    ?>
    ?>
    Code (markup):
    -the mole
     
    themole, Jul 29, 2007 IP