thecore7
thecore7

Reputation: 484

Mysql query to get field name and result as array key and array value

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

Upvotes: 1

Views: 11557

Answers (4)

krishna
krishna

Reputation: 4089

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

Upvotes: 0

jogesh_pi
jogesh_pi

Reputation: 9782

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.

Upvotes: 3

Mandeep Gill
Mandeep Gill

Reputation: 390

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.

Upvotes: 0

Andrew Mackrodt
Andrew Mackrodt

Reputation: 1826

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

Upvotes: 0

Related Questions