0

I am running into a very strange issue.

I have a function which prints the columns name of a given database table from MySql.

function getColoumn() {

    //replace it with your host normally it could be localhost
    $hostname='localhost';
    //mysql user name
    $username='admin_datauser';
    //mysql user password
    $password='iCoq4KrJM8';
    //connect to the mysql server
    $ss = mysql_pconnect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR);
    //select a database on the mysql server
    //please change as you like the database name
    mysql_select_db('admin_data');

    //SHOW COLUMNS FROM TABLENAME
    $query=mysql_query('SHOW COLUMNS FROM user') or die(mysql_error());


    foreach($fields as $key=>$field){
    echo '"'.$field->Field.'", '; // print each field name

    }
}   

The output looks like below-

"id", "first_name", "last_name", "email", "address", "country", "city", "state", "phone_number", "fax", "image", "datecreated", "dateupdated", "Company", "token", 

I have another array variable where the current static values are like below-

$searchArray = array("first_name","last_name","email","address","country","city","state","phone_number","fax","image");

I need to use the function's variable in this $searchArray. I tried calling the variable in the arrya but it doesn't work in any way.

Any help please??

fiona
  • 55
  • 5
  • 4
    Please dont use [the `mysql_` database extension](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), it is deprecated (gone for ever in PHP7) Specially if you are just learning PHP, spend your energies learning the `PDO` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly May 12 '16 at 15:04
  • And after a `mysql_query()` which runs the query and creates a result set. You have to read rows from that resultset with something like `$fields = mysql_fetch_object();` Normally written in a `while () {}` loop – RiggsFolly May 12 '16 at 15:05
  • This function does not look like it should work. `$fields` is undefined unless I'm missing something. – Don't Panic May 12 '16 at 15:12
  • The main problem appears to be this: you need your function to return an array, but instead it prints a string and returns null. Just replace the `echo` with something that puts the field name into an array, and when you've gotten them all, return the array. – Don't Panic May 12 '16 at 15:16
  • 1
    Is this ALL your relevant code, or have you missed bits out. **I do hate having to do the 20 questions dance before getting to the real question** – RiggsFolly May 12 '16 at 15:23

1 Answers1

1

first of all you shouldn't use mysql_ functions anymore since they are deprecated and prone to SQL injections. rather use PHP's own PDO or mysqli libraries.

then it's much easier to directly add the elements to your array in the foreach loop:

$searchArray = array();

foreach($fields as $key=>$field){
    $searchArray[]= $field->Field;
}

note: the []= operator in PHP acts similar to the += operator. but instead of adding to a number value, it adds elements to an array.

low_rents
  • 4,481
  • 3
  • 27
  • 55
  • 2
    And again, where does `$fields` come from?? You missed that OP is not reading the result set – RiggsFolly May 12 '16 at 15:10
  • @RiggsFolly true, but OP says that "output looks like below" - which can't be true if s/he is not reading the resultset. – low_rents May 12 '16 at 15:12
  • @RiggsFolly so I am guessing it's just a copy-paste error from OP - where should the output magically come from otherwise? – low_rents May 12 '16 at 15:19