Jump to content
php.lv forumi

Dinamisks kverijs


daGrevis

Recommended Posts

Tas tik, ja tiks lietots MySQL.

 

Bet normālajai INSERT sintaksei arī var daaaudz vienkāršāk veidot kveriju:

 

$q = array("username" => "'$uname'", "password" => "'$pass'", ...); // eskeipošanu var iznest ārpusē, vai darīt vēlāk
$query = "INSERT INTO users (" . implode(",", array_keys($q)) . ") VALUES (" . implode(",", array_values($q)) . ")";

Link to comment
Share on other sites

Bubu, viņam kas nepatīk, nevar saprast kas... =(

 

$SQL = array(

'username' => $username,
'password' => sha1($password),
'mail' => $mail,
'time' => time(),
'gender' => ($gender ? $gender : ''),
'country' => ($country ? $country : '')

);

mysql_query('

INSERT INTO `users` (' . implode(',', array_keys($SQL)) . ')
VALUES (' . implode(',', array_values($SQL)) . ')

') or die(mysql_error());

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@gmail.com,1274119340,male,lv)' at line 2
Link to comment
Share on other sites

Bubu, viņam kas nepatīk, nevar saprast kas... =(

 

$SQL = array(

'username' => $username,
'password' => sha1($password),
'mail' => $mail,
'time' => time(),
'gender' => ($gender ? $gender : ''),
'country' => ($country ? $country : '')

);

mysql_query('

INSERT INTO `users` (' . implode(',', array_keys($SQL)) . ')
VALUES (' . implode(',', array_values($SQL)) . ')

') or die(mysql_error());

 

Nez, ievietoajmās vērtības ir pēdiņās?

Link to comment
Share on other sites

Hmmm Es vispār uztvēru pareizi domu?

 

$SQL = array(

'`username`' => $username,
'`password`' => sha1($password),
'`mail`' => $mail,
'`time`' => time(),
'`gender`' => ($gender ? $gender : ''),
'`country`' => ($country ? $country : '')

);

mysql_query('

INSERT INTO `users` (' . implode(',', array_keys($SQL)) . ')
VALUES (' . implode(',', array_values($SQL)) . ')

') or die(mysql_error());

 

Neiet. =(

Link to comment
Share on other sites

E??

 

$SQL = array(

'username' => "$username",
'password' => "sha1($password)",
'mail' => "$mail",
'time' => "time()",
'gender' => "($gender ? $gender : '')",
'country' => "($country ? $country : '')"

);

mysql_query('

INSERT INTO `users` (' . implode(',', array_keys($SQL)) . ')
VALUES (' . implode(',', array_values($SQL)) . ')

') or die(mysql_error());

 

Nekā. =D Bet...

 

Tā nu patiešām nav.

Pārbaudīju, ja ir tas iepriekšējais variants, ar funkciju print_r(), dati masīvā ir ok! =)

 

Problēma ir kur mysql_query(). Tik kur...

 

Tagad ir tā http://paste.php.lv/5fe77d9c6e3cdc441d6d021b947ed5c8?lang=php . Mle... :>

Edited by daGrevis
Link to comment
Share on other sites

kautkā tā. nākamreiz izlasi, ko tev mēģina paskaidrot, nevis krāmē pēdiņas pa labi un pa kreisi.

echo $q = sprintf("INSERT INTO `users` (%s) VALUES ('%s')", implode(', ', array_map('mysql_escape_string', array_keys($SQL))), implode("', '",array_map('mysql_escape_string', $SQL)));

Link to comment
Share on other sites

Pag, briedis. Es sapratu! =D

 

Bet ļoti stulbi izskatās darīt šādi, vai ne??

 

$SQL = array(

'`username`' => '\'' . $username . '\'',
'`password`' => '\'' . sha1($password) . '\'',
'`mail`' => '\'' . $mail . '\'',
'`time`' => '\'' . time() . '\'',

'`gender`' => '\'' . ($gender ? $gender : '') . '\'',
'`country`' => '\'' . ($country ? $country : '') . '\''

);

 

Tad nu es gribēju automātiski pievienot ', bet kaut kas neiet... Mle :>

 

forEach(array_values($SQL) as $x) {

$x = '\'' . $x . '\'';

}

Link to comment
Share on other sites

Pag, briedis. Es sapratu! =D

 

Bet ļoti stulbi izskatās darīt šādi, vai ne??

 

Tad nu es gribēju automātiski pievienot [b]'[/b], bet kaut kas neiet... Mle :>

[code]forEach(array_values($SQL) as $x) {

$x = '\'' . $x . '\'';

}

 

 

Nu jā, $x ta tu modificē, bet x šinī gadījumā nav atkarīgs no masīva, tā ir vienkārši kārtējā vērtība izvilkta no masīva un ielikta mainīgajā $x.

 

Variants pie katras iterācija saglabāt atpakaļ masīvā

 

foreach($sql as $key => $value){
 $value= ...; //modificējam
 $sql[$key] = $value; //saglabājam atpakaļ masīvā
}

 

vai arī masīva elementu ņemt kā referenci/norādi

 

foreach($sql as &$value){
 $value = "modificējam"; //modoficējam uzreiz masīvā, jo mainīgais ir kā norāde/reference
}

Edited by briedis
Link to comment
Share on other sites

Labaak IMO izskataas shaadi:

 

foreach($items as &$item) {
 $item = "'$item'";
}

 

Nekaa ciiniiities ar eskeiposhanu un single quotes.

 

Un vispaar (es jau kuro reizi saku) - parametrizeetie kveeriji FTW.

 

Vai vismaz shaadi:

 

$q = "SELECT stuff FROM the_stuff WHERE stuffing = '%s'";
$q = sprintf($q, 'the-stuffing-thing'); // SELECT stuff FROM the_stuff WHERE stuffing = 'the-stuffing-thing'

Edited by rATRIJS
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...