Jump to content

MySQL, PHP - Turn result into variable


SupremeBeing

Recommended Posts

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

 

Link to comment
Share on other sites

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...
}

Link to comment
Share on other sites

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  :P

 

Thanks

Link to comment
Share on other sites

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;
}
?>

Link to comment
Share on other sites

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;
}
?>

Link to comment
Share on other sites

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 "{" "}"  ;D

 

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.

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.