2

Hi i am trying to get the List of columns from a specific table in mysql and then generate a form based on the columns but i keep getting an error and the page is just blank.

mysql_select_db($DB_Name) or die(mysql_error());
$query=mysql_query("SHOW COLUMNS FROM mytable") or die (mysql_error());
$j=0;

while($row=mysql_fetch_assoc($query)){
    if(substr($row,0,2)=="S_"){
        echo substr($row,2);?>:<input type="text" name="<?php echo $row;?>" maxlength="<?php echo getlength($j, "mytable");?>" /><?php ?><!--textbox code here --><?php
    }
    $j++;
}

getlength(); is a method i wrote to make it easier to get the length of a field for a column. It works accurately.

thanks

i think i didnt mention. Since the site is dynamic the name of the column isnt known. The point of this is to get the list of column names from a specific table and display it in an orderly manner.

3
  • using substr you can restrict the count of string values,not an array... Commented Sep 10, 2012 at 5:19
  • Who keeps decreasing my reputation? I did my research first and tried to be clear. Commented Sep 10, 2012 at 7:17
  • Sorry. Don't know about the reduction of reputation rates Commented Sep 10, 2012 at 7:31

4 Answers 4

2

mysql_fetch_assoc returns an associative array. In this case you very likely are getting an array what kind of looks like this:

[FIELD] => "Name of the field",
 [Type] => "varchar(32)",
 [Null] => YES,
 [Key] => PRI,
 [Default] => '',
 [Extra] => ''

So, if you want to check the field name for S_ then you need to change your substr line to

substr($row['FIELD'], 0, 2)
Sign up to request clarification or add additional context in comments.

Comments

2

When you call substr(), you're currently passing in $row, which is an array. You need to specify an index in the $row in order to return a particular element like this:

 $row['name']

Comments

1

You need to identify the column you wanted to trace. Here is the correct script below:

<?php
    if(substr($row['some_column'],0,2)=="S_"){
        echo substr($row['some_column'],2);

    }
?>

Hope that this help you.

1 Comment

not possible, the column will be added/removed/changed sometimes. The form generated is dynamic that means whichever column is in the table is used as a basis to generate the form and also for validating user input.
0

Replace

if(substr($row,0,2)=="S_"){
    echo substr($row,2);?>

with

 if(substr($row['FIELD'],0,2)=="S_"){
    echo substr($row['FIELD'],2);?>

1 Comment

i get undefined index instead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.