WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search


Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - השוואת מחירים בסופר
ZeroLag.com
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : The opposite of mysql_fetch_assoc - convert an associative array into a row in a table.
Categories : PHP, MySQL, Arrays, Databases Click here to Update Your Picture
Sergiu FUNIERU
Date : Aug 31st 2006
Grade : 4 of 5 (graded 3 times)
Viewed : 16535
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Sergiu FUNIERU
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

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.

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




Below you'll find an example:
<?php
   
// Establish the connection with the database
   
require_once ("dbconnect.php");

   
// The database has one table, called `test`, created as following:
    // CREATE TABLE `test`
    // (
    //     `one` varchar(20) default NULL,
      //     `two` varchar(20) default NULL
    // )
    // TYPE=MyISAM;


    // We create the $test array
   
$row = array (
       
"one" => "first",
       
"two" => "second"
   
);

   
// We want to insert the $row into $table
   
$table = "test";

   
// We insert the array into the database
   
mysql_insert_array ($table, $row);
?>



create a grid out of <INPUT TYPE=TEXT> then saving to a database. Uses a 'multi-dimension array', but not really as the array is just one big array with the index of "[$i][$j]". Have a look at the code and you'll see what I mean.
Categories : PHP, MySQL, Arrays, Databases
Function for retrieving MySQL enum values into a PHP array.
Categories : PHP, Databases, MySQL, Arrays
How to load a query result into a PHP Array
Categories : PHP, Databases, Arrays, MySQL
bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager
Categories : MySQL, PHP, MySQL, Complete Programs, Databases
dynamic table columns
Categories : PHP, HTML and PHP, Arrays, Databases, MySQL
Simple conversion functions to change MySQL dates to arrays, arrays to MySQL dates.
Categories : PHP, Arrays, Date Time, Databases, MySQL
This functions makes it easy to use session-variables known from ASP. With one Cookie the array "session" will save and restore from a db-record. In this version MySQL is used but it's should very easy to change
Categories : PHP, Arrays, Cookies, MySQL, Databases
This simple function will take a few arguments and easily set a associative array for each column in a result from a MySQL query
Categories : Databases, PHP, MySQL, Arrays
This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server.
Categories : Databases, MySQL, Complete Programs, PHP, Databases
Finds the median in an array of numbers - Can be used with a MySql database column read into an array
Categories : PHP, Arrays, Databases, MySQL
Sort the results from a SELECT query (any number of columns) into an array automatically.
Categories : PHP, PHP Classes, Arrays, Databases, MySQL
Simple pipe delimited file export program that downloads to a local machine
Categories : PHP, Filesystem, Databases, MySQL, HTTP
GroupIT Engine v1.00rc1
Categories : PHP, Content Management, MySQL, Databases
mySQL/PHP/search with multientry form and table output with colored rows
Categories : PHP, Beginner Guides, MySQL, HTML and PHP, Databases
A script to generate a report from a valid mysql connection. The user has to supply which fields he wants to display in table. All properties are changable.
Categories : PHP, PHP Classes, Databases, MySQL, HTML and PHP
 Zew Owlet wrote :1672
You have wrote:

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

        for ($i = 0; $i &lt; $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 .= ")"; 


But maybe it`s better to use this:
$keys = array_keys($my_array);
$values = array_values($my_array);

$sql = `INSERT INTO ` . $my_table . `(` . implode(`,`, $keys) . `) VALUES ("` . implode(`","`, $values) . `");`