Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 3rd, 2003, 03:36 PM
Registered User
 
Join Date: Aug 2003
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to shabboleth
Default Assigning Values to Variables

I am trying to create a list box of the contents of a field in a MySQL database using the code below:

<td width="70%"><select name="QuizType">
<?php

    $link_id = mysql_connect("localhost","phpuser","phppass");
    $recordset = mysql_query("SELECT * FROM jpquiz.QuizTypes ORDER BY TypeIndex");

    While($query_data = mysql_fetch_row($recordset))
        {
        $QuizType = $query_data["QuizType"];
        echo "<option value=\"";
        echo $QuizType;
        echo "\">";
        echo $QuizType;
        echo "</option>\n";
        }
        echo "</select>\n";

The form displays OK and does not return any errors. The list box has four rows as it should but they are all blank. After messing around with it for a little while it seems that the value is not being assigned correctly to $QuizType.

Can anyone tell me what I am doing wrong?

Thanks in advance,

Glen


 
Old September 3rd, 2003, 04:22 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, you're using mysql_fetch_row() which returns a numerically indexed array, but you're accessing a string index of that result array. You need to use mysql_fetch_array() or mysql_fetch_assoc().

mysql_fetch_array() returns both numeric and string indexes, or you can pass a second param that specifies one or the other.

Read the manual for these functions:
 http://www.php.net/mysql_fetch_row
 http://www.php.net/mysql_fetch_assoc
 http://www.php.net/mysql_fetch_array



Take care,

Nik
http://www.bigaction.org/
 
Old September 4th, 2003, 07:17 AM
Registered User
 
Join Date: Aug 2003
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to shabboleth
Default

That fixed it, Thank you.

Glen






Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem assigning my Array values to rows - Please Edoloto Excel VBA 1 August 18th, 2008 04:13 AM
Automate assigning the values from one table’s tex pkaptein1 Access 1 May 14th, 2005 06:57 AM
Assigning values to variables in Stored Procedures AlanAtMars SQL Server 2000 3 May 4th, 2005 12:37 AM
Assigning Session Variables morpheus Classic ASP Basics 2 November 21st, 2003 01:55 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.