Jump to content

PHP Repeating SQL


ThomasBalaban

Recommended Posts

Hello, I am somewhat new to php and I am trying to figure something out. Here is my relevant code....

 

function run_my_query3($query){
        //$serverHandle = mysql_connect('localhost', 'root');
        $serverHandle = mysql_connect('localhost:8888', 'root', 'root', 'user_table');
        //$serverHandle = mysql_connect('localhost', 'afterlyf','ev730l500c','user_table');
        if($serverHandle == false){
            die("bad server connection".mysql_error());
        }
        
        $myDb = mysql_select_db('user_table') or die ('DB Problem - '.mysql_error());
        
        $result4 = mysql_query($query) or die ('Query Problem - '.$query.mysql_error());
        
        mysql_close($serverHandle);
        
        return $result4;
    }

 

 

 

 

$result1 = run_my_query3("SELECT decklists.userName, decklists.userPicture, decklists.decklistName, decklists.decklistSideExtra, decklists.decklistDate FROM decklists, watchers WHERE '$_SESSION[userName]' = watchers.watcherName AND '$_SESSION[userName]' <> watchers.userName ORDER BY decklistDate DESC");

 

 

 

<?php
            while($row = mysql_fetch_array($result1)){
                $userName = $row['userName'];
                $userPictureDeck = $row['userPicture'];
                $decklistsId = $row['decklistsId'];
                $decklistName = $row['decklistName'];
                $decklistMain = $row['decklistMain'];
                $decklistSideExtra = $row['decklistSideExtra'];
                $decklistDate = $row['decklistDate'];
                
                
        ?>
                <p>Deck Uploaded by <?php echo $userName; ?>: <?php echo $decklistName; ?><span> <?php echo $decklistDate; ?> </span></p>
        
        <?php
            }
        ?>

 

 

 

 

Table

watchId     userName     watcherName     watcherPicture

110    CrunchTheWicked    Cloud    adamVajoka.jpg
112    Afterlyfe    Cloud    adamVajoka.jpg
108    chewieAFG    Cloud    adamVajoka.jpg

 

 

I understand why it is repeating three times, which is because Cloud is the current user and his name is used three times within the table. What is the best way to get past this?

Link to comment
Share on other sites

- Please show your code in code blocks, identified by the "<>" icon on the top.

- Also, you dont have to open and close your connection on every query. MySQL will use the last opened connection so you can just set it at the beginning of the file, and close it at the end of the file. (or in init/footer files respectively).

- what happens if cloud changes his username? Huge loss of data.

- You dont have to set new variables if your stuff is already available in a variable.

 

Ask yourself: what do I want from this code anyway? You say it displays 3 times, which is correct according to the table. What do you want it to display then?

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.