/    Sign up×
Community /Pin to ProfileBookmark

Can you spot the mistake?

OK, my code just isn’t working. There isn’t even an error message given – just nothing happens. I’ve connected to my DB (MySQL) and then I have the following code:

[code=php]$query = “SELECT * FROM `music_pop` WHERE `name` = ‘$name’ LIMIT 1”;
$result = mysql_query($query, $mysql_link);

while($row = mysql_fetch_row($mysql_result))
{
print_r($row);
if ($row)
{
$hits = $row[‘1’]++;
$sql = “UPDATE `music_pop` SET `hits` = ‘1’, `dls` = ‘” . $dls . “‘ WHERE `name` = ‘” . $name . “‘ AND `hits` = ‘” . $hits . “‘ AND `dls` = ‘” . $row[‘2’] . “‘”;
mysql_query($sql);
}
else
{
$sql = “INSERT INTO `music_pop` ( name, hits, dls ) VALUES ( ‘” . $name . “‘, ‘1’, ‘0’ )”;
mysql_query($sql);
print_r($row);
}
}[/code]

Can anyone see what’s wrong?

Thanks & Regards, Jonnie

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@solavarJul 20.2004 — Try...

[code=php]

$hits = $row[1];

// instead of

$hits = $row['1'];

/* REASON
You've used mysql_fetch_row() which will use numeric fields rather than strings, so '1' will be treated as 'string' and will not return any value.
*/

[/code]
Copy linkTweet thisAlerts:
@JavaHead_JonnieauthorJul 21.2004 — Thank you so much! ?

That wasn't my only problem though; I had my variables in the WHERE part when they should have been in the SET part of the query. Also, $row[1]++; wasn't working so I changed it to $row[1] + 1; and it worked fine... Does ++ (Increment by 1) even work in PHP? I'm getting confused now I'm learning C++ :p

Anyway, thanks again!


[B]EDIT:[/B] Grrrrrrr. The UPDATE query works, but the INSERT one doesn't. I think something might be wrong with the if statement but I don't know what...

This is my code:
[code=php] if ($row)
{
$hits = $row[1] + 1;
$sql = "UPDATE music_pop SET hits = '" . $hits . "', dls = '" . $row[2] . "' WHERE name = '" . $name . "'";
mysql_query($sql);
}
else
{
$sql2 = "INSERT INTO music_pop ( name, hits, dls ) VALUES ( '" . $name . "', '1', '0' )";
mysql_query($sql2);
}[/code]
Copy linkTweet thisAlerts:
@crh3675Jul 21.2004 — Here's your problem:

[code=php]
$result = mysql_query($query, $mysql_link);

while($row = mysql_fetch_row($mysql_result))
[/code]


should be

[code=php]
$result = mysql_query($query, $mysql_link);

while($row = mysql_fetch_row($result))
[/code]
Copy linkTweet thisAlerts:
@JavaHead_JonnieauthorJul 22.2004 — OK, I've done that but it still doesn't seem to want to do the INSERT query...
Copy linkTweet thisAlerts:
@crh3675Jul 22.2004 — I don't understand why you are saying:

if($row)

What are you tryin to do?
Copy linkTweet thisAlerts:
@JavaHead_JonnieauthorJul 22.2004 — Basically, I'm trying to check that, when the page loads, a row (In MySQL) exists, if it doesn't, create it.
Copy linkTweet thisAlerts:
@crh3675Jul 22.2004 — Your code is incorrect then:

[code=php]
$query = "SELECT * FROM music_pop WHERE name = '$name' LIMIT 1";
$result = mysql_query($query, $mysql_link);

if(mysql_num_rows($result)>0){
$hits = $row['1']++;
$sql = "UPDATE music_pop SET hits = '1', dls = '" . $dls . "' WHERE name = '" . $name . "' AND hits = '" . $hits . "' AND dls = '" . $row['2'] . "'";
mysql_query($sql);
}else{
$sql = "INSERT INTO music_pop ( name, hits, dls ) VALUES ( '" . $name . "', '1', '0' )";
mysql_query($sql);
}
[/code]
×

Success!

Help @JavaHead_Jonnie spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...