Welcome to WebmasterWorld Guest from 50.16.52.237

Forum Moderators: open

Message Too Old, No Replies

code structure to display mysql data in array using php? please help

trying to get data in mysql using php. i have an array but dont know how to

     
3:39 pm on Sep 3, 2010 (gmt 0)

New User

5+ Year Member

joined:Sept 3, 2010
posts: 16
votes: 0


Hello everyone. Im a beginner at both MySQL and php and had a little question regarding the proper way to get MySQL data in php.

After i log in to my database, lets say i get these values:

echo "Lighting watts: ".$row['light']; echo "<br />";
echo "Kitchen watts: ".$row['kitchen']; echo "<br />";
echo "House watts: ".$row['house']; echo "<br />";
echo "Misc watts: ".$row['misc']; echo "<br />";
echo "Total Watt Usage: ".$row['gensize']; echo "<br />";


Now what i want to do is to use these results with google api chart to fill in the array for addDataSet(array(light,kitchen,house,misc)); what is the proper way to call these because i tried the example above and it didnt work. Thanks again

?php
require ("gChart.php");

$piChart = new gPieChart();
$piChart->set3D(true);
$piChart->addDataSet(array(112,315,66,40));
$piChart->setLegend(array("Lighting", "Kitchen", "House","Misc"));
$piChart->setLabels(array("Lighting", "Kitchen", "House","Misc"));
$piChart->setColors(array("ff3344", "11ff11", "22aacc", "3333aa"));
?>
<img src="<?php print $piChart->getUrl(); ?>" />
4:29 pm on Sept 3, 2010 (gmt 0)

Senior Member from GB 

WebmasterWorld Senior Member brotherhood_of_lan is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 30, 2002
posts:4845
votes: 4


Hello magneto,

You need something along the lines of

// 1
mysql_connect("host","username","password") or die('Bad MySQL Login');
mysql_select_db("yourdatabase") or die('Wrong DB');

// 2
$query = mysql_query('SELECT columns FROM yourtable') or die(mysql_error());

// 3
while($row = mysql_fetch_array($query,MYSQL_ASSOC))
{
print_r($row);
}


The above example will
1) Connect to your database
2) Perform a query against MySQL
3) A while statement is looping through the results and printing them, as an example.

Since you're relatively new to PHP->MySQL, it's well worth checking out the PHP manual [php.net] for how PHP and MySQL work together.
5:03 pm on Sept 3, 2010 (gmt 0)

New User

5+ Year Member

joined:Sept 3, 2010
posts: 16
votes: 0


Thanks for your reply brotherhood, i do have that already...didnt post it because i thought it was irrelevant but here's my full code:

<?php
mysql_connect("localhost", "username", "pass") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

$result = mysql_query("SELECT * FROM offgrid_calculator WHERE id= (SELECT max(id) FROM offgrid_calculator)")
or die(mysql_error());

$row = mysql_fetch_array( $result );

echo "Name: ".$row['Name']; echo "<br />";
echo "Phone Number: ".$row['Phone']; echo "<br />";
echo "Email: ".$row['Email']; echo "<br />";
echo "Your Comments: ".$row['Feedback']; echo "<br />";
echo "Your Unique ID: ".$row['id']; echo "<br />";
echo "Lighting watts: ".$row['light']; echo "<br />";
echo "Kitchen watts: ".$row['kitchen']; echo "<br />";
echo "House watts: ".$row['house']; echo "<br />";
echo "Misc watts: ".$row['misc']; echo "<br />";
echo "Total Watt Usage: ".$row['gensize']; echo "<br />";

$email = "sender@sender.com"; //senders e-mail adress
$recipient = $row['Email']; //recipient
$subject = "Off Grid Calculator Results Coming Soon!"; //subject

mail($recipient, $subject, $mailtxt); //mail command :)?>

<?php
require ("gChart.php");
$piChart = new gPieChart();
$piChart->set3D(true);
$piChart->addDataSet(array(112,315,66,40));
$piChart->setLegend(array("Lighting", "Kitchen", "House","Misc"));
$piChart->setLabels(array("Lighting", "Kitchen", "House","Misc"));
$piChart->setColors(array("ff3344", "11ff11", "22aacc", "3333aa"));
?>
<img src="<?php print $piChart->getUrl(); ?>" />


so in other words, im able to display the echo in the page, but i also want to email it and to be able to have those values display in the google pieChart. I have tried many combinations of code to try and add the code correctly but nothing works? any suggestions? thanks
7:57 pm on Sept 3, 2010 (gmt 0)

Senior Member from GB 

WebmasterWorld Senior Member brotherhood_of_lan is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 30, 2002
posts:4845
votes: 4


email it


Use a variable to store all the text you want in the e-mail, so maybe change the following

echo "Name: ".$row['Name']; echo "<br />";
echo "Phone Number: ".$row['Phone']; echo "<br />";
echo "Email: ".$row['Email']; echo "<br />";
echo "Your Comments: ".$row['Feedback']; echo "<br />";
echo "Your Unique ID: ".$row['id']; echo "<br />";
echo "Lighting watts: ".$row['light']; echo "<br />";
echo "Kitchen watts: ".$row['kitchen']; echo "<br />";
echo "House watts: ".$row['house']; echo "<br />";
echo "Misc watts: ".$row['misc']; echo "<br />";
echo "Total Watt Usage: ".$row['gensize']; echo "<br />";
$email = "sender@sender.com"; //senders e-mail adress
$recipient = $row['Email']; //recipient
$subject = "Off Grid Calculator Results Coming Soon!"; //subject



$emailtxt = '';
$emailtxt .= "Name: ".$row['Name']; $emailtxt .= "<br />";
$emailtxt .= "Phone Number: ".$row['Phone']; $emailtxt .= "<br />";
$emailtxt .= "Email: ".$row['Email']; $emailtxt .= "<br />";
$emailtxt .= "Your Comments: ".$row['Feedback']; $emailtxt .= "<br />";
$emailtxt .= "Your Unique ID: ".$row['id']; $emailtxt .= "<br />";
$emailtxt .= "Lighting watts: ".$row['light']; $emailtxt .= "<br />";
$emailtxt .= "Kitchen watts: ".$row['kitchen']; $emailtxt .= "<br />";
$emailtxt .= "House watts: ".$row['house']; $emailtxt .= "<br />";
$emailtxt .= "Misc watts: ".$row['misc']; $emailtxt .= "<br />";
$emailtxt .= "Total Watt Usage: ".$row['gensize']; $emailtxt .= "<br />";

echo $emailtxt; // Echoing it to output as you did before
$emailtxt = str_replace('<br />',"\n",$emailtxt); // Replacing HTML link breaks with newlines, for text e-mails

$email = "sender@sender.com"; //senders e-mail adress
$recipient = $row['Email']; //recipient
$subject = "Off Grid Calculator Results Coming Soon!"; //subject


In regards to the chart, I've never used that script before. Have you managed to output a chart before incorporating it into this script? Also, check the manual for it to see if there's a debugging option. Usually you can set it so that it'll give you more intuitive error messages to see if you have a problem with syntax or omitted a particular line of code.

Hope that helps a bit.
4:24 am on Sept 4, 2010 (gmt 0)

New User

5+ Year Member

joined:Sept 3, 2010
posts: 16
votes: 0


brotherhood, you are the man. that worked like a charm. The pie chart in fact did output correctly before, and i used your same structure instead of the numbers and it works great too. Lastly, if i could ask one more thing, i dont have a clue as to arrange the data in a table-like manner. You can go ahead and take a look at it here:

[ultimafantasy.com...]

submit it if you like so you see everything in action. again, thank you very much.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members