Fetch result from MySQL row with multiple same-name columns with PHP

To fetch result from MySQL row with multiple same-name columns with PHP…
Press more and you find that you need. Sorry for bla bla bla some problems with wordpress bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla

<?php
$Host="localhost";
$UserName="username";
$UserPass="password";
$Name="database";
$table1 = "sometable1";
$table2 = "sometable2";
//columnname which used to join tables
$query="SELECT
    ".$table1.".*, ".$table2.".*
FROM
    `".$table1."`
INNER JOIN
    `".$table2."`
ON
    ".$table1.".columnname = ".$table2.".columnname;";
$connection = mysqli_connect($Host, $UserName, $UserPass);
if(!$connection){
    die('Not connected: ' . mysqli_error($connection));
}
$selectDB = mysqli_select_db($connection, $Name);
if(!$selectDB){
    die ("Can't use db: " . mysqli_error($connection));
}
$result = mysqli_query($connection, $query, MYSQLI_USE_RESULT) or die ("Query problem: " . mysqli_error($connection);
if($result){
    $qualified_names = array();
    for ($i = 0; $i < mysqli_num_fields($result); ++$i){
        $fieldinfo=mysqli_fetch_field_direct($result,$i);
        $table = $fieldinfo->table;
        $field = $fieldinfo->name;
        $qualified_names["$table.$field"]="$table.$field";
    }
    while($row = mysqli_fetch_row($result)){
        $newrow = array_combine($qualified_names, $row);
        print_r($newrow);
    }
}
?>

Original from:

  1. https://stackoverflow.com/questions/1416980/how-to-fetch-result-from-mysql-row-with-multiple-same-name-columns-with-ph
  2. https://stackoverflow.com/questions/12851261/how-to-mysql-fetch-array-on-joined-tables-but-columns-have-same-name/12851315#12851315

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s