Jump to content

php variable help within mysql


hosker

Recommended Posts

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'];
  };

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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'];
    }
  }
}

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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');
}}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.