Click to See Complete Forum and Search --> : SQL Alter


Joseph Witchard
10-06-2008, 11:22 AM
I've seen examples of using ALTER to change a field's varchar to fifty instead of twenty-five, but only things like that. How do you actually use ALTER to change a field's value?

scragar
10-06-2008, 02:15 PM
explain what you mean by "value" are you after update? or do you mean default?

chazzy
10-06-2008, 04:59 PM
alter is used to change structure. update is used to modify data. if you're getting an error because you want to change a varchar field from 50 to 25 that the values are too large, just issue the update first then alter.

Joseph Witchard
10-07-2008, 12:07 AM
Okay, so update is what I was trying to do. Could you give me an example of an update query (using PHP, if at all possible)?

scragar
10-07-2008, 02:19 AM
syntax for the most part:
UPDATE tableName SET field1=value1, field2=value2
WHERE condition

$query = "UPDATE users SET password={$newPass} WHERE uid={$userID}";
if( mysql_query($query) ){
echo mysql_affected_rows() . " rows updated";
}else{
echo "error, mysql said: " . mysql_error();
}

Joseph Witchard
10-07-2008, 12:52 PM
Thanks:)

Can that be done with MySQL Improved syntax?

chazzy
10-07-2008, 07:54 PM
Thanks:)

Can that be done with MySQL Improved syntax?

what in the world is "mysql improved syntax"?

Joseph Witchard
10-07-2008, 11:12 PM
Well, for example, if you were using PHP's MySQL Improved extension instead of the usual MySQL, a select query would be written out like:

<?php

$query = "SELECT user_id, username FROM users WHERE user_id = ? AND username = ?";

// instead of the usual
$query = "SELECT user_id, username FROM users WHERE user_id = '$id' AND username = 'Lord Vader'"; ?>

chazzy
10-07-2008, 11:22 PM
Well, for example, if you were using PHP's MySQL Improved extension instead of the usual MySQL, a select query would be written out like:

<?php

$query = "SELECT user_id, username FROM users WHERE user_id = ? AND username = ?";

// instead of the usual
$query = "SELECT user_id, username FROM users WHERE user_id = '$id' AND username = 'Lord Vader'"; ?>

Ok, well it's not like mysqli has different sql structure. the same way you bind parameters to a select works in inserts and updates as well.


$query = "UPDATE users SET password=? WHERE uid=?";