Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have this code below and I am trying to get results from query as array containing array key to be table field name and value to be the result from the field. So far I have this:

$query='select      
en_product_name,de_product_name,fr_product_name,ru_product_name    
from products where id="'.$pid.'"';    
$result=mysql_query($query) or die('Mysql Error:'.mysql_error().'<br /> Query:'.$query);    
$num_rows=mysql_num_rows($result);    
$row = mysql_fetch_array($result);    
$columns = mysql_num_fields($result);    
$fields =array();    
for($i = 0; $i < $columns; $i++) {     
echo $fields = mysql_field_name($result,$i).'<br />';
}    

this $fields returnes only the field name.. How can I have the result as: Array ( [en_product_name] => New en product name, [de_product_name] => New de product name) and etc.. Thank you for any help and suggestions

share|improve this question
    
try $fields[key] = $value; –  krishna Feb 12 at 10:26
    
mysql_* has been deprecated. Please use PDO (php.net/manual/en/book.pdo.php) or mysqli_* instead. Otherwise there are security problems. I would recommend PDO. –  Patrick Geyer Feb 12 at 10:27

4 Answers 4

up vote 1 down vote accepted

Try with this

$query='select en_product_name,de_product_name,fr_product_name,ru_product_name    
    from products where id="'.$pid.'"';    
$result=mysql_query($query) or die('Mysql Error:'.mysql_error().'<br /> Query:'.$query);    
$num_rows=mysql_num_rows($result);    
$row = mysql_fetch_array($result);    
$columns = mysql_num_fields($result);    
$fields =array();    
for($i = 0; $i < $columns; $i++) {     
    $field = mysql_field_name($result,$i);
    $fields[$field] = $row[$field];
}  

print_r($fields);

NOTE:

mysql_* has been deprecated. Please use PDO (php.net/manual/en/book.pdo.php) or mysqli_* instead. Otherwise there are security problems.

share|improve this answer
    
jogesh_pi - good job ! Accepted as correct ! Thank you all guys !! Good day to all of you ! –  thecore7 Feb 12 at 10:34
    
@thecore7 glad to help you, to understand a bit more take a look on codepad.org/w0dGw78r it is slimier to your problem. –  jogesh_pi Feb 12 at 10:37

Try to avoid use of mysql_* function as it is deprecated from php 5.5 and will be removed in future.So use mysqli_* function.like this

$con = mysqli_connect("localhost","username","pwd","dbname");
$array = array();
$equery1223 = "SHOW COLUMNS FROM products ";
$eresults1223 = mysqli_query($con,$equery1223) or die(mysqli_error($con));
$i=0;
while($rows3 = mysqli_fetch_array($eresults1223))   
   {
      if($rows3['Field'] ="en_product_name" || $rows3['Field'] ="de_product_name" || $rows3['Field'] ="fr_product_name" || $rows3['Field'] ="ru_product_name" ) 
      {  $array[$i]=$rows3['Field'];$i++; }
   }
$array2 = array();$i=0;
$equery1224 = "select * FROM products ";
$eresults1224 = mysqli_query($con,$equery1224) or die(mysqli_error($con));
$i=0;
while($rows4 = mysqli_fetch_array($eresults1224))   
   {
      $array2[$array[$i]]=$rows4[$array[$i]];$i++;
   }
share|improve this answer

So you want to access key => value, i am using while statement for clearing the Answer.

You can use these functions and parameters:

while ($row = mysql_fetch_assoc($result)) {
    echo $row["userid"];
    echo $row["fullname"];
    echo $row["userstatus"];
}

Or

$row = mysql_fetch_array($result, MYSQL_ASSOC);

It should be solve your problem.

share|improve this answer

Consider replacing mysql_fetch_array with mysql_fetch_assoc.

From the documentation for mysql_fetch_assoc:

Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead. mysql_fetch_assoc() is equivalent to calling mysql_fetch_array() with MYSQL_ASSOC for the optional second parameter. It only returns an associative array.

Updated code:

$query='select en_product_name,de_product_name,fr_product_name,ru_product_name    
    from products where id="'.$pid.'"';    
$result=mysql_query($query) or die('Mysql Error:'.mysql_error().'<br /> Query:'.$query);    
$num_rows=mysql_num_rows($result);    
$row = mysql_fetch_assoc($result);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.