PHP - MySQL problems

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

User avatar
pio
New php-forum User
New php-forum User
Posts: 12
Joined: Sat Nov 29, 2003 9:58 am
Location: Oslo, Norway
Contact:

PHP - MySQL problems

Postby pio » Sat Nov 29, 2003 10:15 am

SORRY FOR POSTING IN THE WRONG FORUM :oops:

I'm getting a very annoying problem trying to make a simple counter for my website. The counter keeps a different count for each section of the site using a MySQL table. Here's the code for the counter:

4. function teller($disp) {
5.
6. if($disp == '') {
7. $resultat = mysql_query("SELECT * FROM teller WHERE side='hoved'",$db);
8.
9. $gammelt_tall = mysql_result($resultat,0,'tall');
10.
11. $nytt_tall = $gammelt_tall+1;
12.
13. echo $nytt_tall;
14.
15. mysql_query("UPDATE teller SET tall=".$nytt_tall."
WHERE side='hoved'",$db);
16.
17. } else {
18.
19. $resultat = mysql_query("SELECT * FROM teller WHERE side='".$disp."'",$db);
20.
21. $gammelt_tall = mysql_result($resultat,0,'tall');
22.
23. $nytt_tall = $gammelt_tall+1;
24.
25. echo $nytt_tall;
26.
27. mysql_query("UPDATE teller SET tall=".$nytt_tall." WHERE side='".$disp."'",$db);
28.
29. }
30. }

Explanation: $disp is the identifier for the section of the site (embedded in the url) even though the variables are in norwegian, they should have obvious functions.. (by the way, 'tall' means number, 'teller' means counter and 'side' means page)

Here's the structure and data of the MySQL table used:

mysql_query("CREATE TABLE teller (id tinyint(4) NOT NULL auto_increment, side varchar(25) NOT NULL default '', tall mediumint(9) NOT NULL default '0', PRIMARY KEY (id), UNIQUE KEY id (id))",$db);
mysql_query("INSERT INTO teller VALUES (1,'hoved',51)",$db);
mysql_query("INSERT INTO teller VALUES (2,'karinger',43)",$db);
mysql_query("INSERT INTO teller VALUES (3,'skriv',15)",$db);
mysql_query("INSERT INTO teller VALUES (4,'nyheter',38)",$db);
mysql_query("INSERT INTO teller VALUES (5,'polls',34)",$db);
mysql_query("INSERT INTO teller VALUES (6,'ringen',50)",$db);
mysql_query("INSERT INTO teller VALUES (7,'bilder',32)",$db);

Here are the error messages I get:

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/petronas/www/fagga/funksjoner.php on line 7

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/petronas/www/fagga/funksjoner.php on line 9

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/petronas/www/fagga/funksjoner.php on line 15

As far as I can see from the code, it should work perfectly fine, so why do I get these errors? any suggestions?

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Re: PHP - MySQL problems

Postby swirlee » Sat Nov 29, 2003 2:10 pm

pio wrote:Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/petronas/www/fagga/funksjoner.php on line 7

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/petronas/www/fagga/funksjoner.php on line 9

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/petronas/www/fagga/funksjoner.php on line 15


Well, the first error means that $db isn't a valid link-identifier, as returned by mysql_connect()[. Since you didn't post your connection code, I can't tell what might've gone wrong. But make sure you're connected to the database successfully.

User avatar
pio
New php-forum User
New php-forum User
Posts: 12
Joined: Sat Nov 29, 2003 9:58 am
Location: Oslo, Norway
Contact:

connection code:

Postby pio » Sat Nov 29, 2003 2:30 pm

$db = mysql_connect('mysql.domeneshop.no','fagga','removed for safety');

same connection code I use for all connections on the site, and works perfectly well except in the counter-script!?

I know I should know this, but would it help to make $db global?

YUP, THAT SOLVED IT!, thanks for bringing my thoughts to $db :D


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 4 guests

cron