Results 1 to 2 of 2

Thread: mysql_fetch_assoc help

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    6

    mysql_fetch_assoc help

    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 Code:
    <?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?
    Last edited by AJByrne; Mar 31st, 2006 at 07:26 AM.

  2. #2
    Fanatic Member jcavard's Avatar
    Join Date
    Jul 2005
    Location
    Quebec, CANADA
    Posts
    534

    Re: mysql_fetch_assoc help

    try adding $array = array(); before the while loop. this might fix the undefined variable

    PHP Code:
    <?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 Code:
    <?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
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

    accoustic emo-rock band: a tailormade fable

    Visual Studio 2003 / Framework 1.1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width