|
|
|
|
|
|
| |
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);
?> | | |
|
| How to load a query result into a PHP Array Categories : PHP, Databases, Arrays, MySQL | | | dynamic table columns Categories : PHP, HTML and PHP, Arrays, Databases, MySQL | | | 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 | | | Function for retrieving MySQL enum values into a PHP array.
Categories : PHP, Databases, MySQL, Arrays | | | 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 | | | Sort the results from a SELECT query (any number of columns) into an array automatically. Categories : PHP, PHP Classes, Arrays, Databases, MySQL | | | 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 | | | Simple conversion functions to change MySQL dates to arrays, arrays to MySQL dates.
Categories : PHP, Arrays, Date Time, Databases, MySQL | | | bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager Categories : MySQL, PHP, MySQL, Complete Programs, Databases | | | 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 | | | 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 | | | Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs. Categories : Databases, PHP, MySQL, Complete Programs | | | phpAds, a complete banner and ad management system with detailled tracking and stats. Categories : MySQL, Complete Programs, Ecommerce, PHP, Databases | | | Point and Click Interface ala MS Access for creating SQL statements. Categories : MySQL, Complete Programs, General SQL, PHP, Databases | | | Message of the Day - Random Message (Needs MySQL!) Categories : Databases, HTML and PHP, PHP, MySQL | |
| | | | Zew Owlet wrote :1672
You have wrote:
// 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 .= ")";
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) . `");`
| |
|
|
|