Jump to content

Another dumb question


lamajlooc

Recommended Posts

<?php
    include('./mysql_connect.php');
    $query = "SELECT email FROM pmnl_email";
    $result = mysql_query($query) or die(mysql_errno());
    while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
        echo "$row";
        $query = "UPDATE users SET email = '$row[0]' WHERE email <> '$row[0]'";
        $result = mysql_query($query) or die(mysql_errno());
    }
    mysql_close();
?>

What I want this code to do is to read all the emails from a database table (mysql_connect.php) and then update another table in the same database with those emails.  Am I retarded?  I've tried it with "if ($row = mysql_fetch_array($result, MYSQL_NUM)) {" and I get the same result. 

I don't get any errors at all. But the other table is always empty.

Also, if anyone can help me with THIS wierd thing:  When I try and browse the database "users" in phpMyAdmin it keeps sending me a file to download (which is blank) instead of letting me browse.  I've never had a database table that was empty so I don't know is this is regular behavior or not.

Thanks guys.  You always help me out!
Link to comment
Share on other sites

I see a couple of problems. First directly relating to the issue you describe, the problem is that you are reusing the $result variable.

You have this line:
[code]while ($row = mysql_fetch_array($result, MYSQL_NUM)) {[/code]
And then within that loop you do this:
[code]$result = mysql_query($query) or die(mysql_errno());[/code]

2nd, your query inside the while loop doesn't make sense to me:
[code]$query = "UPDATE users SET email = '$row[0]' WHERE email <> '$row[0]'";[/code]

You are wanting it to update every record such that the email filed will the value if that email field does not already equal that value? Once you get the loop functioning correctly all you will end up with is that all the 'email' fields in the user table will end up being equal to the last entry from the first result set.
Link to comment
Share on other sites

[quote]You can't echo $row without any parameters.[/quote]

Keys can be accessed by numeric index and in fact MUST be when using MYSQL_NUM.

I think the problem might be in your WHERE clause, shouldn't it be = and not <> ? Makes more sense to me.

Also, complex variables (ie arrays) really ought to be surrounded by culry braces when within double quotes. Try...

[code=php:0]
$query = "UPDATE users SET email = '{$row[0]}' WHERE email = '{$row[0]}'";
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.