Stevie Posted July 8, 2006 Share Posted July 8, 2006 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:[quote]$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 }[/quote]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:[quote]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[/quote]And line 40:[quote]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| }[/quote][sup](the <b r> is cause the forum sees this as new line)[/sup]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. Quote Link to comment https://forums.phpfreaks.com/topic/14046-some-help-with-php-mysql-please/ Share on other sites More sharing options...
Kurt Posted July 8, 2006 Share Posted July 8, 2006 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"); Quote Link to comment https://forums.phpfreaks.com/topic/14046-some-help-with-php-mysql-please/#findComment-54901 Share on other sites More sharing options...
effigy Posted July 8, 2006 Share Posted July 8, 2006 In your first code posting, $vaelgfornavn is being used in place of $vaelgfoernavn. Quote Link to comment https://forums.phpfreaks.com/topic/14046-some-help-with-php-mysql-please/#findComment-54902 Share on other sites More sharing options...
Kurt Posted July 8, 2006 Share Posted July 8, 2006 [quote author=effigy link=topic=99896.msg393696#msg393696 date=1152390118]In your first code posting, $vaelgfornavn is being used in place of $vaelgfoernavn.[/quote]Ha, that was probably the problem. I didn't even notice that :SGood eye ;) Quote Link to comment https://forums.phpfreaks.com/topic/14046-some-help-with-php-mysql-please/#findComment-54908 Share on other sites More sharing options...
Stevie Posted July 8, 2006 Author Share Posted July 8, 2006 Hey!Thanks alot! Im really glad you guys helped me :D-Steve Quote Link to comment https://forums.phpfreaks.com/topic/14046-some-help-with-php-mysql-please/#findComment-54946 Share on other sites More sharing options...
Stevie Posted July 8, 2006 Author Share Posted July 8, 2006 Oookay... I tried changing the wrong stuff.Now i have something like this:[quote]$mysql = mysql_connect("*****", "*****", "*****") or die ("No connection!"); //connect to mysqlmysql_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>"; }[/quote][sup](Still, the space in <b r> is only cause the forum recognises this)[/sup]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:[quote]MySQL data:[/quote]Nothing? That can't be good? ???Anyway, im really glad you guys helped me before, and i really hope you will agian! ;DThanks,Steve. Quote Link to comment https://forums.phpfreaks.com/topic/14046-some-help-with-php-mysql-please/#findComment-54959 Share on other sites More sharing options...
fenway Posted July 10, 2006 Share Posted July 10, 2006 Why are you quoting your variable? Just use $row[$fieldnavn]. Quote Link to comment https://forums.phpfreaks.com/topic/14046-some-help-with-php-mysql-please/#findComment-55336 Share on other sites More sharing options...
Stevie Posted July 10, 2006 Author Share Posted July 10, 2006 Thanks for your reply.I tried it, and it works now, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/14046-some-help-with-php-mysql-please/#findComment-55455 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.