ridiculous Posted November 12, 2006 Share Posted November 12, 2006 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 https://forums.phpfreaks.com/topic/27007-resolvedthanks-array-to-list-of-variables-how-do-you-do-it/ Share on other sites More sharing options...
chiprivers Posted November 12, 2006 Share Posted November 12, 2006 <? php$query = "SELECT blah blah blah";$result = mysql_query($query);while ($row = mysql_fetch_array($result)) {$Course_Pic = $row['Course_Pic'];$Course_Name = $row['Course_Name'];etc etc}DISCLAIMER: I am relatively new to PHP / MySQL but I think this is right :D?> Link to comment https://forums.phpfreaks.com/topic/27007-resolvedthanks-array-to-list-of-variables-how-do-you-do-it/#findComment-123485 Share on other sites More sharing options...
nert Posted November 12, 2006 Share Posted November 12, 2006 I think $row['field_name'] is case sensitive too Link to comment https://forums.phpfreaks.com/topic/27007-resolvedthanks-array-to-list-of-variables-how-do-you-do-it/#findComment-123487 Share on other sites More sharing options...
ridiculous Posted November 12, 2006 Author Share Posted November 12, 2006 $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 https://forums.phpfreaks.com/topic/27007-resolvedthanks-array-to-list-of-variables-how-do-you-do-it/#findComment-123488 Share on other sites More sharing options...
printf Posted November 12, 2006 Share Posted November 12, 2006 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 variablesecho $Course_Begin_Time;[/code] Link to comment https://forums.phpfreaks.com/topic/27007-resolvedthanks-array-to-list-of-variables-how-do-you-do-it/#findComment-123491 Share on other sites More sharing options...
ridiculous Posted November 12, 2006 Author Share Posted November 12, 2006 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 https://forums.phpfreaks.com/topic/27007-resolvedthanks-array-to-list-of-variables-how-do-you-do-it/#findComment-123492 Share on other sites More sharing options...
trq Posted November 12, 2006 Share Posted November 12, 2006 [quote]How much memory does this waste? [/quote]Not enough to be concerned about. Link to comment https://forums.phpfreaks.com/topic/27007-resolvedthanks-array-to-list-of-variables-how-do-you-do-it/#findComment-123498 Share on other sites More sharing options...
ridiculous Posted November 12, 2006 Author Share Posted November 12, 2006 None of this is working. Now I'm concerned. Link to comment https://forums.phpfreaks.com/topic/27007-resolvedthanks-array-to-list-of-variables-how-do-you-do-it/#findComment-123502 Share on other sites More sharing options...
printf Posted November 12, 2006 Share Posted November 12, 2006 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 https://forums.phpfreaks.com/topic/27007-resolvedthanks-array-to-list-of-variables-how-do-you-do-it/#findComment-123503 Share on other sites More sharing options...
trq Posted November 12, 2006 Share Posted November 12, 2006 [quote]None of this is working. Now I'm concerned.[/quote]Really... your yet to ask a question. Stating that something does not work does not help. Post the RELEVANT code, and a description of the problem! Link to comment https://forums.phpfreaks.com/topic/27007-resolvedthanks-array-to-list-of-variables-how-do-you-do-it/#findComment-123504 Share on other sites More sharing options...
ridiculous Posted November 12, 2006 Author Share Posted November 12, 2006 [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 https://forums.phpfreaks.com/topic/27007-resolvedthanks-array-to-list-of-variables-how-do-you-do-it/#findComment-123507 Share on other sites More sharing options...
chiprivers Posted November 12, 2006 Share Posted November 12, 2006 have you tried changing$Course_Pic= ' . $row['Course_Pic'] . ';to$Course_Pic = $row['Course_Pic']; Link to comment https://forums.phpfreaks.com/topic/27007-resolvedthanks-array-to-list-of-variables-how-do-you-do-it/#findComment-123510 Share on other sites More sharing options...
trq Posted November 12, 2006 Share Posted November 12, 2006 [code=php:0]while ($row = mysql_fetch_assoc($result)) { extract($row); echo $Course_Pic; echo $Course_Name; // etc etc ...}[/code]PS; No need for the sarcastic caps either. Link to comment https://forums.phpfreaks.com/topic/27007-resolvedthanks-array-to-list-of-variables-how-do-you-do-it/#findComment-123512 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.