Solved

Trouble populating a selectlist with values from a single record

Posted on 2013-12-02
4
239 Views
Last Modified: 2013-12-02
Hi,
I'm not sure if its obvious but I'm struggling with below. I'm querying the db to populate the select list looking to get both phone numbers from  a single record. My resulting selectlist only contains just one phone number instead of the leadPhone and leadOtherPhone for that record.

What am I missing and I'll go bang my head on a wall when its pointed out:
<select name="dialnumber" id="dialnumber">
<?php
$query = "SELECT leadPhone, leadOtherPhone FROM leads WHERE user_id = '$current_id' AND id = '$thelead'";
$result = mysql_query($query) or die(mysql_error());
		echo("<option value=\"\">Choose a Number</option>");
		while($row = mysql_fetch_row($result)) {
		echo("<option value=\"$row[0]\">$row[0]</option>");
		}
?>

Open in new window


So if the record contained a number in each column... I'm only getting the first one populating my selectlist.
0
Comment
Question by:tjyoung
  • 3
4 Comments
 
LVL 42

Accepted Solution

by:
Chris Stanyon earned 500 total points
Comment Utility
You can't loop through using a while loop because there's only 1 record!!

Try this:

$row = mysql_fetch_row($result);
printf("<option value='%s'>%s</option>", $row[0], $row[0] );
printf("<option value='%s'>%s</option>", $row[1], $row[1] );

Open in new window

0
 
LVL 42

Expert Comment

by:Chris Stanyon
Comment Utility
It may make your code easier to read if you fetch an object rather than a row. You can then refer to the columns by name...

$row = mysql_fetch_object($result);
printf("<option value='%s'>%s</option>", $row->leadPhone, $row->leadPhone );
printf("<option value='%s'>%s</option>", $row->leadOtherPhone, $row->leadOtherPhone );

Open in new window

FYI - The mysql library is deprecated so sooner rather than later you should be switching to either the PDO or mySQLi libraries.

Ray Paseur, an Expert here on EE, has written a great article about it:

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
 
LVL 1

Author Closing Comment

by:tjyoung
Comment Utility
Shoot me... it would be a mercy killing.
Thanks for the swat upside the head Chris.
0
 
LVL 42

Expert Comment

by:Chris Stanyon
Comment Utility
:)

We all need it from time to time...
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
spacing 5 24
Problem with SqlConnection 5 108
html input clean up 3 28
PHP loop not working 4 29
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now