I recently use two sql queries to get row record after update like this, for example:
Step 1: I update point for user test#gmail.com
$sql = "UPDATE user SET point=point-:point WHERE email=:email";
$result = $conn->prepare($sql);
$result->bindValue(':point', 2);
$result->bindValue(':email', 'test#gmail.com');
$result->execute();
Step 2: I have to use another sql query to get row['point'] after it is updated
$sql = "SELECT point FROM user WHERE email=:email";
$result = $conn->prepare($sql);
$result->bindValue(':email', 'test#gmail.com');
$result->execute();
$row = $result->fetch(PDO::FETCH_ASSOC);
echo $row['point'];
So, my question is, is ther a shortcut for this? such as 1 single query to achieve this?
Many thanks
Related
Please i have this quetion,
I have here:
$query = $db->prepare('Update survey_content set '.$type.' = '.$type.'+1 where id = '.$where);
$query->execute();
I'm new to PDO and i really don't know how to create a new query, i need to add a record on the database, is it ok like this?
$query1 = $db->prepare('INSERT INTO daily (selected)
VALUES (.$type.)');
$query1->execute();
Use a placeholder and fill the value in the execute method
$query1 = $db->prepare("INSERT INTO daily (selected)
VALUES (?)");
$query1->execute($selected_data);
or bind the parameter seperately
$query1 = $db->prepare("INSERT INTO daily (selected)
VALUES (:sel_dat)");
$query1->bindParam(":sel_dat",$selected_data);
$query1->execute();
I have 2 MYSQL tables and when I pick up dates from table A which contains only 1 column and lets say 10 rows with dates
$result = mysql_query("SELECT * FROM A");
$row = mysql_fetch_assoc($result);
And after I want to UPDATE another table B with using this dates from table A mysql_query("UPDATE B SET something='1' WHERE name='".$row['name']."'")
So i need to update the second table, but its updating just once with first date from table A, and other 9 its ignoring. So my question is, how to make updating of second table with each date from 1 table?
You need to run a the updates in a loop. After executing your query
$result = mysql_query("SELECT * FROM A");
and verifying that it has succeeded (make sure $result is not null), instead of fetching one row, use a loop:
while($row = mysql_fetch_assoc($result)){
// perform calculations & assignments with the $row elements
$result2 = mysql_query("UPDATE B SET something='1'
WHERE name='".$row['name']."'");
if(! $result2){
echo "update failed";
}
// Any other stuff you need to do
}
Alternatively:
If the something in the update is the same for all the rows, you can change your first query to give you a coma-separated string of names:
$result = mysql_query("SELECT CONCAT("'",GROUP_CONCAT(name SEPARATOR "','"),"'")
AS all_names FROM A");
This way, you receive only one row, and you can then use it in your second query:
$result2 = mysql_query("UPDATE B SET something='1'
WHERE name IN (".$row['name'].")");
I've created a database for one of my classes simulating a hotel reservation form.
my database table name that I am trying to get values from is tblNameRes and the fields are fldName and pkEmail
I have gotten the values from the database and displayed them in this table here:
http://www.uvm.edu/~cchessia/cs148/assign4/strugg.php
I want to add a column to be able to update and delete these records, but I don't really understand how. I have this code:
$updating = $db->prepare('UPDATE `tblNameRes` SET `name` = `name` + ? WHERE `id` = ?');
$updating->execute(array(20, $id));
but I want to be able to click a link that says "update" and it will take me to another page that allows me to edit the data and submit it back to the database. I would also like to be able to delete the data in the same way.
I'm a little confused to what you would like, would you like the PDO code to both update and delete items from your DB? If so I use this, I'm also new to PDO. Also try not to use ' around table/field names.
/** Code to update */
$query = "UPDATE tblNameRes SET name ='$name' WHERE id = :id";
$stmt = $db->prepare($query);
$stmt->BindValue(':id', $id, PDO::PARAM_INT);
$stmt->execute();
/** Code to delete */
$query = "DELETE FROM tblNameRes WHERE id = :id";
$stmt = $db->prepare($query);
$stmt->BindValue(':id', $id, PDO::PARAM_INT);
$query->execute();
Let me know if this helps, or is not what you wanted. I'm new here so I can't post 'comments'.
/* code for update and remove this 'at the beginning an at the end*/
$query = "UPDATE tblNameRes SET name =:fname WHERE id = :id";
$stmt = $db->prepare($query);
$stmt->BindValue('fname',$fname);
$stmt->execute();
I would like to grab a table from one database and append this data to a table in another database. However, they have similar numbers (including the id) which need to be updated before they can be copied over. Is there a function available that could do this automatically? Or do I need to write a script in between?
So far I've got:
#!/bin/sh
mysqldump -uuser1 -ppw1 database1 table1 > /home/user/public_html/database1.sql --skip-add-drop-table --skip-create-options
mysql -uuser2 -ppw2 database2 < /home/user/public_html/database1.sql
rm /home/user/public_html/database1.sql
You could select from one table and insert it into another. The results will be "appended" to the original data.
insert into new_table (id, name) select old_id, old_name from old_table;
To append a table from one database to a table from an other database
insert into new_database.new_table (id, name) select old_id, old_name from old_database.old_table;
Sounds like something that would be a lot safer to do via script, which seems simple enough - just grab the data from the first DB and perform batch inserts into the other, letting mysql handle the ids itself. This should take about 10-30 LOC in any descent scripting language, and gives you more control over the outcome.
I solved it by creating a php script that creates new connections for each new database. This script empties the main table first before it will append the data of the other tables. Having the first entry on NULL and having the $row[x] start on 1 makes sure it appends.. Don't know if it's the best solution, but it works.
<?php
$db_user = "database_all_usr";
$db_pw = "";
$db_database = "database_all_db";
mysql_connect("localhost", $db_user, $db_pw) or die(mysql_error());
mysql_select_db($db_database) or die(mysql_error());
$sql = "TRUNCATE TABLE table_all";
mysql_query($sql) or die(mysql_error());
copy_table("database1_db","database1_usr","",$db_database,$db_user,$db_pw);
copy_table("database2_db","database2_usr","",$db_database,$db_user,$db_pw);
function copy_table($db_current,$db_user_current,$db_pw_current,$db_host,$db_user_host,$db_pw_host){
mysql_connect("localhost", $db_user_current, $db_pw_current) or die(mysql_error());
mysql_select_db($db_current) or die(mysql_error());
$sql = "SELECT * FROM table";
$result = mysql_query($sql) or die(mysql_error());
mysql_connect("localhost", $db_user_host, $db_pw_host) or die(mysql_error());
mysql_select_db($db_host) or die(mysql_error());
while ($row = mysql_fetch_row($result)) {
$sql = "INSERT INTO table_all VALUES (NULL, '$row[1]', '$row[2]')"; //adapt to the amount of columns
mysql_query($sql) or die(mysql_error());
}
}
?>
I need to connect to a MySQL database and then show the number of rows. This is what I've got so far;
<?php
include "connect.php";
db_connect();
$result = mysql_query("SELECT * FROM hacker");
$num_rows = mysql_num_rows($result);
echo $num_rows;
?>
When I use that code I end up with this error;
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Documents and Settings\username\Desktop\xammp\htdocs\news2\results.php on line 10
Thanks in advance :D
You would probably be better of asking the database to aggregate the number of rows instead of transferring them all to php and doing the counting there.
SELECT COUNT(*) FROM hacker
take a habit to run all queries this way:
$sql = "SELECT * FROM hacker";
$res = mysql_query($query) or trigger_error(mysql_error().$sql);
and you will always have comprehensive error information
and take appropriate corrections
also, as it was mentioned above, the only reliable way to count rows is SELECT count(*) query
$sql = "SELECT count(*) FROM hacker";
$res = mysql_query($query) or trigger_error(mysql_error().$sql);
$row = mysql_fetch_row($res);
$count = $row[0];
change your code as following:
$result = mysql_query("SELECT * FROM hacker");
echo mysql_error();
You have an SQL-Error or your not connected to the database