Jump to content

Archived

This topic is now archived and is closed to further replies.

renojim

Newbie - trouble getting data from database

Recommended Posts

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.

Share this post


Link to post
Share on other sites
$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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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];
}

Share this post


Link to post
Share on other sites
  Thanks all for getting me straightened out - great responses. I'm beginning to get a clue.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.