Jump to content


Photo

Newbie - trouble getting data from database


  • Please log in to reply
4 replies to this topic

#1 renojim

renojim
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 24 September 2006 - 06:40 PM

Not new to programming, but an infant in PHP. I'm using the following code to get a value from a mySql table. The table has seven columns, one row, field Ronum is mediumint with a value of 1 in it and is the primary key. The following code runs fine, but when I get to the echo row/ronum at the bottom, $row contains only "ARRAY", and $ronum is blank. Don't get it.:

<?php
$user="root";
$host="localhost";
$password="";
$database = "Vshop";
  $connection = mysql_connect($host,$user,$password)
    or die ("couldn't connect to server");
  $db = mysql_select_db($database,$connection)
    or die ("Couldn't select database");
{
  $query = "Select Ronum From ro";
}
$result = mysql_query($query)
    or die ("Couldn't execute query.");
$row = mysql_fetch_array($result,MYSQL_NUM);
extract ($row);
echo "$row";
echo "$ronum";
?>

  Any help is greatly appreciated.


#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 24 September 2006 - 06:45 PM

$row is an array of your data, with the field names as array elements.  extracting $row will create variables having the same name as your field names. do this loop to see what is actually being retrieved, and then you will see what variables are being extracted.

foreach ($row as $key => $val) {
  echo "key: $key  value: $val <br>";
}

the "key" is the variable names that extract should build.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 24 September 2006 - 06:47 PM

I havent used the extract() function before, but from what I just read, $row should echo Array.

However for the $ronum, should the key name be case sensitive? ie: $Ronum?
If you're not part of the solution, you're part of the precipitate

#4 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 24 September 2006 - 06:53 PM

Replace...
$row = mysql_fetch_array($result,MYSQL_NUM);
extract ($row);
echo "$row";
echo "$ronum";

With...

while( $data = mysql_fetch_array($result,MYSQL_NUM)){
echo $data[0];
}

#5 renojim

renojim
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 25 September 2006 - 04:06 AM

  Thanks all for getting me straightened out - great responses. I'm beginning to get a clue.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users