Good day

What I would like to do is to alter a mysql table with data in it then add manipulated string data to that created column i.e I first add an extra column to my table then add a reverse string coded from data that had already been inserted.

I have an email field in my database so what I would actually do is to take the email address reverse string it then insert it into the newly added mysql column.

My reverse string works perfectly its just that I can't seem to add the extra column to my table, any ideas would be highly appreciated.

Here is my code

$query1="alter table Registration add column Deep text (20)";
$result1=mysql_query($query1);

//here's the query that selects the email field
$sql="select email fromRegistration";
$result2=mysql_query($sql);

While($row=mysql_fetch_array($result2))
{
$my_str=strrev($row['email_add']));

echo $my_str."<br>";

}

But the table isn't getting altered.

Dani AI

Generated

Quick summary and diagnosis (for , and following /@diafol): the reverse logic itself is fine — the ALTER failed because of small syntax/logic issues and because your script never reported the real SQL error. Also, avoid the removed/ deprecated PHP mysql extension in new code; use mysqli or PDO instead. (php.net)

Common problems to fix (seen in your snippet)

  • Typos / mismatched names (for example select email fromRegistration missing a space, using email_add vs email, an extra parenthesis in strrev(...)), which will make the script fail before you see results.
  • No error checking: always surface DB errors (or use exceptions) so you see why ALTER failed. (php.net)
  • Choose an appropriate column type: use VARCHAR(n) for short reversed values or TEXT/TINYTEXT when you need larger storage; TEXT doesn’t require a size in the same way VARCHAR does. (dev.mysql.com)

Quick, modern example (PDO with exceptions — replaces deprecated mysql_* calls)

<?php
$pdo = new PDO('mysql:host=localhost;dbname=yourdb;charset=utf8mb4', $user, $pass, [
  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);

$pdo->exec("ALTER TABLE `Registration` ADD COLUMN `rev_email` VARCHAR(255) NULL");
$pdo->exec("UPDATE `Registration` SET `rev_email` = REVERSE(`email`)");
?>

This prints an exception if something goes wrong so you can fix it (bad SQL, permissions, wrong table name, etc.). Use prepared statements when inserting user data.

Better approach: don’t store derived values unless you must. Compute on select or use a generated column (MySQL 5.7+): the server can maintain the reversed email automatically, avoiding redundancy. Example DDL:

ALTER TABLE `Registration`
  ADD COLUMN `rev_email` VARCHAR(255) GENERATED ALWAYS AS (REVERSE(`email`)) VIRTUAL;

Generated columns and rules are documented in the MySQL manual; REVERSE() is a built-in string function. (docs.oracle.com)

Troubleshooting checklist

  • Run the ALTER by itself in a DB client to see the error.
  • Enable/display DB errors (mysqli_error / PDOException). (php.net)
  • Check DB user privileges and table-name casing on your server.
    Following those steps will show the exact failure and let you apply one of the clean fixes above.

Recommended Answers

All 2 Replies

Member Avatar for Member #120589

This can be done from just mysql - no php as such req'd

ALTER TABLE Registration ADD COLUMN Deep TEXT (20)

UPDATE Registration SET Deep = REVERSE(email_add)

There's no need to do this though as you're just repeating info to some extent.

You can retrieve the reverse string at any time with php (as you've done) or...

SELECT REVERSE(email_add) AS Deep FROM Registration

So in this case Deep is just a calculated field - it doesn't exist in the DB.

Thanks diafol it's much appreciated!!

That was pretty awesome.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.