Database Code

  Home arrow Database Code arrow The opposite of mysql_fetch_assoc
DATABASE CODE

The opposite of mysql_fetch_assoc
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2006-08-31

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    The mysql_fetch_assoc "converts" a row from a table into an associative array.
    The mysql_insert_array "converts" an associative array into a row in a table.

    By : LunLun

    Please see a full working example in the zip file.

    <?php
    function mysql_insert_array ($my_table, $my_array) {


    // Version 1.1
    // 31.aug..2006 12:41
    // Copyright: you may do whatever you wish (and can ;-) ) with this code

    // Find all the keys from the array $my_array
    $keys = array_keys($my_array);

    // Find the number of the keys
    $keys_number = count($keys);

    // Generate the column name for the $sql query
    $column_names = "(";
    $values = "(";

    for ($i = 0; $i < $keys_number; $i++) {
    $column_name = $keys[$i];

    // We don't add "," after the last one
    if ($i == ($keys_number - 1)) {
    $column_names .= "`$keys[$i]`";
    $values .= "'$my_array[$column_name]'";
    } else {
    $column_names .= "`$keys[$i]`" . ", ";
    $values .= "'$my_array[$column_name]'" . ", ";
    }
    }

    // Proper end the column name and the value
    $column_names .= ")";
    $values .= ")";

    // We compose the query
    $sql = "insert into `$my_table` ";
    $sql .= $column_names;
    $sql .= ' values ';
    $sql .= $values;

    $result = mysql_query($sql);

    if ($result)
    {
    echo "The row was added sucessfully";
    return true;
    }
    else
    {
    echo ("The row was not added<br>The error was" . mysql_error());
    return false;
    }
    }
    ?>

    Click to Download File



    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Database Code Articles
    More By Codewalkers

    blog comments powered by Disqus

    DATABASE CODE ARTICLES

    - Converting CSV Files to MySQL Insert Queries...
    - Examples and Tools for Database Design
    - Relationships, Entities and Database Design
    - Modeling and Designing Databases
    - Data extract to Excel
    - Oracle database class 0.76
    - The opposite of mysql_fetch_assoc
    - On line Thermal Transmitance Calculation
    - pjjTextBase
    - PHP Object Generator
    - FastMySQL
    - RC4PHP
    - SQL function with integrated sprintf()
    - DB Interaction Classes v1.1
    - deeMySQLParser

    Developer Shed Affiliates

     



    © 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap