get a variable from a mysql table?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
garyed
Forum Newbie
Posts: 13
Joined: Fri Feb 20, 2009 6:45 pm

get a variable from a mysql table?

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: get a variable from a mysql table?

Post by requinix »

What's the rest of the code? Did you connect first? Change to the right database? Try to read any rows from the result?
garyed
Forum Newbie
Posts: 13
Joined: Fri Feb 20, 2009 6:45 pm

Re: get a variable from a mysql table?

Post 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.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: get a variable from a mysql table?

Post 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)
garyed
Forum Newbie
Posts: 13
Joined: Fri Feb 20, 2009 6:45 pm

Re: get a variable from a mysql table?

Post by garyed »

mikosiko wrote: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)
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.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: get a variable from a mysql table?

Post 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);
?>
garyed
Forum Newbie
Posts: 13
Joined: Fri Feb 20, 2009 6:45 pm

Re: get a variable from a mysql table?

Post by garyed »

Wow thanks mikosiko,
That was exactly what i was looking for.
Your explanations were perfect too.
Post Reply