hosker Posted December 30, 2011 Share Posted December 30, 2011 I am trying to extract a value that is stored in a variable from my database. It will not work with how I have the code written. If I replace .$tournament with what I want it to find, it will give me the correct result. ANy suggestions? Below is my code: $result = mysql_query("SELECT id FROM `2012_tournaments` WHERE tournament = .$tournament"); while($row = mysql_fetch_array($result)) { echo $row['id']; }; Quote Link to comment https://forums.phpfreaks.com/topic/254057-php-variable-help-within-mysql/ Share on other sites More sharing options...
trq Posted December 30, 2011 Share Posted December 30, 2011 What is that full stop doing there? Quote Link to comment https://forums.phpfreaks.com/topic/254057-php-variable-help-within-mysql/#findComment-1302409 Share on other sites More sharing options...
hosker Posted December 30, 2011 Author Share Posted December 30, 2011 nothing, even if I remove it, I do not get the result I am looking for. Quote Link to comment https://forums.phpfreaks.com/topic/254057-php-variable-help-within-mysql/#findComment-1302410 Share on other sites More sharing options...
trq Posted December 30, 2011 Share Posted December 30, 2011 I assume the tournament field is a text type then. String need to be quoted in sql. $result = mysql_query("SELECT id FROM 2012_tournaments WHERE tournament = '$tournament'"); Quote Link to comment https://forums.phpfreaks.com/topic/254057-php-variable-help-within-mysql/#findComment-1302412 Share on other sites More sharing options...
hosker Posted December 30, 2011 Author Share Posted December 30, 2011 I get this: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\Hosting\6027795\html\demo\submit.php on line 86 the actual value I am looking for is an int. Quote Link to comment https://forums.phpfreaks.com/topic/254057-php-variable-help-within-mysql/#findComment-1302415 Share on other sites More sharing options...
trq Posted December 30, 2011 Share Posted December 30, 2011 This means that your query is failing and your failing to check it succeeds before passing it to mysql_fetch_assoc(). What does mysql_error() display? Quote Link to comment https://forums.phpfreaks.com/topic/254057-php-variable-help-within-mysql/#findComment-1302418 Share on other sites More sharing options...
hosker Posted December 30, 2011 Author Share Posted December 30, 2011 mysql_error() retured this: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''2012_tournaments' WHERE tournament = ''' at line 1 Server version: 5.0.91 Quote Link to comment https://forums.phpfreaks.com/topic/254057-php-variable-help-within-mysql/#findComment-1302422 Share on other sites More sharing options...
trq Posted December 30, 2011 Share Posted December 30, 2011 Post you current code. Quote Link to comment https://forums.phpfreaks.com/topic/254057-php-variable-help-within-mysql/#findComment-1302424 Share on other sites More sharing options...
hosker Posted December 30, 2011 Author Share Posted December 30, 2011 The code below works. $result = mysql_query("SELECT id FROM `2012_tournaments` WHERE tournament = '$tournament'"); while($row = mysql_fetch_array($result)) { echo $row['id']; }; After looking through the remaining code, I realized that I was not storing the variable $tournament before I was calling the function, thus the error. It works now. Thanks for walking me through the steps to re-examine my code. Quote Link to comment https://forums.phpfreaks.com/topic/254057-php-variable-help-within-mysql/#findComment-1302428 Share on other sites More sharing options...
trq Posted December 30, 2011 Share Posted December 30, 2011 Your still not checking the query has succeeded before using it's results. This is a terrible habit to get into. Your code should be... if ($result = mysql_query("SELECT id FROM 2012_tournaments WHERE tournament = '$tournament'")) { if (mysql_num_rows($result)) { while($row = mysql_fetch_array($result)) { echo $row['id']; } } } Quote Link to comment https://forums.phpfreaks.com/topic/254057-php-variable-help-within-mysql/#findComment-1302431 Share on other sites More sharing options...
hosker Posted December 30, 2011 Author Share Posted December 30, 2011 again, thanks for the tips. I also cannot get the following code to work: $sql = "SELECT * FROM weekly_picks WHERE tournament = '$tournament' AND usr = '$usr'"; $result = mysql_query($sql); echo mysql_num_rows($result); mysql_error(); if ($num > 0) mysql_query("UPDATE weekly_picks SET player = '$golfer' WHERE tournament = '$tournament' AND user = '$usr'"); else mysql_query("INSERT INTO weekly_picks (t_id, tournament, user, player, backup, timestamp) VALUES ('$t_id', '$tournament', '$usr', '$golfer', '$backup', '$time')",$link) or die('Error, insert query failed'); I have a form, and when it is submitted I would like to check to see if the $tournament variable already exists in the table along with the userid. IF it does, I would like to update the golfer field with the new $golfer. If it does not, insert a new row. Any suggestions? I get this response: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in D:\Hosting\6027795\html\demo\submit.php on line 125 Quote Link to comment https://forums.phpfreaks.com/topic/254057-php-variable-help-within-mysql/#findComment-1302434 Share on other sites More sharing options...
trq Posted December 30, 2011 Share Posted December 30, 2011 Did you read my previous reply? Quote Link to comment https://forums.phpfreaks.com/topic/254057-php-variable-help-within-mysql/#findComment-1302435 Share on other sites More sharing options...
hosker Posted December 30, 2011 Author Share Posted December 30, 2011 yes I did, sorry to no have commented, I had that post up prior to me reading it. I put that code in and it worked and I got the result I was looking for. Quote Link to comment https://forums.phpfreaks.com/topic/254057-php-variable-help-within-mysql/#findComment-1302437 Share on other sites More sharing options...
hosker Posted December 30, 2011 Author Share Posted December 30, 2011 well, I just thought I would let you know that from your earlier help, I adapted that code to fit the current issue I was trying to resolve and the following code is doing exactly what I want it to if ($result2 = mysql_query("SELECT * FROM weekly_picks WHERE tournament = '$tournament' AND user = '$usr'")) { if (mysql_num_rows($result2)) { while($row2 = mysql_fetch_array($result2)) { mysql_query("UPDATE weekly_picks SET player = '$golfer', backup = '$backup', timestamp = '$time' WHERE tournament = '$tournament' AND user = '$usr'"); } } else { mysql_query("INSERT INTO weekly_picks (t_id, tournament, user, player, backup, timestamp) VALUES ('$t_id', '$tournament', '$usr', '$golfer', '$backup', '$time')",$link) or die('Error, insert query failed'); }} Quote Link to comment https://forums.phpfreaks.com/topic/254057-php-variable-help-within-mysql/#findComment-1302443 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.