My Query return always the Mysql HY093 error but why?
I have all parameters and still its not working...
Can someone help ?
// create PDO params array
$params = [
':employee ' => $formData['employee'],
':usedFrom' => $formData['usedFrom'],
':usedTill' => $formData['usedTill'],
':company' => $formData['company'],
':costCenter' => $formData['costCenter'],
':vehicle' => $formData['vehicle'],
':vehicleTag' => $formData['vehicleTag'],
':updatedAt' => $formData['updatedAt'],
':ID' => $formData['ID']
];
// update entry
$return = $db->pquery(
'UPDATE entry SET employee_id = :employee, used_from = :usedFrom, used_till = :usedTill, company_id = :company, cost_center_id = :costCenter, vehicle_id = :vehicle, vehicle_tag = :vehicleTag, updated_at = :updatedAt WHERE ID = :ID',
$params
);
I have an update query which is not working for me. I am able to make selects quite happily on the same page but I cannot get an update statement to work.
The table is not a part of wordpress so Im wondering if that could be it or if I have just got something wrong.
$query = "UPDATE login_count SET `count` = '100' WHERE `user_id` = $userID ";
$insrt = $wpdb->query($query);
Try this,
$insrt = $wpdb->update(
'login_count', //table_name
array('count'=>'100'),//data
array('user_id'=>$userID),//where
array('%s'),//data format
array('%s')
);
$insrt = $wpdb->update(
'login_count', //table_name
array(
'count' => '100', // string
),
array( 'user_id' => $user_id ), //Where Condition
array(
'%d', // value1
),
array( '%d' )
);
Try this ..user_id = {$userID} ";
Edit
See: Use a $variable inside a SQL string?
I created one table with decimal data types in mysql database. when i want to store some data it stores as 0 only. I tried by changing data types but no result. i am including the portion of php code
$que_sgpdetails = "INSERT INTO sgpa_details ( usn, sem_I, sem_II, sem_III, sem_IV, sem_V, sem_VI, sem_VII, sem_VIII ) VALUES ( :usn, :sem_I, :sem_II, :sem_III, :sem_IV, :sem_V, :sem_VI, :sem_VII, :sem_VIII )";
$query_params5 = array( ':usn' => $_POST['usn'], ':sem_I' => $_POST['sem_I'], ':sem_II' => $_POST['sem_II'], ':sem_III' => $_POST['sem_III'], ':sem_IV' => $_POST['sem_IV'], ':sem_V' => $_POST['sem_V'], ':sem_VI' => $_POST['sem_VI'], ':sem_VII' => $_POST['sem_VII'], ':sem_VIII' => $_POST['sem_VIII'] );
$statement5 = $db->prepare($que_sgpdetails);
$result5 = $statement5->execute($query_params5);
Situation: 2 tables, the first (Persons) storing person names and some other data, and the second (Phones) storing their phone numbers. There can be multiple phone numbers per person (thats why I am using separate tables in the first place).
Goal: Select everything so that in the end I'd have a php array like this:
array
(
'0' => array
(
'name' => 'John Smith'
// other values from table Persons...
'Phones' => array('0' => '12345', '1' => '324343') // from Phones table
),
'1' => array
(
'name' => 'Adam Smith'
// other values from table Persons...
'Phones' => array('0' => '645646', '1' => '304957389', '2' => '9435798') // from Phones table
)
);
ETC.
Phones.person_id = Persons.id
What would be the fastest way to do this? Fastest in a sense of program execution time, not the coding time. I could do simple JOIN but in this case I'd get many duplicate rows, i.e. for each phone I get all the data of the same person again and again in each row if you see what I mean. So I need to work on the array in PHP side. Maybe there's a better way?
One query. Check for typos:
$return = array();
$query = "SELECT pe.id, pe.name, ph.phone FROM Persons pe INNER JOIN phones ph ON pe.id = ph.person_id ";
$results = mysql_query($query);
if($results && mysql_num_rows($results)) {
while($row = mysql_fetch_assoc($results)) {
if(!$return[$row['id']]) {
$return[$row['id']] = array('name' => $row['name'], 'Phones' => array());
}
array_push($return[$row['id']]['Phones'], $row['phone']);
}
}
return $return;
Get the person first, and then query for each of the phone numbers.
$return = array();
$query = "SELECT `id`, `name` FROM `Persons`";
$person_results = mysql_query($query);
if($person_results && mysql_num_rows($person_results)) {
while($person_row = mysql_fetch_assoc($person_results)) {
$person = array();
$person['name'] = $person_row['name'];
$person['phone'] = array();
$query = "SELECT `number` FROM `Phones` WHERE `person_id` = '{$person_row['id']}'";
$phone_results = mysql_query($query);
if($phone_results && mysql_num_rows($phone_results)) {
while($phone_row = mysql_fetch_assoc($phone_results)) {
array_push($person['phone'], $phone_row['number']);
}
}
}
}
return $return;
$res (array)-> (count 50 (!) )
Example:
(
[1] => Array
(
[artistname] => Lady GaGa
[songname] => Love Games
[duration] => 3:31
[url] => 7e91a5ca16ae
[server] => 3
)
[2] => Array
(
[artistname] => DJ Layla
[songname] => Single Lady
[duration] => 3:20
[url] => f0906a3087eb
[server] => 3
)
[3] => Array
(
[artistname] => Lady Gaga
[songname] => Bad Romance (Bimbo Jones Clean Radio Remix)
[duration] => 3:59
[url] => 36e77d5a80357
[server] => 3
)
}
PHP code:
$massquery = '';
foreach($res as $value)
{
if(!get_magic_quotes_gpc())
{
$value['artistname'] = mysql_escape_string($value['artistname']);
$value['songname'] = mysql_escape_string($value['songname']);
$value['duration'] = mysql_escape_string($value['duration']);
$value['url'] = mysql_escape_string($value['url']);
$value['server'] = mysql_escape_string($value['server']);
}
$value['artistname'] = trim($value['artistname']);
$value['songname'] = trim($value['songname']);
$value['duration'] = trim($value['duration']);
$value['url'] = trim($value['url']);
$value['server'] = trim($value['server']);
$sh = mysql_query("SELECT `artistname`,`songname`,`server` FROM `music` WHERE `artistname`='".$value['artistname']."' AMD `songname`='".$value['songname']."' AND `server`='".$value['server']."' LIMIT 1");
if(!mysql_num_rows($sh))
{
$massquery .= '("'.$value['artistname'].'", "'.$value['songname'].'", "'.$value['duration'].'", "'.$value['url'].'", "'.$value['server'].'"),';
}
}
if(!empty($massquery))
{
$massquery = substr($massquery, 0, -1);
$query = mysql_query('INSERT INTO `music` (`artistname`, `songname`, `duration`, `url`, `server`) VALUES '.$massquery);
}
mysql_close($mysql);
It turns out 50 requests "SELECT" to the database, which is very bad = (
How can I optimize this code?
From answers:
CREATE TABLE `music` (
`id` int(50) NOT NULL auto_increment,
`artistname` varchar(50) NOT NULL,
`songname` varchar(50) NOT NULL,
`duration` varchar(6) NOT NULL,
`url` varchar(255) NOT NULL,
`server` int(5) NOT NULL,
PRIMARY KEY (`id`),
KEY `artistname` (`artistname`,`songname`,`server`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `music` VALUES ('test', 'btest', 1);
...
SELECT `artistname` , `songname` , `server`
FROM `music`
WHERE FALSE
OR (
`artistname` = 'test'
AND `songname` = 'btest'
AND `server` = '1'
)
OR (
`artistname` = 'sas'
AND `songname` = 'asf'
AND `server` = '1'
)
LIMIT 0 , 30
How do I INSERT those songs that are not yet in the database?
Sorry for bad english
You want to insert new records only if no other record with the tuple (artistname,songname,server) (already) exists.
If you create a unique index for these three fields MySQL won't insert a doublet. Then you can either use something like
INSERT IGNORE INTO
tablename
(a,b,c,x,y,z)
VALUES
(1,2,3,4,5,6),
(7,8,9,10,11,12),
...
(95,96,97,98,99,100)
or a prepared statement, e.g.
$pdo = new PDO("mysql:host=localhost;dbname=test", 'localonly', 'localonly');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/* test table */
$pdo->exec('
CREATE TEMPORARY TABLE foo (
id int auto_increment,
artistname varchar(64) not null,
songname varchar(64) not null,
duration varchar(16) not null,
url varchar(64) not null,
server int not null,
primary key(id),
unique key (artistname,songname,server)
)
');
$data = array(
array(':artistname' => 'Lady GaGa', ':songname' => 'Love Games', ':duration' => '3:31', ':url' => '7e91a5ca16ae', ':server' => 3),
array(':artistname' => 'DJ Layla', ':songname' => 'Single Lady', ':duration' => '3:20', ':url' => 'f0906a3087eb', ':server' => 3),
array(':artistname' => 'Lady Gaga', ':songname' => 'Bad Romance (Bimbo Jones Clean Radio Remix)', ':duration' => '3:59', ':url' => '36e77d5a80357', ':server' => 3)
);
/* the "actual" test script */
$stmt = $pdo->prepare('
INSERT IGNORE INTO
foo
(duration, artistname, songname, server, url)
VALUES
(:duration, :artistname, :songname, :server, :url)
');
// first run, all three records should be inserted
foreach( $data as $params ) {
$stmt->execute($params);
}
// second run
// same artist/songname, different server
$newData = $data[0]; $newData[':server'] = 4;
$data[] = $newData;
// and a completly new record
$data[] = array(':artistname' => 'xyz', ':songname' => 'The ABC song', ':duration' => '2:31', ':url' => 'whatever', ':server' => 2);
// again insert all records (including the three that have already been inserted)
foreach( $data as $params ) {
$stmt->execute($params);
}
/* fetch all records */
foreach( $pdo->query('SELECT * FROM foo', PDO::FETCH_NUM) as $row ) {
echo join(', ', $row), "\n";
}
prints
1, Lady GaGa, Love Games, 3:31, 7e91a5ca16ae, 3
2, DJ Layla, Single Lady, 3:20, f0906a3087eb, 3
3, Lady Gaga, Bad Romance (Bimbo Jones Clean Radio Remix), 3:59, 36e77d5a80357, 3
4, Lady GaGa, Love Games, 3:31, 7e91a5ca16ae, 4
5, xyz, The ABC song, 2:31, whatever, 2
The first three records have not been duplicated.
Create a single select for all the relevant cases like this, and verify the results by means of PHP:
$sh = "SELECT `artistname`,`songname`,`server` FROM `music` WHERE ";
$pq = ""
foreach($res as $value)
{
if(!get_magic_quotes_gpc())
{
$value['artistname'] = mysql_escape_string($value['artistname']);
$value['songname'] = mysql_escape_string($value['songname']);
$value['duration'] = mysql_escape_string($value['duration']);
$value['url'] = mysql_escape_string($value['url']);
$value['server'] = mysql_escape_string($value['server']);
}
$value['artistname'] = trim($value['artistname']);
$value['songname'] = trim($value['songname']);
$value['duration'] = trim($value['duration']);
$value['url'] = trim($value['url']);
$value['server'] = trim($value['server']);
$sh .= $pq . `(artistname`='".$value['artistname']."' AMD `songname`='".$value['songname']."' AND `server`='".$value['server']."')");
$pq = " OR ";
}
$res = mysql_query($sh);
The select query you wrote can't be inside the foreach, or of course it'll query each time. Turn your $res into a long WHERE clause:
$sql = "SELECT `artistname`,`songname`,`server` FROM `music` WHERE FALSE ";
foreach($res as $value)
{
// ...
$sql .= "OR (`artistname`='".$value['artistname']."' AND `songname`='".$value['songname']."' AND `server`='".$value['server'].")";
}
and then run that query against the database and build your INSERT query.