/    Sign up×
Community /Pin to ProfileBookmark

Hello everybody, i have a problem

Example:

$tableName=”User”;
$colummName=array(‘userID’,’User_name’,’Password’);
$valueName=array(‘1′,’admin’,’123456′);

and i want to print query like that

INSERT INTO User (userID, User_name, Password) VALUES (1,’admin’,’123456′)

What can i do? ?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@Curtis004Mar 06.2010 — Forgive me if my syntax is a little rough but hey wheres the fun in a solution that works first time.

echo "INSERT INTO" . $tableName . " (" . $colummName[0] . ", " . $colummName[1] . ", " . $colummName[2] . ") VALUES (" . $valueName[0] . ", " . $valueName[1] . ", " . $valueName[2] . ");";

? I think thats right..
Copy linkTweet thisAlerts:
@hoangtucomputerauthorMar 06.2010 — Thanks for your help, but my problem is:

Example

class queryGenerator()

{

var $tableName;

var $colummName=array();

var $valueName=array();

function InsertQuery()
{
echo "INSERT INTO ".$tableName."(".$this->colummName.")"." VALUES (".$this->valueName.")";

}


}

I need a function to generate mysql query automatically
Copy linkTweet thisAlerts:
@NogDogMar 06.2010 — One approach:
[code=php]
<?php
/**
* Get comma-separated list for use in query
* @return string
* @param array $array Values to be used
* @param string $type 'Value' (default) or 'Column'
*/
function dbListFromArray($array, $type='Value')
{
$type = ucfirst($type);
if(in_array($type, array('Value', 'Column')))
{
$func = 'mysql'.$type;
array_walk($array, $func);
}
return implode(', ', $array);
}
/**
* Callback function for 'Column' list type
* @return bool
* @param string $value
* @param string $key (Not used)
*/
function mysqlColumn(&$value, $key)
{
$value = "$value";
return true;
}
/**
* Callback function for 'Value' list type
* @return bool
* @param string $value
* @param string $key (Not used)
*/
function mysqlValue(&$value, $key)
{
if(!is_numeric($value))
{
if(get_magic_quotes_gpc())
{
$value = stripslashes($value);
}
if(($value = mysql_real_escape_string($value)) === false)
{
throw new Exception("No MySQL connection");
}
$value = "$value";
}
return true;
}

// Sample usage:
mysql_connect('localhost', '*****', '*****');
$cols = array('col_1', 'col_two', 'column3');
$vals = array(1.23, "John", "O'Reilly");
try
{
$sql = "INSERT INTO table (" . dbListFromArray($cols, 'Column') .
") VALUES (" . dbListFromArray($vals, 'Value') . ")";
echo $sql;
}
catch(Exception $e)
{
die($e);
}
[/code]
Copy linkTweet thisAlerts:
@hoangtucomputerauthorMar 08.2010 — Thanks NogDog, My problem is found
×

Success!

Help @hoangtucomputer spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...