0

So I have a function that handles queries:

function cfquery($query){
    // Connection's Parameters
    $db_host="localhost";
    $db_name="d";
    $username="u";
    $password="p";
    $db_con=mysql_connect($db_host,$username,$password);
    $connection_string=mysql_select_db($db_name);
    // Connection
    mysql_connect($db_host,$username,$password);
    mysql_select_db($db_name);

    // Query
    $sql = $query;
    $results = mysql_query($sql);
    return $results; 
}

And I'm trying to write a function that will take the results and put them into an array, but this function is generating a 'resource', and everything I'm finding on the internet doesn't work for that data type, or isn't generated dynamically - like you have to know the column names in order to get to the data.

For instance:

while ($row = mysql_fetch_array($results)){
    echo $row['name'] . ',' ;
    echo $row['email'] . ',' ;
    echo $row['city'] . ',' ;
}

I'm looking for something that will get the column names from the results. Like this:

$array = new array();
loop rows index x {
    loop column_names index y {
        $array[x][y] = $results[rows][column_names];
    }
}

If you can't tell, I'm transitioning from ColdFusion, so I have only a vague idea of what a "resource" is (after reading some documentation), and no idea why I can't get an array out of it.

1
  • 1
    It is not recommended to use the old mysql extension for new development, as it has been deprecated as of PHP 5.5.0 and will be removed in the future. See MySQL: Choosing an API Commented Jan 26, 2014 at 13:48

2 Answers 2

2

If you can use MySQLi: Use mysqli_fetch_field_direct() to get field metadata.

The field name can be accessed like so (within your results loop):

$metadata = mysqli_fetch_field_direct($row, $field_index);
$name = $metadata->name;

If you are stuck using the deprecated (as of 5.5) mysql functions: then you can use mysql_field_name($result, $field_index) instead in the same manner although it will directly return the name as a string and not an object.

You can read up on MySQLi and it's implementation here: https://www.php.net/mysqli

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

Comments

0

mysql_* is deprecated... use mysqli_* or pdo

In MySQL...

function cfquery($query)
{
    $return = array();

    $cn = mysql_connect("localhost","u","p") or die('connection error');
    mysql_select_db("d", $cn) or die('select db error');

    $results = mysql_query($query, $cn) or die(mysql_error($cn));

    while ( $row = mysql_fetch_array($results) )
        $return[] = $row;

    mysql_free_result($results);
    mysql_close($cn);

    return $return;
}

3 Comments

You used the deprecated functions in your answer.
User use mysql_* function... I have warned
Your top comment makes it sound like you hadn't. I'd explicitly state that your example uses the deprecated functions to prevent confusion.

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.