Updating a column for multiple rows

29 views
Skip to first unread message

Da-Omiete Iboroma

unread,
Feb 2, 2016, 10:16:22 PM2/2/16
to Robert Gonzalez, Professional PHP Developers
Hello, please I am trying to generate passcodes then updating the passcodes colunm in the database for all the rows.  My code is just updating one row and then it will stop.  Please help me out.

$sql5 = "
SELECT *
FROM $table_name
WHERE passcode = ''
";
$result =  mysql_query($sql5,$connection) or die("couldn't connect.");
$j = 1;
while ($row = mysql_fetch_array($result))
{
$_POST["matric_id"] = $row['matric'];
function random_string($length)
{
$key = '';
$keys = array_merge(range(1, 9), range('A', 'Z'));
for ($i = 0; $i < $length; $i++) 
{
$key .= $keys[array_rand($keys)];
}
return $key;
}
$passcode = random_string(8); 
$sql1 = "
UPDATE $table_name SET
passcode = '$passcode'
WHERE matric = '$_POST[matric_id]'
";
$result = mysql_query($sql1,$connection) or die(mysql_error());
}
$j++;

Thanks

Robert Gonzalez

unread,
Feb 2, 2016, 10:42:27 PM2/2/16
to Da-Omiete Iboroma, Professional PHP Developers
Probably because you are generating an error in the second iteration. You can only define a function once. You are defining that function in a loop. You should define your function outside of the loop then call that function from the loop.

There are other areas to be improved, but that one stands out to me the most.
--

Robert Gonzalez
   

Da-Omiete Iboroma

unread,
Feb 4, 2016, 2:32:27 AM2/4/16
to savla jigs, Professional PHP Developers
Thanks Savla Jigs, I tried it but I am still having the same result. I will just update one record and stop.  I am getting this warning 

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given

Thanks. 

On Wed, Feb 3, 2016 at 2:40 PM, savla jigs <savla...@gmail.com> wrote:
$sql5 = "SELECT * FROM $table_name WHERE passcode = ''
";
$result = mysql_query($sql5, $connection) or die("couldn't connect.");
$j = 1;

function random_string($length) {
    $key = '';
    $keys = array_merge(range(1, 9), range('A', 'Z'));
    for ($i = 0; $i < $length; $i++) {
        $key .= $keys[array_rand($keys)];
    }
    return $key;
}

while ($row = mysql_fetch_array($result)) {
    $_POST["matric_id"] = $row['matric'];

    $passcode = random_string(8);

    $sql1 = "UPDATE $table_name SET passcode = '$passcode' WHERE matric = '".$_POST["matric_id"]."'";
    $result = mysql_query($sql1, $connection) or die(mysql_error());
}
$j++;


Try this and let me know.

Thanks & Regard,

Jigar Savla | Software Engineer  

M - 997 080 0919  | E - savla...@gmail.com 

Twitter - @
savlajigar | Facebook - @savlajig

Save Paper, Save Trees, Save the Planet.

Please do not print until & unless it is very necessary.


--
This group is managed by the web application development team at www.360psg.com
---
You received this message because you are subscribed to the Google Groups "Professional PHP Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to professional-p...@googlegroups.com.
Visit this group at https://groups.google.com/group/professional-php.
To view this discussion on the web visit https://groups.google.com/d/msgid/professional-php/CAERp-YOTLbWPqWZir1OfUSqiHLjBen5dTjz3VbSNeuGbaaXPdA%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.


David Dyess

unread,
Feb 4, 2016, 3:48:40 AM2/4/16
to professi...@googlegroups.com, savla jigs

The last $result var needs to be changed, you are overriding the original $result var


Da-Omiete Iboroma

unread,
Feb 4, 2016, 6:26:48 AM2/4/16
to Professional PHP Developers, savla jigs, david...@gmail.com, Robert Gonzalez
Thanks alot, it WORKED.  I renamed the last $result variable to $result2.  The final code is;

$sql5 = "SELECT * FROM $table_name WHERE passcode = ''
";
$result = mysql_query($sql5, $connection) or die("couldn't connect.");
$j = 1;
function random_string($length) {
$key = '';
$keys = array_merge(range(1, 9), range('A', 'Z'));
for ($i = 0; $i < $length; $i++) {
$key .= $keys[array_rand($keys)];
}
return $key;
}
while ($row = mysql_fetch_array($result)) {
$_POST["matric_id"] = $row['matric'];
$passcode = random_string(8);
$sql1 = "UPDATE $table_name SET passcode = '$passcode' WHERE matric = '".$_POST["matric_id"]."'";
$result2 = mysql_query($sql1, $connection) or die(mysql_error());
}
$j++;

Thanks to all of you for helping

Reply all
Reply to author
Forward
0 new messages