assign specific varibles to fetch assoc

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
chrisatnetronix
Forum Newbie
Posts: 16
Joined: Mon Jul 19, 2010 8:13 pm

assign specific varibles to fetch assoc

Post by chrisatnetronix »

here is what I need:

I need to assign a specific variable to a mysql_fetch_assoc:

example:

lets say the fetch assoc brings me this list

number1

number2

number3


I need to assign a variable to each: WITHOUT printing it

like

$var = number1;

$var2 = number2;

how do I do this?

here is the current code and result:

$result = mysql_query("SELECT number FROM smart_acc WHERE user_info_id = 'null' LIMIT 3")
or die(mysql_error());

if ($result && mysql_num_rows($result)) {

while ($row = mysql_fetch_assoc($result)) {

while(list($var, $val) = each($row)) {
print "$val<br />";
}

print "<br />";

}
}


This prints:

412100031065

412100031066

412100031067

******************

I need to assign each number a variable
Gargoyle
Forum Contributor
Posts: 130
Joined: Wed Jul 14, 2010 12:25 am

Re: assign specific varibles to fetch assoc

Post by Gargoyle »

Code: Select all

$a = array();
while ($row = mysql_fetch_assoc($result))
{$a[] = $row;}

foreach($a as $k=>$v)
{echo 'set '.$k.'|'.$v['number1'].'|'.$v['number2'].'|'.$v['number3']."\n";}

Last edited by Gargoyle on Mon Jul 19, 2010 8:50 pm, edited 1 time in total.
chrisatnetronix
Forum Newbie
Posts: 16
Joined: Mon Jul 19, 2010 8:13 pm

Re: assign specific varibles to fetch assoc

Post by chrisatnetronix »

could you explain how the above works
chrisatnetronix
Forum Newbie
Posts: 16
Joined: Mon Jul 19, 2010 8:13 pm

Re: assign specific varibles to fetch assoc

Post by chrisatnetronix »

Gargoyle wrote:

Code: Select all

$a = array();
while ($row = mysql_fetch_assoc($result))
{$a[] = $row;}

foreach($a as $k=>$v)
{echo 'set '.$k.'|'.echo $v['number1'].'|'.$v['number2'].'|'.$v['number3']."\n";}


yes but your are specifying number 1 , number2, number3, if the fetch assoc is grabbing the data based on the limit and min(id) how will it work with the above code:
chrisatnetronix
Forum Newbie
Posts: 16
Joined: Mon Jul 19, 2010 8:13 pm

Re: assign specific varibles to fetch assoc

Post by chrisatnetronix »

remember each number like:

412100031065

412100031066

412100031067

has to have a different variable foreach:

like $var1 = 412100031065

$var2 = 412100031066

$var3 = 412100031067

so if the limit in the in the select area is set to 5 and the assoc list 5 numbers I need a variable created for each automatically so I can use it to store a user_info_id foreach then mail the numbers
User avatar
Alex-V
Forum Newbie
Posts: 17
Joined: Mon Jul 19, 2010 3:53 pm

Re: assign specific varibles to fetch assoc

Post by Alex-V »

Only way you can store that data is in array and Gargoyle posted the solution
chrisatnetronix
Forum Newbie
Posts: 16
Joined: Mon Jul 19, 2010 8:13 pm

Re: assign specific varibles to fetch assoc

Post by chrisatnetronix »

ok now i am confused...explain
User avatar
Alex-V
Forum Newbie
Posts: 17
Joined: Mon Jul 19, 2010 3:53 pm

Re: assign specific varibles to fetch assoc

Post by Alex-V »

I can try :D

When you add this code instead of your while loop:

Code: Select all

$a = array();
while ($row = mysql_fetch_assoc($result))
{$a[] = $row;}
You will get an array that looks like this:

0 => array("number1" => 412100031065, "number2" => 412100031066, "number3" => 412100031066),
1 => array("number1" => some_number1, "number2" => some_number2, "number3" => some_number3),
etc.

Then using this code:

Code: Select all

foreach($a as $k=>$v)
{echo 'set '.$k.'|'.$v['number1'].'|'.$v['number2'].'|'.$v['number3']."\n";}
You will get an output that looks like:

set 0|412100031065|412100031066|412100031066
set 1|some_number1|some_number2|some_number3
Gargoyle
Forum Contributor
Posts: 130
Joined: Wed Jul 14, 2010 12:25 am

Re: assign specific varibles to fetch assoc

Post by Gargoyle »

I didn't realize the OP is just trying to read the single column "number" from the table but wants different variables for each ROW.

consider this code instead:

Code: Select all

$a = array();
while ($row = mysql_fetch_assoc($result))
{$a[] = $row;}

foreach($a as $k=>$v)
{echo 'row '.$k.'|'.echo $v['number']."\n";}
chrisatnetronix
Forum Newbie
Posts: 16
Joined: Mon Jul 19, 2010 8:13 pm

Re: assign specific varibles to fetch assoc

Post by chrisatnetronix »

I have got it where it loads the user_info_id numbers in the db based on the list it gets from the fetch assoc, now I Need to know how to set a variable that will be (echoable) and show all numbers in the fetch assoc in a comma separated line:

like

412100031065, 412100031066, 412100031067

this way I can mail the numbers to someone

BUT MAKE NOTE, I need the numbers to be echoable from a variable like $show

Here is my code so far:

// SC Connect for user_info_id update
$conn123 = mysql_connect($dbschost, $dbscuser, $dbscpass) or die ('Error connecting to mysql');
mysql_select_db($dbscname);


$result555 = mysql_query("SELECT number FROM smart_acc WHERE user_info_id = 'null' LIMIT $quantityvm")
or die(mysql_error());




if ($result555 && mysql_num_rows($result555)) {


while ($row555 = mysql_fetch_assoc($result555)) {


while(list($var, $val) = each($row555)) {


// SC Connect for user_info_id update
$connuidsc = mysql_connect($dbschost, $dbscuser, $dbscpass) or die ('Error connecting to mysql');
mysql_select_db($dbscname);
// this assigns the user_info_id to all values the fetch array provided
$resultuidsc = mysql_query("UPDATE smart_acc SET user_info_id = '$uii' WHERE number = '$val'")
or die(mysql_error());


}
}
}
Post Reply