Jump to content


Photo

Some help with PHP <=> MySQL, please?


  • Please log in to reply
7 replies to this topic

#1 Stevie

Stevie
  • New Members
  • Pip
  • Newbie
  • 4 posts
  • LocationDenmark

Posted 08 July 2006 - 08:16 PM

Hi there, i just started up working with PHP and MySQL a little time ago, and i built up a site, which doesn't work quite as i want it to.

Well, i wrote this code:

$mysql = mysql_connect("*****", "*****", "*****") or die ("No connection!"); //connect to mysql
mysql_select_db("*****", $mysql) or die ("No database establishment!"); //choosing a database

$fieldnavn = $produktnummer; //$produktnummer is defined by the user in a form, a little earlier. It's a number from 1-10
$tablenavn = "produkter";

$vaelgfoernavn = mysql_query("SELECT $fieldnavn FROM $tablenavn") or die(mysql_error());
while($row = mysql_fetch_assoc($vaelgfornavn)) {
  echo $row['$fieldnavn']."<br />";
}

mysql_query("DELETE `$produktnummer` FROM `$tablenavn`"); //Here i was trying to delete whats in the field previously, which didn't work.

mysql_query("INSERT INTO `$tablenavn` ( `$produktnummer` ) VALUES ('$produktnavn')"); //put in the new stuff

}


Yeah, i know my code looks crazy, sometimes using ` sometimes using " sometimes using nothing at all, anyway, when i launch this, it doesn't delete the stuff, AND i recieve the error:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /var/www/sjuk.dk/public_html/mysql/tilfoej.php on line 40


And line 40:

line 39|$vaelgfoernavn = mysql_query("SELECT $fieldnavn FROM $tablenavn") or die(mysql_error());
line 40| while($row = mysql_fetch_assoc($vaelgfornavn)) {
line 41|  echo $row['$fieldnavn']."<b r>";
line 42| }

(the <b r> is cause the forum sees this as new line)

Okay, i really hope anyone here understand me, and won't get pissed on me, just because im a noob. Hope you guys can help me, this is driving me nuts!  ???

-Steve.

#2 Kurt

Kurt
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts
  • LocationNew York

Posted 08 July 2006 - 08:21 PM

The only thing that I can think of that might be wrong with your select query is that you are calling a non existant table or column. And your delete query is wrong. It should be:

mysql_query("DELETE FROM `$tablenavn` WHERE $fieldnavn=$produktnummer");

#3 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 08 July 2006 - 08:21 PM

In your first code posting, $vaelgfornavn is being used in place of $vaelgfoernavn.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#4 Kurt

Kurt
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts
  • LocationNew York

Posted 08 July 2006 - 08:31 PM

In your first code posting, $vaelgfornavn is being used in place of $vaelgfoernavn.

Ha, that was probably the problem. I didn't even notice that :S

Good eye ;)

#5 Stevie

Stevie
  • New Members
  • Pip
  • Newbie
  • 4 posts
  • LocationDenmark

Posted 08 July 2006 - 10:02 PM

Hey!
Thanks alot!
Im really glad you guys helped me  :D

-Steve

#6 Stevie

Stevie
  • New Members
  • Pip
  • Newbie
  • 4 posts
  • LocationDenmark

Posted 08 July 2006 - 10:28 PM

Oookay... I tried changing the wrong stuff.

Now i have something like this:

$mysql = mysql_connect("*****", "*****", "*****") or die ("No connection!"); //connect to mysql
mysql_select_db("*****", $mysql) or die ("No database establishment!"); //Choosing a database

$fieldnavn = "1";
$tablenavn = "produkter";


$vaelgfoernavn = mysql_query("SELECT $fieldnavn FROM $tablenavn") or die(mysql_error());
while($row = mysql_fetch_assoc($vaelgfoernavn)) {
  echo "MySQL data:".$row['$fieldnavn']."<b r>";
  }

(Still, the space in <b r> is only cause the forum recognises this)

When i open phpMyAdmin, i see that the field called "1" of the table "produkter" contains for instance, "Acme Speargun".

It gives me the output:

MySQL data:


Nothing? That can't be good?  ???

Anyway, im really glad you guys helped me before, and i really hope you will agian!  ;D

Thanks,
Steve.


#7 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 10 July 2006 - 12:58 AM

Why are you quoting your variable?  Just use $row[$fieldnavn].
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#8 Stevie

Stevie
  • New Members
  • Pip
  • Newbie
  • 4 posts
  • LocationDenmark

Posted 10 July 2006 - 10:42 AM

Thanks for your reply.
I tried it, and it works now, thanks!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users