PDA

View Full Version : How would i do this...? [php]/[mysql]



chrisclick
December 31st, 2007, 06:49 PM
Ok, im trying to make my footer use a mysql database instead of a text file...

but im having some problems...
I am using the code to get the mysql data:


$dbhost = 'localhost';
$dbuser = 'designs1';
$dbpass = '*****';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'designs1_footer';
mysql_select_db($dbname);

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);

$query = "SELECT * FROM footer ORDER BY RAND() LIMIT 1";

$result = mysql_query($query);

while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$row['comment'];
}
mysql_close($conn);
That works fine. but i cannot put that into to footer code, it doesnt work if I try:


$string = while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$row['comment'];
}


it works if I use



$string = $array[0];

but it only shows ONE letter from the whole word...

So does anyone know how to do this?

hl
December 31st, 2007, 07:20 PM
I didn't even know setting a variable equal to a loop was possible. Anyway, this should work:


$dbhost = 'localhost';
$dbuser = 'designs1';
$dbpass = '*****';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'designs1_footer';
mysql_select_db($dbname);
$query = "SELECT * FROM footer ORDER BY RAND() LIMIT 1";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$string = $row['comment'];
mysql_close($conn); // you don't actually need this, PHP auto closes it when the script terminates, though if you have multiple connections in one process.. it saves memory

chrisclick
December 31st, 2007, 07:35 PM
OMG! what would i do without you helpfull php'ers :P