Page 1 of 1

Arrays - Why can't I print value in table?

Posted: Sun Apr 19, 2009 3:42 pm
by Burton333
I am trying to populate a table in mySQL by exploding a file from a website. I have got it to add the fields correctly, but not the foreign key of the other table it is supposed to reference. I have tried to create a function to select the primary key(pkDeptID) from the table as seen here in validation_functions.php:

Code: Select all

function deptIDNum($dept){
    include("connect.inc");
    //Create your SQL statement
    $sql = "SELECT pkDeptID ";
    $sql .= "FROM tblDept ";
    $sql .= "WHERE fldDeptName ='" . $dept . "'";
    $results = $myDatabase->select($sql);
    return $results;
}
And then this is the code in order to populate all the information into the table(tblClasses):

Code: Select all

<?php
$lines = file('http://giraffe.uvm.edu/~rgweb/batch/curr_enroll_fall.txt');
$i=1;
//files to connect to database
include("db.inc");
include("error.inc");
include("mydb.inc");
include ("validation_functions.php");
while ($i <= 3030)
    {
    list($dept, $num, $title) =
    explode(",", $lines[$i]);
    // create sql statements to insert into table
       $sql = "INSERT INTO ";
       $sql .= "tblClasses ";
       $sql .= "SET ";
       $sql .= "fkDeptID='" . $j . "',";
       $sql .= "fldClassNum='" . $num . "',";
       $sql .= "fldClassName='" . $title . "';";
    $j=deptIDNum($dept);
    echo $j;
    //Make a connection
    include("connect.inc");                 
    // execute SQL statement
//$className=$myDatabase->insert($sql);                     
    $i++;
    }
?>
When I echo out $j in order to check to see if it is obtaining the correct number to insert into tblClasses, it prints "ArrayArrayArrayArray...etc." instead of the correct primary key. Can someone please tell me what I am doing wrong, and how I can correct this to obtain the number(pkDeptID) of the table in order to make it the foreign key in tblClasses?

Re: Arrays - Why can't I print value in table?

Posted: Sun Apr 19, 2009 5:08 pm
by jayshields
It's difficult to say without knowing exactly how your SQL class works.

At a guess it the select function returns a result set as a 3D array. To access the data you want you'll probably need to use $j[0]['pkDeptID'] or $j[0][0].

Re: Arrays - Why can't I print value in table?

Posted: Sun Apr 19, 2009 5:25 pm
by Burton333
Actually that did work. I changed it to $j[0]['pkDeptID'] and now it is getting the correct number. Thank you very much jayshields!