PDA

Click to See Complete Forum and Search --> : mysql_fetch_assoc help


AJByrne
Mar 31st, 2006, 04:02 AM
i got this function that create's a dynamic HTML table the outputs SOL query result the code works if i manually enter a SQL query but if i add assign a variable to the query it brings up an error.

Warning: mysql_fetch_assoc(): on line 61

Notice: Undefined variable: array on line 64

<?PHP
include("dbandy.php");

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

$dbname = 'db_andrewb';
mysql_select_db($dbname)or die('error finding DB');

$qstring = $_SERVER['QUERY_STRING'];

$qstring = str_replace('queries=', '',$qstring);

echo urldecode($qstring);

$query = $qstring; //if i use this it dosen't work

//$query = "Select * from students"; if i use this it works


$result = mysql_query($query);


?>

<?PHP
function array2table($arr,$width)
{
$count = count($arr);
if($count > 0){
reset($arr);
$num = count(current($arr));
echo "<table align=\"center\" border=\"1\"cellpadding=\"5\" cellspacing=\"0\" width=\"$width\">\n";
echo "<tr>\n";
foreach(current($arr) as $key => $value){
echo "<th>";
echo $key."&nbsp;";
echo "</th>\n";
}
echo "</tr>\n";
while ($curr_row = current($arr)) {
echo "<tr>\n";
$col = 1;
while ($curr_field = current($curr_row)) {
echo "<td>";
echo $curr_field."&nbsp;";
echo "</td>\n";
next($curr_row);
$col++;
}
while($col <= $num){
echo "<td>&nbsp;</td>\n";
$col++;
}
echo "</tr>\n";
next($arr);
}
echo "</table>\n";
}
}

?>
<?php

while($row = mysql_fetch_assoc( $result)){
$array[] = $row; }

array2table($array,600); // Will output a table of 600px width

?>


can anyone please help?

jcavard
Mar 31st, 2006, 10:45 AM
try adding $array = array(); before the while loop. this might fix the undefined variable


<?php
$array = array();
while($row = mysql_fetch_assoc( $result)){
$array[] = $row; }

array2table($array,600); // Will output a table of 600px width

?>


also, since this is all PHP code, you can get rid of those unnecessary <?PHP ?> TAG.



<?PHP
include("dbandy.php");

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

$dbname = 'db_andrewb';
mysql_select_db($dbname)or die('error finding DB');

$qstring = $_SERVER['QUERY_STRING'];

$qstring = str_replace('queries=', '',$qstring);

echo urldecode($qstring);

$query = $qstring; //if i use this it dosen't work

//$query = "Select * from students"; if i use this it works


$result = mysql_query($query);

function array2table($arr,$width)
{
$count = count($arr);
if($count > 0){
reset($arr);
$num = count(current($arr));
echo "<table align=\"center\" border=\"1\"cellpadding=\"5\" cellspacing=\"0\" width=\"$width\">\n";
echo "<tr>\n";
foreach(current($arr) as $key => $value){
echo "<th>";
echo $key."&nbsp;";
echo "</th>\n";
}
echo "</tr>\n";
while ($curr_row = current($arr)) {
echo "<tr>\n";
$col = 1;
while ($curr_field = current($curr_row)) {
echo "<td>";
echo $curr_field."&nbsp;";
echo "</td>\n";
next($curr_row);
$col++;
}
while($col <= $num){
echo "<td>&nbsp;</td>\n";
$col++;
}
echo "</tr>\n";
next($arr);
}
echo "</table>\n";
}
}

$array = array();

while($row = mysql_fetch_assoc($result)){
$array[] = $row;
}

array2table($array,600); // Will output a table of 600px width

?>


it won't fix anything, but just clean the code, hope that helped