1

I am creating a browser statistics script in PHP.
When PHP finds a match in a table how do I retrieve the row and pull all three fields of the table into separate variables.

My table rows are (browser, version, amount)

so I want to select everything from the TB were

browser = $browser and version = $version.
But when this happens how do I add one to the amount field and resubmit the row. Without altering anything else. Below is the code I have.

$browser_name= "MSIE";
$version = "7";
$query = ("SELECT * FROM $TB 
  WERE browser = '$browser_name' AND version = '$version'");
$result = mysql_query($query);
$count = mysql_num_rows($result);
if($count == 1)
{
  $mysql = ("SELECT * FROM $TB 
    WERE browser = '$browser_name' AND version = '$version'");

  //code to add 1 to amount would be here

}
else
{
  //if browser is not detected insert fresh row.
  $mysql = "INSERT INTO $TB (browser, version, amount)
    VALUES ('$browser_name', '$version', 1)";
}

I have been looking around for answers for ages but they just tell me how to select information out of a DB as a whole.

6 Answers 6

1
update table set amount_field = amount_field + 1 where browser = '$browser_name' and version = '$version'

I don't understand why you make twice the same select.

Sign up to request clarification or add additional context in comments.

Comments

1
$browser_name= "MSIE";
$version = "7";
$query = "SELECT * FROM $TB WHERE browser = '$browser_name' AND version = '$version'";
$result = mysql_query($query);

if($data = mysql_fetch_assoc($result))
{
    print_r($data);
    //use UPDATE from other answers if you need one here.
}
else
{
    //if browser is not detected insert fresh row.
    mysql_query("INSERT INTO $TB (browser, version, amount)
     VALUES ('$browser_name', '$version', 1)");
}

Beware of SQL injections!

Comments

0
SELECT * FROM $TB WHERE browser

you have a mistake it must be WHERE instead of WERE.

Comments

0

You update query will be like this

UPDATE $TB SET ammount = 1 WHERE browser = $browser and version = $version

Comments

0

There are hundreds of issues with your query like sanitization etc. I suggest you read a good mySQL book to improve the queries. But to answer your question:

//code to add 1 to ammount would be here
mysql_query("update $TB set amount = amount + 1 where browser = '".mysql_real_escape_string($browser_name)."' and version = '".mysql_real_escape_string($version)."'");

Comments

0

First of all, you should connect to db using

$db = new mysqli('host','user','pass','dbname');

Then, your query isn't right. You have a sql syntax error.

SELECT * FROM table WHERE column = value

So, without prepare, your code should look like:

$tb          = 'table_name'; // to avoid sql injections use addslashes() to your input
$browserName = addslashes('browser_name'); // like this
$version     = '7';

$db     = new mysqli('host','user','pass','dbname');
$result = $db->query("SELECT * FROM $tb WERE browser = '$browserName' AND version = '$version'");

// Than, you can fetch it.
while ($result->fetch as $row) {
    echo $row['yourColumn']; // will display value of your row. 
}

$db->query("INSERT INTO $tb (browser, version) VALUES ('$browser_name', '$version')";

}");

And, that's all. You can count your records using mysql count() method. Your code will work. I advice you, at first go to php.net and check the doc. There are many examples.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.