note 70435 added to function.mysql-fetch-assoc

From: Date: Mon, 16 Oct 2006 19:05:21 +0000
Subject: note 70435 added to function.mysql-fetch-assoc
Groups: php.notes 
Request: Send a blank email to php-notes+get-118580@lists.php.net to get a copy of this message
I just fixed some things in my own mysql_insert_assoc - I used it in development and just noticed things that went wrong... <?php class MySQL { static $link = NULL; static function uncached_insert ($table, $values) { // Find all the keys (column names) from the array $my_array $columns = array_keys($values); // Find all the values from the array var_export($columns); reset($values); while (list($key, $val) = each($values)) { $values[$key] = mysql_real_escape_string($val,self::$link); } // We compose the query $sql = 'INSERT INTO '.$table.' '; // implode the column names, inserting ', ' between each // we add the enclosing quotes at the same time if (count($columns)>0) { $sql .= '(' . implode(', ', $columns) . ')'; } $sql .= ' VALUES '; // Same with the values if (count($columns)>0) { $sql .= '(\'' . implode('\', \'', $values) . '\')'; } else { $sql .= '()'; } $result = mysql_query($sql,self::$link); var_export($sql); if (!$result) echo 'The row was not added<br>The error was ' . mysql_error(); return $result; } } ?> Unfortunately it looks way different from what I had before (because I copied it from my program), but it fixes these bugs: - fields MUST have backticks, no single quotes - empty arrays did trouble - space after the error echo ---- Server IP: 217.13.201.10 Probable Submitter: 84.142.65.23 ---- Manual Page -- http://www.php.net/manual/en/function.mysql-fetch-assoc.php Edit -- https://master.php.net/note/edit/70435 Del: integrated -- https://master.php.net/note/delete/70435/integrated Del: useless -- https://master.php.net/note/delete/70435/useless Del: bad code -- https://master.php.net/note/delete/70435/bad+code Del: spam -- https://master.php.net/note/delete/70435/spam Del: non-english -- https://master.php.net/note/delete/70435/non-english Del: in docs -- https://master.php.net/note/delete/70435/in+docs Del: other reasons-- https://master.php.net/note/delete/70435 Reject -- https://master.php.net/note/reject/70435 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#118580) next »