LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-13-2008, 10:54 AM   #1
ohcarol
Member
 
Registered: Dec 2004
Location: Nepal
Posts: 86

Rep: Reputation: 15
php mysql query help needed


Hello all,

My sql table "users" structure looks like this

-----------------------------
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.
 
Old 03-13-2008, 12:55 PM   #2
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
Quote:
Originally Posted by ohcarol View Post
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.

Last edited by BrianK; 03-13-2008 at 12:57 PM.
 
Old 03-13-2008, 10:51 PM   #3
ohcarol
Member
 
Registered: Dec 2004
Location: Nepal
Posts: 86

Original Poster
Rep: Reputation: 15
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?
 
Old 03-13-2008, 11:28 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Store the results in an array
 
Old 03-14-2008, 03:03 AM   #5
ohcarol
Member
 
Registered: Dec 2004
Location: Nepal
Posts: 86

Original Poster
Rep: Reputation: 15
I did this to store result in array didn't work.

$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";
 
Old 03-14-2008, 04:24 AM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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";
 
Old 03-14-2008, 11:12 AM   #7
ohcarol
Member
 
Registered: Dec 2004
Location: Nepal
Posts: 86

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by konsolebox View Post
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.

echo <<<EOM
<tr>
<td>MAC Use
<select name="macuse" size="1">
<option value="N">N</option>
<option selected value="Y">Y</option>
</select>
<input type="text" name="mac" size="12" value="$mac1" ><input type="submit" value="Update" name="Update"></td>
EOM;
 
Old 03-14-2008, 01:12 PM   #8
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
Quote:
Originally Posted by ohcarol View Post
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.

echo <<<EOM
<tr>
<td>MAC Use
<select name="macuse" size="1">
<option value="N">N</option>
<option selected value="Y">Y</option>
</select>
<input type="text" name="mac" size="12" value="$mac1" ><input type="submit" value="Update" name="Update"></td>
EOM;
here's an example:
PHP Code:
$arr = array();
for (
$i=0$i<10; ++$i) {
  
$arr[] = $i;
}

for (
$j=0$j<count($arr); ++$j) {
  echo 
"arr[$j] = $arr[$j]<br>\n";

which outputs:
Code:
arr[0] = 0
arr[1] = 1
arr[2] = 2
arr[3] = 3
arr[4] = 4
arr[5] = 5
arr[6] = 6
arr[7] = 7
arr[8] = 8
arr[9] = 9
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.

Last edited by BrianK; 03-14-2008 at 01:33 PM.
 
Old 03-14-2008, 11:00 PM   #9
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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
}
yes this is truly a better version. perhaps if you want to show the output in html you can append this:
Code:
foreach ($mac1 as $m) {
?>
	<tr><td>
		MAC Use
		<select name="macuse" size="1">
			<option value="N">N</option>
			<option selected value="Y">Y</option>
		</select>
		<input type="text" name="mac" size="12" value="<?php echo $m; ?>" />
		<input type="submit" value="Update" name="Update" />
	</td></tr>
<?php
}
please check for typos just in case.. i haven't tested this yet.

Last edited by konsolebox; 03-14-2008 at 11:01 PM. Reason: </tr></td>
 
Old 03-16-2008, 03:28 AM   #10
ohcarol
Member
 
Registered: Dec 2004
Location: Nepal
Posts: 86

Original Poster
Rep: Reputation: 15
Thanks all for your suggestion. I will try it out.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
I'm trying to get PHP to filter results from a MySQL query!!! Tom "Techno" Earl Programming 4 07-18-2011 06:49 AM
query mysql using PHP for Persian mohtasham1983 Programming 2 07-08-2007 09:56 PM
php/,mysql problem: can't query JJX Linux - General 4 01-06-2005 05:10 PM
php: Why Can't I Query Mysql DB?? flamesrock Programming 7 11-16-2004 12:36 AM
PHP MySQL Query Question vi0lat0r Programming 1 07-15-2004 05:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration