Forum Moderators: coopster

Message Too Old, No Replies

MySQL and PHP array code problems ! HELP ;)

trying to display MySQL array data in PHP

         

magneto

1:18 am on Sep 3, 2010 (gmt 0)

10+ Year Member



Hello guys, Im a newbie at MySQL and PHP and started working on a little project. I used snippets of information here and there to create my code, so it might not be very well written but it does the job, except there's one little bug. I want to be able to insert my MySQL data into an array for mailtxt and also for a google api (bottom code of php where it shows numbers). I would like to be able to insert the results from my form.

<?php
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("database_name") or die(mysql_error());

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

$row = mysql_fetch_array( $result );

$mailtxt .=" Name: *********this is the part i cant figure out*******. how should i call the array so it fetches the correct result. If im confusing you, scroll all the way to the bottom\n";
$mailtxt .=" Phone: $row\n";
$mailtxt .=" Email: $_GET[Email]\n";

echo "Name: ".$row['Name']; echo "<br />";
echo "Phone Number: ".$row['Phone']; echo "<br />";
echo "Email: ".$row['Email']; echo "<br />";


//$email = $row['Email']; //senders e-mail adress
//$subject = "Subject for reviever"; //subject

//mail($recipient, $subject)
$email = "myemail@abc.com"; //senders e-mail adress
$recipient = $row['Email']; //recipient
$subject = "Results"; //subject

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


<?php
require ("gChart.php");

$piChart = new gPieChart();
$piChart->set3D(true);

//In the code below, i would like to insert my values from the data into array(112,315,66,40) instead of actual numbers. Thanks guys

$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(); ?>" />

magneto

1:23 am on Sep 3, 2010 (gmt 0)

10+ Year Member



if you guys wanna see it in action, go here:

[ultimafantasy.com...]

you might want to include an email address so you see what its being sent into your email. thanks again. ohh and as you can see, the graph is always the same, thats why i want to be able to select the data from database

rocknbil

5:22 pm on Sep 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here,
$row = mysql_fetch_array( $result );

$row contains TWO arrays, an associative array and a list (indexed) array. That is, very likely, $row[0] and $row['id'] are identical values (test it. :-) )

You got it right here,

echo "Email: ".$row['Email']; echo "<br />";

but missed it here. Change

$mailtxt .=" Name: ...";
$mailtxt .=" Phone: $row\n";

to

$mailtxt .=" Name: " . $row['name'] . "<br>\n";
$mailtxt .=" Phone: " . $row['phone'] . "<br>\n";

Now let's optimize just a little. :-) This selects the last record, the one with the highest ID, right?

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

There's no need for a nested select, or use of max. This will give you an identical result.

$query = "select * from data table order by id desc limit 1";
$result = mysql_query($query) or die(mysql_error());

If you want HTML emails, just build a header.



$email = "myemail@abc.com"; //senders e-mail adress
$recipient = $row['Email']; //recipient
$subject = "Results"; //subject

$headers = "From: $email\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($recipient, $subject, $mailtxt,$headers);

magneto

4:06 am on Sep 4, 2010 (gmt 0)

10+ Year Member



thanks rocknbill, however, the code for the email (once it is sent) only gives me this:

Hardy Solar Off Grid Report
========================================
Lighting Section
Name: <br>
Phone: <br>


That is exactly how i see it. i couldnt get it to show the name or phone or more data for that matter. any more suggestions?

rocknbil

6:00 pm on Sep 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well magneto, name and Name are not the same thing, your field names are obviously Name and Phone, not name and phone. :-) My mistake, adjust accordingly.

magneto

8:55 am on Sep 5, 2010 (gmt 0)

10+ Year Member



haha..didnt catch that. didnt mean to be rude either its just that i kept trying for the longest time before and i thought it wouldnt work but thanks a lot...works good now.

rocknbil

4:47 pm on Sep 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not at all, I type on the fly and my time is limited, I often miss Really Important Stuff. Like capitalization.