PDA

View Full Version : Basic Php Mysql Help



dmc
08-15-2004, 08:42 PM
ok, so i dont really konw about how to make a web browswer work with mysql.. so hopefully someone can answer some basic questions for me...

so how do i update a variable throught php...

i have the line.. update countera set counter = counter +1; that generates an error of Parse error: parse error, unexpected T_STRING..
so how do i increment the variable .. i actually wanted it to be incremented each time the page is loaded. like a basic counter...

also my select statemnt of the variable.. is mysql_query(SELECT counter FROM countera) adn that produces the following results..

resource id#3 (no errors) how do i get the value of it to show up.

sorry im pretty new to it.. if anyone can point me to some good internet resources or recommend any books that would be apprectiated..

thanks alot.

Herbster
08-15-2004, 10:15 PM
Your variables need a dollar sign:
$counter = $counter +1;
Or check here:
http://www.php.net/manual/en/language.oper...s.increment.php (http://www.php.net/manual/en/language.operators.increment.php)


/* Performing SQL query */
$query = "SELECT * FROM my_table";
$result = mysql_query($query) or die("Query failed : " . mysql_error());

/* Printing results in HTML */
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
Complete example here:
http://www.php.net/manual/en/ref.mysql.php

dbmasters
08-16-2004, 08:27 AM
I read his post assuming "counter" was a database field, not a PHP variable, making his SQL syntax correct.

The resource error I makes me wonder if you are forgetting to make the connection before you make the query...

dmc
08-16-2004, 10:30 PM
this is my code.. minus some of the specific info..


<html>

<?php
mysql_connect('localhost');
mysql_select_db('count');

update countera set counter = counter +1;
$query = "SELECT counter FROM countera";

$counta = mysql_query($query);
echo("This page has been viewed $counta times");
?>
</html>

countera is a table.. with on field counter as the primary key.

and $counta is the php variable that gets the value that the query produces.. which comes back as resource id#3

Herbster
08-16-2004, 11:47 PM
Only for SELECT,SHOW,DESCRIBE or EXPLAIN statements, mysql_query() returns a new result identifier that you can pass to mysql_fetch_array() and other functions dealing with result tables.
http://www.php.net/manual/en/function.mysql-query.php

<?php
mysql_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysql_error());
mysql_select_db("mydb");

$result = mysql_query("SELECT id, name FROM mytable");

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
printf("ID: %s Name: %s", $row[0], $row[1]);
}

mysql_free_result($result);
?>
http://www.php.net/manual/en/function.mysql-fetch-array.php