Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I want to edit/change the value of a specific column value with PHP. For example, let's say that I have thousands of records and I want to change every record's column value to 1. How would I do this in PHP?

<?php
$id='rank';
$username="root";
$password="";
$database="python_crawler";
$host="localhost";
mysql_connect($host,$username,$password);

$query=" SELECT * FROM url WHERE id='$id'";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();

$i=0;
while ($i < $num) {
$first=mysql_result($result,$i,'1');
++$i;
}
?>
share|improve this question

closed as off topic by Jeff Ferland, Mark Wagner, Shane Madden, womble, John Gardeniers Apr 4 '12 at 5:26

Questions on Server Fault are expected to relate to professional server, networking, or related infrastructure administration within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

4  
Follow a PHP/MySQL tutorial. Use an update statement without a where clause. –  Jeff Ferland Apr 4 '12 at 1:47
    
Really didn't help me, I've already looked for one. –  Noah Brainey Apr 4 '12 at 1:52
    
Well, what have you done and where in the process are you stuck? –  Jeff Ferland Apr 4 '12 at 1:54
    
Updated, I'm getting an error. mysql_numrows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\add.php on line 11 –  Noah Brainey Apr 4 '12 at 1:55
    
Noah, please review your previous questions and accept correct answers where appropriate. –  Kyle Smith Apr 4 '12 at 2:15

1 Answer 1

mysql_close();

$i=0;
while ($i < $num) {
$first=mysql_result($result,$i,'1');

Don't close your connection before you fetch your results. You need to not have the mysql_close() until you're done with your database connection.

$query="UPDATE url SET someColumn = 1";
$result = mysql_query($query);

$result is a boolean (true / false) on update statements.

Finally, if you're taking user input anywhere, be aware that you've set yourself up for SQL injection. Read up on using mysqli and binding parameters. At this point, it's kind of the fault of PHP that this isn't what the tutorial is, but that's the way of the world.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.