0

I have this little tid-bit of my code, which is eventually sent to a MySQL database. The rest of the code is sound, but this code likes to give me empty data some of the time. Is there any way to prevent this from happening?

Edit: Here's the whole chunk

//random Species
$sp_one = mt_rand(1,10);    
$one_species = "Water Leaper";

//random Genetics

if($one_species == "Water Leaper")
{
    $one_gene = mt_rand(1,5);

    if($one_gene < 3)
    {
        $one_genetics = "1";
    }

    else if($one_gene < 5)
    {
        $one_genetics = "2";
    }

    else
    {
        $one_genetics = "3";
    }
}

//random Gender
$one_sex_num = mt_rand(1,2);

if($one_sex_num == 1)
{
    $one_gender = "Female";
}

if($one_sex_num == 2)
{
    $one_gender = "Male";
}

//Entering it
$sql="INSERT INTO creatures (species, sex, location, genetics)
VALUES('{$one_species}','{$one_gender}', 's1','{$one_genetics}')";

mysqli_query($con,$sql);
13
  • Where is the empty data situated ? Commented Aug 3, 2013 at 14:31
  • 1
    Please specify what you mean by give me empty data some of the time, especially, what are the values of $one_gene and $one_genetics after the code gave you empty data. Commented Aug 3, 2013 at 14:32
  • In a MySQL table column. It pops up randomly and causes errors on my site that I have to go and fix manually. Commented Aug 3, 2013 at 14:32
  • 1
    That problem is unrelated to this code. Check the code that inserts the data into the database for errors, and post here if necessary. Commented Aug 3, 2013 at 14:32
  • 2
    why are doing a rand between 1 a 5 if you only want a number between 1 and 3? Commented Aug 3, 2013 at 14:34

2 Answers 2

1

First, your if clauses seem a bit redundant. More concise code:

if ($one_gene <3)      { $one_genetics = "1"; }
elseif ($one_gene <5)  { $one_genetics = "2"; }
else                   { $one_genetics = "3"; }

This should always return a value - if everything else fails, "3".

Maybe better even:

$one_genetics = ($one_gene + 1) / 2; // integer division
Sign up to request clarification or add additional context in comments.

1 Comment

Great - you have discovered that the error is elsewhere! Please accept the answer and post a new question with the relevant code.
0

I don't know what you are doing exacly but maby you can take a look at this:

<?php

    $animals = array();

    $animals[] = array('dog', 78, array('Komondor','Old English Sheepdog'));    
    $animals[] = array('Drosophila', 8, array('Vestigal','Ebony')); 

    $number_animals = count($animals) - 1;
    $list_size = 30;

    for($q = 1; $q <= $list_size; $q++){

        $rand = rand(0, $number_animals);
        $animal = $animals[$rand];

        $number_species = count($animal[2]) - 1;

        $rand = rand(0, $number_species);
        $randsex = rand(0, 1);

        $species = $animal[2][$rand];
        $sex = ($randsex ? 'male' : 'female');
        $genes = $animal[1];

        echo "$q: $species - $sex - $genes <br>";
    }

?>

See a live demo at: here

It pics random animals with specs if you want you can modify to your own wishes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.