1

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

2
  • try $fields[key] = $value; Commented Feb 12, 2014 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. Commented Feb 12, 2014 at 10:27

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

jogesh_pi - good job ! Accepted as correct ! Thank you all guys !! Good day to all of you !
@thecore7 glad to help you, to understand a bit more take a look on codepad.org/w0dGw78r it is slimier to your problem.
0

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);

Comments

0

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.

Comments

0

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++;
   }

Comments

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.