We hope you enjoy your visit.

You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.


Join our community!


If you're already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
Question about ALTER MySQL Command
Topic Started: Mar 15 2005, 01:02 PM (380 Views)
Dennis
Member
[ *  *  *  *  *  *  * ]
Well, I'm setting up a script that will redirect you to an external link, and I want it to increment a hit. Kind of like a TopSites type of thing.

At the moment, I've got a few questions:

1. Can I send a header() function after a MySQL Query, and if not, what can I use in it's place?

2. What is the command for altering a row? More specifically, adding +1 to the value of total hits.

3. Is there a way to record a person's IP address, and would I have to store it in a seperate Table?

4. What is the easiest way to redirect to another page with PHP?

Thanks!
Offline Profile Quote Post Goto Top
 
Ben
Member Avatar
Quantum-locked when observed.

The Balrog
March 15, 2005 12:02 PM
1. Can I send a header() function after a MySQL Query, and if not, what can I use in it's place?

I'm not quite sure on this one, I think the answer is yes. But if it doesn't work, try putting the header before the query and seeing if it still executes.

Quote:
 
2. What is the command for altering a row? More specifically, adding +1 to the value of total hits.

Use UPDATE:

Code:
 
<?php
//Get the amount of hits presently in the row:
$query = mysql_query("SELECT * FROM your_table");
$querydata = mysql_fetch_array($query);
$hits = $querydata["total_hits"];

//Now update the table by adding 1 to the total hits:
$hits += $hits;
mysql_query("UPDATE your_table SET total_hits = '$hits'");
?>


Quote:
 
3. Is there a way to record a person's IP address, and would I have to store it in a seperate Table?

Yes, a person's IP address is stored in the $_SERVER["REMOTE_ADDR"] variable. So just use a MySQL insert command like so:

Code:
 
$ip = $_SERVER["REMOTE_ADDR"];
mysql_query("INSERT INTO your_other_table (ip) VALUES('$ip')");


Quote:
 
4. What is the easiest way to redirect to another page with PHP?

Once again, the only way I know how to do it would be with a header() statement. :/
Offline Profile Quote Post Goto Top
 
Gornakle
Member
[ *  *  *  *  * ]
Tachyon
March 15, 2005 08:21 PM
The Balrog
March 15, 2005 12:02 PM
1. Can I send a header() function after a MySQL Query, and if not, what can I use in it's place?

I'm not quite sure on this one, I think the answer is yes. But if it doesn't work, try putting the header before the query and seeing if it still executes.


As long as nothing was outputted to the screen (echo/print etc), you can use header(). You could also use output buffering to store all output temporarily, then use header(), and then flush the output to the screen.

Quote:
 

Quote:
 
2. What is the command for altering a row? More specifically, adding +1 to the value of total hits.

Use UPDATE:

Code:
 
<?php
//Get the amount of hits presently in the row:
$query = mysql_query("SELECT * FROM your_table");
$querydata = mysql_fetch_array($query);
$hits = $querydata["total_hits"];

//Now update the table by adding 1 to the total hits:
$hits += $hits;
mysql_query("UPDATE your_table SET total_hits = '$hits'");
?>


Just a note.. but a faster way would be the following. Saves a query. :P
Code:
 
mysql_query("UPDATE your_table SET total_hits = total_hits+1");
Offline Profile Quote Post Goto Top
 
Dennis
Member
[ *  *  *  *  *  *  * ]
Cool! Thanks guys.
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Technology Chat · Next Topic »
Add Reply