SupremeBeing Posted February 1, 2011 Share Posted February 1, 2011 Hello Everyone, I have a quick question for you all, I think its fairly simple... I have created a database and I am using PHP to grab the data: $usera = $_SESSION['username']; $query2 = "SELECT * FROM tracker WHERE id = '$usera', hidden = yes"; mysql_query($query2) or die('Error, query failed : ' . mysql_error()); This hopefully will return multiple rows which look like this in the database. id username date reps hidden 1 supremebeing 2011-01-02 30 yes 4 supremebeing 2011-04-02 46 yes How would i turn each result into a variable eg: $date1 = 2011-01-02; $date2 = 2011-04-02; $reps1 = 30; $reps2 = 46; I think i have explained that well enough for you to understand, please reply if not though and i will provide more information. Thanks in Advance Quote Link to comment https://forums.phpfreaks.com/topic/226386-mysql-php-turn-result-into-variable/ Share on other sites More sharing options...
AbraCadaver Posted February 1, 2011 Share Posted February 1, 2011 First off, you have an error in the query. Instead of a , in the WHERE clause it should probably be AND. Second, you don't assign what is returned from the query to a variable. Thirdly you aren't escaping the data sent to the database (SQL injection): $query2 = "SELECT * FROM tracker WHERE id = '" . mysql_real_escape_string($usera) . "' AND hidden = 'yes'"; $result = mysql_query($query2) or die('Error, query failed : ' . mysql_error()); //loop through results one row at a time while($row = mysql_fetch_assoc($result)) { echo $row['date']; //etc... } Quote Link to comment https://forums.phpfreaks.com/topic/226386-mysql-php-turn-result-into-variable/#findComment-1168474 Share on other sites More sharing options...
SupremeBeing Posted February 1, 2011 Author Share Posted February 1, 2011 Hi, AbraCadaver, thanks for the quick reply and for pointing out that error. Your suggestion would list the results, I need to store the results to variables so they can be called later on. I am using jqplot to plot a graph, and need to input the results like this: line1 = [['$date1',$rep1],['$date2',$rep2]]; etc... If "variables" is the wrong word to use, could you please correct me so I don't make a fool of myself anymore Thanks Quote Link to comment https://forums.phpfreaks.com/topic/226386-mysql-php-turn-result-into-variable/#findComment-1168481 Share on other sites More sharing options...
AbraCadaver Posted February 1, 2011 Share Posted February 1, 2011 OK, normally you would use an array: while($row = mysql_fetch_assoc($result)) { $data[] = $row; } //then access later as $data[0]['date']; $data[1]['date']; $data[0]['reps']; $data[1]['reps']; Quote Link to comment https://forums.phpfreaks.com/topic/226386-mysql-php-turn-result-into-variable/#findComment-1168484 Share on other sites More sharing options...
SupremeBeing Posted February 1, 2011 Author Share Posted February 1, 2011 Hi again, Your code works when i echo it, but when i try to insert it into my javascript it comes out like: <?php echo Array['date'];?> Any ideas why? Heres the section im trying to echo into : <?php if ( $query2 == '' ) { echo 'Error!'; } else { print <<<HERE <h4>Graph of $ex1</h4> <script type="text/javascript"> $(document).ready(function(){ line1 = [['2011-01-31',6],['<?php echo $data[0]['date'];?>',<?php echo $data[0]['reps'];?>]]; newPlot = $.jqplot('newgraph', [line1], { axes:{ xaxis:{ renderer:$.jqplot.DateAxisRenderer }, yaxis:{ autoscale: true } }, series:[{lineWidth:3}], seriesDefaults: {fill: true, color: "", fillAndStroke: true, fillAlpha:0.4, shadow:false} }); }); </script> <div id="newgraph" style="margin: 24px 12px; height:192px;"></div> HERE; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/226386-mysql-php-turn-result-into-variable/#findComment-1168548 Share on other sites More sharing options...
AbraCadaver Posted February 1, 2011 Share Posted February 1, 2011 Hi again, Your code works when i echo it, but when i try to insert it into my javascript it comes out like: <?php echo Array['date'];?> Any ideas why? We would need to see how you are trying to put it into javascript. Quote Link to comment https://forums.phpfreaks.com/topic/226386-mysql-php-turn-result-into-variable/#findComment-1168552 Share on other sites More sharing options...
SupremeBeing Posted February 1, 2011 Author Share Posted February 1, 2011 Yeah, thought so; just edited last post, but heres the section again: <?php if ( $query2 == '' ) { echo ''; } else { print <<<HERE <h4>Graph of $ex1</h4> <script type="text/javascript"> $(document).ready(function(){ line1 = [['2011-01-31',6],['<?php echo $data[0]['date'];?>',<?php echo $data[0]['reps'];?>]]; newPlot = $.jqplot('newgraph', [line1], { axes:{ xaxis:{ renderer:$.jqplot.DateAxisRenderer }, yaxis:{ autoscale: true } }, series:[{lineWidth:3}], seriesDefaults: {fill: true, color: "", fillAndStroke: true, fillAlpha:0.4, shadow:false} }); }); </script> <div id="newgraph" style="margin: 24px 12px; height:192px;"></div> HERE; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/226386-mysql-php-turn-result-into-variable/#findComment-1168556 Share on other sites More sharing options...
AbraCadaver Posted February 1, 2011 Share Posted February 1, 2011 You're already printing it so why try and echo it? line1 = [['2011-01-31',6],['{$data[0]['date']}',{$data[0]['reps']}]]; Quote Link to comment https://forums.phpfreaks.com/topic/226386-mysql-php-turn-result-into-variable/#findComment-1168557 Share on other sites More sharing options...
SupremeBeing Posted February 1, 2011 Author Share Posted February 1, 2011 AbraCadaver, you are a legend. I can't thank you enough! I though i was barking up the wrong tree trying to echo. New knowledge for my PHP bank "{" "}" Quote Link to comment https://forums.phpfreaks.com/topic/226386-mysql-php-turn-result-into-variable/#findComment-1168561 Share on other sites More sharing options...
AbraCadaver Posted February 1, 2011 Share Posted February 1, 2011 AbraCadaver, you are a legend. I can't thank you enough! I though i was barking up the wrong tree trying to echo. New knowledge for my PHP bank "{" "}" Cool... Just FYI, you don't always need {} around vars but it makes it easier to pick them out AND some complex vars like the arrays or object properties {$some_obj->my_var} need it to tell the PHP parser that it is a var and not regular text. Quote Link to comment https://forums.phpfreaks.com/topic/226386-mysql-php-turn-result-into-variable/#findComment-1168565 Share on other sites More sharing options...
SupremeBeing Posted February 2, 2011 Author Share Posted February 2, 2011 Cheers, I'll remember. And thanks for helping me. Quote Link to comment https://forums.phpfreaks.com/topic/226386-mysql-php-turn-result-into-variable/#findComment-1168624 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.