Jump to content

[SOLVED] Fetching variables


timmah1

Recommended Posts

I want to get each line in a different variable.

This what I tried, and I'm getting no results

 

This is for a database that the admin can add custom fields to forms

<?php
$result=mysql_query('SELECT * FROM `main`');
for ($n=1;$row=mysql_fetch_array($result);$n++) {
$var='variable'.$n;
$var=$row['columnname']; //replace columnname with real column name
} 
echo $va[$n]r;
?>

How can I do this?

 

Thanks in advance

Link to comment
Share on other sites

So essentially this

<?php
require("config.php");

$result=mysql_query('SELECT * FROM `main`');
$array = array();
while($row = mysql_fetch_array($result))
{
echo $array[] = $row['column'];
}
?>

 

I'm still not getting any results, and right now there are 15 fields in the database, I don't understand why?

Link to comment
Share on other sites

What? What are you trying to do? If you want each result in an array, do this:

 

require("config.php");

$result=mysql_query('SELECT * FROM `main`');
$array = array();
while($row = mysql_fetch_array($result))
{
$array[] = $row['column'];
}
print_r($array);

Link to comment
Share on other sites

ok, I'm trying to make a contact database for a friend.

 

He wants to store information , kinda like a rolodex.

 

He wants to be able to add custom fields to the database, not just your standard name, address, phone, etc..

 

I need to be able to pull all of the custom field names, as well the data out to view it, and possibly edit it.

 

So, right now, in the database, these are the fields he has in there now

group1

first

middle

last

title

company

birthday

 

Since I will not know the field names, how can I call of the fields, with their data, from the database to view them?

 

I hope that makes sense

Link to comment
Share on other sites

So essentially this

<?php
require("config.php");

$result=mysql_query('SELECT * FROM `main`');
$array = array();
while($row = mysql_fetch_array($result))
{
echo $array[] = $row['column'];
}
?>

 

I'm still not getting any results, and right now there are 15 fields in the database, I don't understand why?

 

Your echo statement makes no sense.  You're trying to output an assignment.  Try:

$array = array();
$result = mysql_query("SELECT * FROM `main`");
$count = 1;

while($row = mysql_fetch_assoc($result))
{
   $array[] = $row['column'];
   echo "Row $count value: {$array[$count - 1]}<br />\n";
   $count++;
}

Link to comment
Share on other sites

ok, I'm trying to make a contact database for a friend.

 

He wants to store information , kinda like a rolodex.

 

He wants to be able to add custom fields to the database, not just your standard name, address, phone, etc..

 

I need to be able to pull all of the custom field names, as well the data out to view it, and possibly edit it.

 

So, right now, in the database, these are the fields he has in there now

group1

first

middle

last

title

company

birthday

 

Since I will not know the field names, how can I call of the fields, with their data, from the database to view them?

 

I hope that makes sense

 

Will there ever be a finalized database?  Or will he be adding fields indefinitely?

Link to comment
Share on other sites

I would be more inclined to simply have 3 fields in the database. An id, a key and a value, then you would simply do something like.

 

<?php

  if ($result = mysql_query("SELECT k, v FROM tbl")) {
    if (mysql_num_rows($result)) {
      while ($row = mysql_fetch_assoc($result) {
        echo "{$row['k']} = {$row['v']}\n";
      }
    }
  }

Link to comment
Share on other sites

Nightslyr,doing this

<?php
require("config.php");

$array = array();
$result = mysql_query("SELECT * FROM `main`");
$count = 1;

while($row = mysql_fetch_assoc($result))
{
   $array[] = $row['column'];
   echo "Row $count value: {$array[$count - 1]}<br />\n";
   $count++;
}
?>

 

Returns this

Row 1 value: 

 

So nothing is being shown

 

 

Link to comment
Share on other sites

Nightslyr,doing this

<?php
require("config.php");

$array = array();
$result = mysql_query("SELECT * FROM `main`");
$count = 1;

while($row = mysql_fetch_assoc($result))
{
   $array[] = $row['column'];
   echo "Row $count value: {$array[$count - 1]}<br />\n";
   $count++;
}
?>

 

Returns this

Row 1 value: 

 

So nothing is being shown

 

 

 

Because you have no field called column!

Link to comment
Share on other sites

I still think your easiest solution is to go with my 3 field approuch. That way your user can add as many different key / value pairs without ever needing to alter the tables structure.

 

Anyway, if you want field names try....

 

<?php
require("config.php");

$array = array();
$result = mysql_query("SELECT * FROM `main`");
$count = 1;

while($row = mysql_fetch_assoc($result))
{
  foreach ($row as $k => $v) 
    echo "{$k} = {$v} ";
  }
  echo "\n";
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.