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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Burton333
Forum Newbie
Posts: 15
Joined: Wed Apr 01, 2009 7:04 pm

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

Post 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?
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

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

Post 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].
Burton333
Forum Newbie
Posts: 15
Joined: Wed Apr 01, 2009 7:04 pm

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

Post 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!
Post Reply