Page 1 of 1
assign specific varibles to fetch assoc
Posted: Mon Jul 19, 2010 8:25 pm
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
Re: assign specific varibles to fetch assoc
Posted: Mon Jul 19, 2010 8:46 pm
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";}
Re: assign specific varibles to fetch assoc
Posted: Mon Jul 19, 2010 8:48 pm
by chrisatnetronix
could you explain how the above works
Re: assign specific varibles to fetch assoc
Posted: Mon Jul 19, 2010 8:50 pm
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:
Re: assign specific varibles to fetch assoc
Posted: Mon Jul 19, 2010 9:24 pm
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
Re: assign specific varibles to fetch assoc
Posted: Mon Jul 19, 2010 9:28 pm
by Alex-V
Only way you can store that data is in array and Gargoyle posted the solution
Re: assign specific varibles to fetch assoc
Posted: Mon Jul 19, 2010 9:29 pm
by chrisatnetronix
ok now i am confused...explain
Re: assign specific varibles to fetch assoc
Posted: Mon Jul 19, 2010 9:37 pm
by Alex-V
I can try
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
Re: assign specific varibles to fetch assoc
Posted: Mon Jul 19, 2010 10:06 pm
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";}
Re: assign specific varibles to fetch assoc
Posted: Mon Jul 19, 2010 10:53 pm
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());
}
}
}