Jump to content

[RESOLVED,thanks!] Array to list of variables. How do you do it?


ridiculous

Recommended Posts

  My question is how to convert an array from a query into a list of $variables


[color=blue]Per you suggestions, I've tried 2 techniques so far. Both result in an error:"parse error, unexpected $"[/color]

I'm going to condense my code to make things easy as possible, assume I have just pulled a query:

      { 
             while   ($row = mysql_fetch_assoc($result))   [color=blue]<---Is something missing here?>[/color]
            {
$Course_Pic= ' . $row['Course_Pic'] . ';
            $Course_Name= ' . $row['Course_Name'] . ';
[color=blue]Or is the syntax wrong?-->[/color]$Course_Id=' . $row['Course_Id'] . ';
 


[color=maroon]Now I tried the above an no dice. Then I took the extract approach:[/color]

extract (mysql_fetch_assoc ($result)); 


             echo $Course_Pic;
echo $Course_Name;


[color=maroon]Again, same result. I cut off all other code and tried to print a variable with no result. Maybe I am failing to understand your suggestions, or maybe there is some better way to do this. I'm sorry, I'm new and looking everywhere for an answer with no success.[/color]
Link to comment
Share on other sites

$sql = "SELECT
Course_Pic,
Course_Name,
Course_Id,
Description,
Did_You_Know,
Days_Meets,
Course_Begin_Time,
Instructor_1,
Instructor_Pic,
Site,
Address,
City,
State,
Zip,
Course_Fees
FROM
course_catalogue
ORDER BY RAND() LIMIT 1"; 

$refresh_rate = 8; ////This is the variable that determines the refresh rate.

if( ($result = mysql_query($sql)) === FALSE )

  echo mysql_error(); 
} else

       if( ($result = mysql_query($sql)) === FALSE )

  echo mysql_error(); 
} else

        /// Fetch the row from the DB as an associative array
        // then you can use the $row['key'] methods to retrieve data
     
  while   ($row = mysql_fetch_assoc($result))   [color=limegreen]<---Is this the problem>[/color]
{
[color=red]error here------>[/color] $Course_Pic= ' . $row['Course_Pic'] . ';
$Course_Name= ' . $row['Course_Name'] . ';
[color=limegreen]Or is this forming my problem?-->[/color]$Course_Id=' . $row['Course_Id'] . ';
$Description=' . $row['Description'] . ';
$Did_You_Know=' . $row['Did_You_Know'] . ';
$Days_Meets=' . $row['Days_Meets'] . ';
$Course_Begin_Time=' . $row['Course_Begin_Time'] . ';
$Instructor_1=' . $row['Instructor_1'] . ';
$Instructor_Pic=' . $row['Instructor_Pic'] . ';
$Site= ' . $row['Site'] . ';
$Address=' . $row['Address'] . ';
$City= ' . $row['City'] . ';
$State=' . $row['State'] . ';
$Zip=' . $row['Zip'] . ';
$Course_Fees=' . $row['Course_Fees'] . ';
}

------------------------------------------

I modified the code to this, still no dice. Hits an error as soon as gets to variable conversion line.
Link to comment
Share on other sites

If you must waste memory rewriting already existing variables, then use evil [b]extract()[/b] on the fetched result!

[code]$sql = "SELECT Course_Pic, Course_Name, Course_Id, Description, Did_You_Know, Days_Meets, Course_Begin_Time, Instructor_1, Instructor_Pic, Site, Address, City, State, Zip, Course_Fees FROM course_catalogue ORDER BY RAND() LIMIT 1";

$refresh_rate = 8;

$result = mysql_query ( $sql ) or die ( mysql_error () );

if ( mysql_num_rows ( $result ) != 1 )
{
echo 'no results returned for query';
exit ();
}

extract ( mysql_fetch_assoc ( $result ) );

// continue (the array is converted) to localized variables

echo $Course_Begin_Time;[/code]



Link to comment
Share on other sites

Darn. That still doesn't work. Here's what I have now:


include ("function_index.php");


$conn = db_connect();

$sql = "SELECT
Course_Pic,
Course_Name,
Course_Id,
Description,
Did_You_Know,
Days_Meets,
Course_Begin_Time,
Instructor_1,
Instructor_Pic,
Site,
Address,
City,
State,
Zip,
Course_Fees
FROM
course_catalogue
ORDER BY RAND() LIMIT 1"; 

$refresh_rate = 8; ////This is the variable that determines the refresh rate.

if( ($result = mysql_query($sql)) === FALSE )

  echo mysql_error(); 
} else

        /// Fetch the row from the DB as an associative array
        // then you can use the $row['key'] methods to retrieve data
   
  extract (mysql_fetch_assoc ($result)); 
echo $Course_Pic;
echo $Course_Name;
echo $Course_Id;
echo $Description;
echo $Did_You_Know;
echo $Days_Meets;
echo $Course_Begin_Time;
echo $Instructor_1;
echo $Instructor_Pic;
echo $Site;
echo $Address;
echo $City;
echo $State;
echo $Zip;
echo $Course_Fees;

print $Zip;
?>
Link to comment
Share on other sites

less than copy, all I was saying is doing this...

[code]$the_column = $row['the_column'];[/code]

Would create (2) variables ($the_column, $row['the_column']), which are exactly the same. but because you copied them, you create overhead onto the memory stack! Using [b]extract()[/b], the way I did, you just convert the array to the localized [b]array key name[/b], the overhead in doing that, is just the memory allocated to do the conversion. Which is much less than copying! But if your doing a db result loop, then ($row) already holds the variables you need so you shouldn't do [b]copy or extract[/b], because in effect your doing extra stuff that you don't need to, which is a very bad habit that you will find very hard to break, if you start doing it!


Sonia
Link to comment
Share on other sites

[color=red][size=14pt]My question is how to convert an array from a query into a list of $variables[/size][/color]


[color=blue]Per you suggestions, I've tried 2 techniques so far. Both result in an error:"parse error, unexpected $"[/color]

I'm going to condense my code to make things easy as possible, assume I have just pulled a query:

      { 
             while   ($row = mysql_fetch_assoc($result))   [color=blue]<---Is something missing here?>[/color]
            {
[color=red]error flag cites this line------>   [/color] $Course_Pic= ' . $row['Course_Pic'] . ';
            $Course_Name= ' . $row['Course_Name'] . ';
[color=blue]Or is the syntax wrong?-->[/color]$Course_Id=' . $row['Course_Id'] . ';
            $Description=' . $row['Description'] . ';
            $Did_You_Know=' . $row['Did_You_Know'] . ';
            $Days_Meets=' . $row['Days_Meets'] . ';


[color=maroon]Now I tried the above an no dice. Then I took the extract approach:[/color]

extract (mysql_fetch_assoc ($result)); 


             echo $Course_Pic;
echo $Course_Name;


[color=maroon]Again, same result. I cut off all other code and tried to print a variable with no result. Maybe I am failing to understand your suggestions, or maybe there is some better way to do this. I'm sorry, I'm new and looking everywhere for an answer with no success.[/color]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.