Page 1 of 1
get a variable from a mysql table?
Posted: Tue Sep 21, 2010 11:34 pm
by garyed
I'm trying to figure out how to retrieve a number from a mysql database at a specific column & row.
If I run mysql in a terminal & do:
select new from goods where type="chair";
then I'll get | new |
|-----------------|
| 50 |
------------------
If I do in php:
Code: Select all
$result =mysql_query('select new from goods where type="chair"');
it doesn't work.
I'm trying to get that number 50 so i can use it as a variable to add or subtract with.
Any help appreciated.
Re: get a variable from a mysql table?
Posted: Wed Sep 22, 2010 12:19 am
by requinix
Re: get a variable from a mysql table?
Posted: Wed Sep 22, 2010 8:16 am
by garyed
Thanks for taking the time to help,
I don't have all the code in front of me now but I'm not having a problem connecting to the database or table.
I've got checkers that tell me whether or not I've connected properly & also test $result like so.
Code: Select all
if (!$result)
{
die("didn't work");
}
If I change my query to something that won't give $result a true result from the table I'll get the error
so $result must have the answer but I can't figure how to get it out.
Re: get a variable from a mysql table?
Posted: Wed Sep 22, 2010 9:58 am
by mikosiko
what are you saying is answering more or less the first 2 questions from tasairis... what about the 3rd one?
Try to read any rows from the result? (is a link here)
Re: get a variable from a mysql table?
Posted: Wed Sep 22, 2010 4:17 pm
by garyed
I hate to sound stupid, I read it but I just don't get it.
I don't understand the mysql_fetch_array function.
I'm trying to learn this stuff on my own & I'm not doing too good yet.
I was hoping someone could show me in one line what i needed to do but evidently that ain't happening.
My plan is to design a program in php that I expect to take about a year to complete so the extra time it takes until I learn how to get the answer to my original question won't make much difference anyhow. At least If I ever figure it out I'll understand it.
Re: get a variable from a mysql table?
Posted: Wed Sep 22, 2010 4:43 pm
by mikosiko
garyed wrote:I hate to sound stupid, I read it but I just don't get it.
I don't understand the mysql_fetch_array function.
No... you don't sound stupid... you are learning as everyone else... in a different level everyone here does... so no need to feel bad.
here is a little and simple example for you to analyze and see how you can adapt to what you are trying to do.
Code: Select all
<?php
// Example
// Here we define how we are going to display errors
error_reporting(E_ALL);
ini_set("display_errors", 1);
$db_hostname = "thehost"; //usually "localhost is the default"
$db_username = "youusername"; //your user name
$db_pass = "youpassword"; //the password for your user
$db_name = "thedatabase"; //the name of the database
// We try to open a connection to our DB here.
$link = mysql_connect($db_hostname,$db_username,$db_pass) or die ('Db Connection Error: ' . mysql_error());
// We select the DB to work with
mysql_select_db($db_name, $link) or die('DB Selection Error :' . mysql_error());
// Here we define our query (tbId & tbName are only field examples)
$query ="select tbId,tbName from ourtabla";
// Here the query is executed and results are returned in a recordset
$rs = mysql_query($query, $link) or die("Query Fail : " . mysql_error());
// Here we use a while loop to retieve the rows from the recordset
// and do whatever we want with the values
while ($row = mysql_fetch_assoc($rs))
{
echo "Id = " . $row['tbId'] . "<br />";
echo "Name = " . $row['tbName'] . "<br />";
}
// We close the DB link (even when is not neccesary, because it is closed
// automatically when the php script finish
mysql_close($link);
?>
Re: get a variable from a mysql table?
Posted: Wed Sep 22, 2010 7:50 pm
by garyed
Wow thanks mikosiko,
That was exactly what i was looking for.
Your explanations were perfect too.