Getting specific information from the database

I am trying to figure out how to do a water medication calculator for pigs with php and mysql and forms. I have a database set up with the amount of water a piglet will drink at ?weeks of age.

id age water
1 1 .23
2 2 .35
3 3 .38
4 4 .52

etc up to 21 weeks of age.
I also have another table with the different drugs listed and their dosage rates.

Next I have a form with 3 inputs.
How many pigs? text input
What age are the pigs? Dropdown
How many days you’d like to run the treatment. dropdown (for cost work ups)
submit button.

What I’d like it to do is for me to tell it the age of piglet I’m after, then it tells me the amount of water that age piglet should be drinking. This is done in excel with the vlookup function. Seems everything I’m finding online just gives info about tables.

Can someone point me towards information? thanks so much.

PS, sorry, it doesn’t look like the table is coming out very well.

This along with lots of other possibilities is what I’ve tried so far. It doesn’t work.

[php]

$result = “select * from waterUsage where age=” . $_POST[‘age’] . “’”;

echo $result[‘age’]."
".$result[‘water’];

[/php]

Other than this not being a mysql question. What is happening? Have you confirmed you can get anything from the worksheet? Are you trying to move it to a database?

I’m not following you on the not a mysql question. But yes, I have been able to connect to the database and get the info off the table.

thanks for looking at it at least.

Is your information in excel or done in a data table?

What is the structure of your table/s?

Generally, you would do

SELECT column1, column2column3 FROM table WHERE some_column = value;

It’s correct in the mysql database. the forum’s table button didn’t work so well for me so I tried to html code it which didn’t work so well either I guess.

I’m trying to mimic what’s already been done in excel. This will work I’m sure, and I’m pretty sure everyone’s over thinking this. I’m trying to tell the database to pull the row of info I ask for in a form. I don’t want to get a whole table full of stuff, only specific info about that row.

a similar thing might be to have a database with first and last names. A drop down form lists the last names. when you select the last name, you’re able to look up the first name. I’m pretty sure what’s going on is I’m missing some sort of punctuation or something in it or not laying it out properly.

Are you using mysqli or PDO?

mysqli_version
[php]
$con=mysqli_connect(“database_location”,“username”,“password”,“dbname”);

(I assume you have this as you said you can connect.)

$stmt = $con -> prepare(“SELECT water FROM waterUsage WHERE age=?”);

mysqli_stmt_bind_param($stmt, “s”, $$_POST[‘age’]);

mysqli_stmt_execute($stmt);

mysqli_stmt_bind_result($stmt, $water);

mysqli_stmt_fetch($stmt);

echo “Water amount is: $water”;

mysqli_stmt_close($stmt);

mysqli_close($con);

[/php]

I think the PDO version is better but, it is likely you are using mysqli_ . Change as needed, but this is the general way a mysqli prepared statement is executed… (You should be using prepared statements)

Sponsor our Newsletter | Privacy Policy | Terms of Service