PDA

View Full Version : basic PHP prob ??


vosgien
08-12-2004, 07:31 AM
Hi,
I am having a small problem with some very basic code.
I have a db that records everytime an item is clicked on, this work well, next I have implemented some basic code which extracts the relevant info from the db and displays in a browser, that also works well, the problem is I want to put that info into an email and send it to the site owner, but I can't seem to achieve this. The code :

<?php
//connect to db
include("connect.php");

//record date
$date = date("F jS Y");

//extract info
$result = mysql_query("SELECT * FROM $table WHERE numLook != 0");
$rowNum = mysql_num_rows($result);


while($row = mysql_fetch_assoc($result))
{
$item = $row['item'];
$numLook = $row['numLook'];
print "<p>$item\n$numLook\n</p>";

}
?>

If I replace the print command with an sendMail function, the email gets sent for each result, and if I send the mail from outside the brackets it only contains the last line of info from the db. I need to send the mail with all the info only once. I tried setting a variable( $i), incrementing with each iteration & when it equalled $numRows send the mail, but I keep getting the same results
I have feeling that I am forgetting something basic, so I am hoping that someone will be able to nudge me along a bit

Cheers

Vosgien

snapple
08-12-2004, 09:42 AM
Vosgien,


while($row = mysql_fetch_assoc($result))
{
$tmpStr = "";

$tmpStr = $tmpStr . $row["item"] . ",";

}

mysql_free_result($result);
print "varProductList=$tmpStr";


Now - i'm pretty bad at php, but i think you might need to replace 'item' (in the concatinating line) with the name of your row in the query string (but try both). This would form a commer seperated string of items, you could then just attach your varProductList variable.

I have just realised that your using mysql_fetch_assoc() - i used mysql_fetch_array when i had to do what you are doing.

Hope this helps.

Regards, snapple :)

vosgien
08-12-2004, 01:07 PM
Hi Snapple,
I will try that a bit later, but I can see the logic in it so it looks OK, thanks for responding.
using mysql_fetch_assoc() provides the same functionality asmysql_fetch_array($result, MYSQL_ASSOC) which is to ensure the return array is indexed by string - as I understand it, so I am not sure there is any difference, but I am still very much on my PHP learning curve to be certain of the accuracy of that

Will let you know how I get on

Cheers

Vosgien

vosgien
08-12-2004, 05:52 PM
Hi,
Your code, snapple, produced the same results as mine, so I thought again about using a for loop with the variable $i and came up with the following code that works exactly how I want it to

$result = mysql_query("SELECT * FROM $table WHERE numLook != 0");
$rowNum = mysql_num_rows($result);

for ($i=0; $i < $rowNum; $i++)
{
$row = mysql_fetch_array($result);

$tmpStr .= "\n".$row['item'];
$tmpStr .= " \n".$row['numLook'];
}

echo $tmpStr;
//or in my case I call a function that sends the email with the relevant info


Thanks for your help

Cheers

Vosgien

snapple
08-12-2004, 06:01 PM
Ah, good idea.

Told you i was crap at php ;)

Regards, snapple

freddycodes
08-12-2004, 06:39 PM
The difference between fetch_assoc and fetch_array are as follows:

fetch array returns to items per field one indexed by field name and one indexed by numerical index.

For instance


$result = mysql_query("select field1 from table WHERE ID = 1");
$row = mysql_fetch_array($result)
/*
$row contains
$row[0]
and
$row['field1']
which have the same value
*/

$row = mysql_fetch_assoc($result)
/*
$row contains
$row['field1']
*/
$row = mysql_fetch_array($result, MYSQL_ASSOC)
/*
$row contains
$row['field1']
*/

$row = mysql_fetch_row($result)
/*
$row contains
$row[0]
*/

Dark_Element
08-13-2004, 12:32 PM
You guys proberbly should concider replacing those "s with 's because:
- "s are mainly used while dealing with regular expressions
- "s can slow down load speed and consume unnessecary server resources
- "s may cause troubles which can otherwise be avoided with 's

don't need to listen to me but er... just thought to let you guys know

note: you may not notice the differences on a couple small tasks cos its only around (0.00 something per second differences between them) though when you run a huge loop and count the concumption you will be suprised ;)

freddycodes
08-13-2004, 03:05 PM
What the hell are you talking about?

vosgien
08-13-2004, 05:49 PM
DITTO - what the hell are you talking about dark_element !!!!!!!!!!

Dark_Element
08-14-2004, 01:09 AM
im talking about the double quotes and how inefficent they are in some cases when compared with single quotes...

sorry if it upsets you (its just a suggestion which you dont really even have to notice)

freddycodes
08-14-2004, 04:22 AM
Yeah well maybe you should be a little more clear next time.

Hopefully this will shed some light. single quotes are for literals, double quotes are for expressions.

Meaning

Things encapsulated in double quotes will be evaulated, while in single quotes they will be rendered at face value.

So naturally when addressing array elements single quotes would be faster unless of course there needs to be a expression in it. For instance


$array["foo$i"] I do believe will be faster than $array['foo'.$i]

Although I don't know the specs on that.

freddycodes
08-14-2004, 04:23 AM
Also if you notice my post uses only single quotes as I like to promote good skills. So maybe you should also be more particular in who and what you are addressing, and you will avoid comments like "What the hell are you talking about?"