homepage Welcome to WebmasterWorld Guest from 54.161.191.154
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderator: open

Databases Forum

    
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
magneto



 
Msg#: 4196119 posted 3:39 pm on Sep 3, 2010 (gmt 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(); ?>" />

 

brotherhood of LAN

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



 
Msg#: 4196119 posted 4:29 pm on Sep 3, 2010 (gmt 0)

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.

magneto



 
Msg#: 4196119 posted 5:03 pm on Sep 3, 2010 (gmt 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

brotherhood of LAN

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



 
Msg#: 4196119 posted 7:57 pm on Sep 3, 2010 (gmt 0)

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.

magneto



 
Msg#: 4196119 posted 4:24 am on Sep 4, 2010 (gmt 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved