0

How can i get all the value of a column(first value to the last value) in my table in php, then put it on a container(array) so i can use the data
to plot the table values on a chart. Im using FusionCharts and i saw an example in which it present a form, ask for inputs from the user and
it plot the values on a table when i click Ok button. By the way the code is written in PHP and it uses $_POST['variable'] thing. I tried to write a code to get all the column values but it only get the first value and its not on an array.

<?php
function dbquery($sql) {

  $arr    = array();
$conn= odbc_connect('Landingpage','','');
  $rs     = odbc_exec($conn,$sql);
  $x      = 1;
  while (odbc_fetch_row($rs)) {
    for ($y = 1; $y <= odbc_num_fields($rs); $y++) 
      $arr[$x][$y] = odbc_result($rs,$y);
    $x++;
  }
  if ($x > 1)
    return $arr;
}

$arr=dbquery("SELECT * FROM tblUser");
echo $arr[1][1]
?>
2
Contributors
1
Reply
16
Views
3 Years
Discussion Span
Last Post by ryantroop
0

I hated ODBC on PHP with GoDaddy's windows servers, so much in fact that I abandoned it even after I got it working.

This worked for me, I am assuming it will be a starting point for you. If not, I appologize for wasting your time.

PHP5 has spotty support for ODBC, and even their documentation gives misleading code.

I really hope this works for you,

Ryan

please note, the query function will probably need some adjustments for all cases. If you pass in "null" as a param, it may fail hard. The prepare/exec combo should sanitize for you, but you will want to be sure before putting this into production.

function DBConnect()
{
  $usr = "usr";
  $pw = "pw";
  $dbURL = "url.com";
  $dbname = 'name';
  return odbc_connect("Driver={SQL SERVER};Server=$dbURL; Database=$dbname;", $usr, $pw);
}

function sql_query($rConn, $cStmt, $aValues)
{
  if (!rConn)
    return;

    $cArgs = "";
    $iParams = count($aValues);
    for ($iLup = 0; $iLup < $iParams; $iLup++)
        {
            if ($iLup > 0)
                {
                    $cArgs .= ",";
                }

            $cArgs .= "?";
        }

  if ($iParams == 0)
    {
      $cFullStmt = "EXEC $cStmt";
      $oResult = odbc_exec($rConn, $cFullStmt);
    }
  else
    {
      $cFullStmt = "EXEC $cStmt $cArgs";
      $oResult = odbc_prepare($rConn, $cFullStmt);
        $bExec = odbc_execute($oResult, $aValues);
    }

  if ($bExec && $iParams > 0)
    return $oResult;
    else if ($oResult && $iParams == 0)
        return $oResult;
    else
    {
          return $oResult;
    }
}


$rConn = DBConnect();
$cStmt = "select * from tbl_user";
$aValues = array();
$Results = sql_query($rConn, $cStmt, $aValues);

if (count($Results) > 0)
{
  //do stuff here...
  for ($i=0;$i<count($Results);$i++)
    {
      $Result = $Results[$i];
      //$Result should now be an associative array with column names as keys.
    }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.