ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
-----------------------------
id | name | attribute | value
-----------------------------
01 | tori | callerid | 546878
02 | tori | callerid | 789562
------------------------------
I would like to get the attribute value printed, mysql sql queries is like:
$sql="select value from user where attribute='callerid' and name='tori'";
$query=mysql_query($sql);
$result=mysql_num_rows($query);
while(list($caller1, $caller2)=mysql_fetch_row($query)){
$mac1=$caller1;
$mac2=$caller2;
}
echo "$mac1";
echo "$mac2";
=================
From the above statement I am getting only one value? What I did wrong can anyone guide me? I am not a programmer?
I want to grab the individual Value so that I can edit individually the callerid value.
From the above statement I am getting only one value? What I did wrong can anyone guide me? I am not a programmer?
I want to grab the individual Value so that I can edit individually the callerid value.
you're doing the echo outside of the while loop, so you're only seeing the last assignment. do the echo inside the while loop & you'll see both values.
edited to add: also, do this query on a command line to see what it returns... it will only return one item, so :
list($caller1, $caller2)=mysql_fetch_row($query)
will only assign anything to $caller1, but will assign it each time through the loop.
Yes, Briank while doing echo inside the while loop I will get the both value on the same row.
How is it possible to get seperate value outside while loop?
$sql="select value from user where attribute='callerid' and name='tori'";
$query=mysql_query($sql);
$result=mysql_num_rows($query);
while(list($caller1, $caller2)=mysql_fetch_row($query)){
$mac1=$caller1;
$mac2=$caller2;
}
echo "$mac1";
echo "$mac2";
please try this one.. i haven't tested this yet since i don't have the mysql's
Code:
$sql="select value from user where attribute='callerid' and name='tori'";
$query=mysql_query($sql);
$mac1 = array();
$mac2 = array();
while (list($caller1, $caller2) = mysql_fetch_row($query)) {
array_push($mac1, $caller1);
array_push($mac2, $caller2);
}
echo implode($mac1, " ")."\n";
echo implode($mac2, " ")."\n";
Thanks, some what to the points. While with the above statement it only displays the result with echo command. But can't display the value with following php code.
Thanks, some what to the points. While with the above statement it only displays the result with echo command. But can't display the value with following php code.
as I stated earlier, it's not correct to assign the mysql fetch to an array of two elements, being that it only returns one.
konsolebox's example was pretty close. Here's a [imho] better version:
PHP Code:
$sql="select value from user where attribute='callerid' and name='tori'"; $query=mysql_query($sql); $mac1 = array(); while (list($caller1) = mysql_fetch_row($query)) { $mac1[] = $caller1 }
then see my above example on how to access that stuff later.
note that "array_push($arr,$thing)" is essentially the same as "$arr[] = $thing"
One more note: it's arguable that you could make your select box inside of the while loop. Just put the select open before the loop, the options in the loop then your select close after the loop. That said, it's probably more readable to do what you're asking, i.e. store the mysql return in an array then use the array to create the selection box.
$sql="select value from user where attribute='callerid' and name='tori'";
$query=mysql_query($sql);
$mac1 = array();
while (list($caller1) = mysql_fetch_row($query)) {
$mac1[] = $caller1
}
yes this is truly a better version. perhaps if you want to show the output in html you can append this:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.