Jump to content

Code works only sometimes


mcfmullen

Recommended Posts

Here's an odd one I can't figure out for the life of me..

 

I have the following code that DOES work:

<?php 
$sql3 = "SELECT animalMethods.nameMethod FROM animalMethods WHERE nameAnimal = '{$_GET['animal']}'";
$methods = mysql_query($sql3);
if (trim($row['nameMethod']) != '') 
while($row3 = mysql_fetch_array($methods)){
echo $row3['nameMethod'];
        echo '<br>';
}
else 
  echo 'None';
?>

 

This code DOES work as well:

<?php 
$sql3 = "SELECT cropMethods.nameMethod FROM cropMethods WHERE nameCrop = '{$_GET['crop']}'";
$methods = mysql_query($sql3);
if (trim($row['nameMethod']) != '') 
while($row3 = mysql_fetch_array($methods)){
echo $row3['nameMethod'];
        echo '<br>';
}
else 
  echo 'None';
?>

 

This code also DOES work:

<?php 
$sql3 = "SELECT treeMethods.nameMethod FROM treeMethods WHERE nameTree = '{$_GET['tree']}'";
$methods = mysql_query($sql3);
if (trim($row['nameMethod']) != '') 
while($row3 = mysql_fetch_array($methods)){
echo $row3['nameMethod'];
        echo '<br>';
}
else 
  echo 'None';
?>

 

However, this code does NOT work:

<?php 
$sql3 = "SELECT vehicleMethods.nameMethod FROM vehicleMethods WHERE nameVehicle = '{$_GET['vehicle']}'";
$methods = mysql_query($sql3);
if (trim($row['nameMethod']) != '') 
while($row3 = mysql_fetch_array($methods)){
echo $row3['nameMethod'];
        echo '<br>';
}
else 
  echo 'None';
?>

 

Where the above three working codes display the methods used to acquire animals, crops, or trees, (or "None" if there is no method) the fourth code always displays "None" rather the the method required to obtain the vehicle (even if there is a method).

 

Table and field names are correct. I even changed the table and field names and get the same result. Running the query in MySQL gets me the correct results. It is in the PHP where I am having a problem but the code is exactly the same as the working samples! Each of these samples exists in a separate php page (animalspec.php, cropspec.php, treespec.php, vehiclespec.php) and those pages are virtually identical. The GET calls does work in vehiclespec.php because I do get the page result of the right vehicle...

 

I even changed WHERE nameVehicle = '{$_GET['vehicle']}'"; to WHERE nameVehicle = 'Tractor'"; and still get "None" as a result and I know that not to be true.

 

Am I overlooking something??

Link to comment
Share on other sites

Your code has no error checking or error reporting logic in it to get it to tell you why it is failing, so it will also be a little hard for anyone on a forum to tell you why either.

 

For debugging purposes, echo mysql_error(); on the next line after your mysql_query() line to get php/mysql to tell you why the query is failing.

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.