+ Reply to Thread
Results 1 to 10 of 10

Thread: mysql table if not exists

  1. #1
    garrensilverwing's Avatar
    garrensilverwing is offline x10 Sophmore garrensilverwing is an unknown quantity at this point
    Join Date
    Nov 2008
    Posts
    148

    mysql table if not exists

    hey guys i am trying to write a code with php/mysql that will create a table inside my comments database...however when i run the code the first time i visit the page it creates the table but if i go back to the page it says table already exists when i want it to display the comments left and the post comment input box
    here is my code:
    Code:
    function pagecomments($thread)
        {
                    $username = "frostbit_admin";
                    $password = "***"; 
                    $server = "localhost";
                    $commentsdatabase="frostbit_comments";
                    $mysqlconnection = mysql_connect($server, $username, $password); 
                    if (!$mysqlconnection) 
                        { 
                            die('There was a problem connecting to the mysql server. Error returned: '. mysql_error()); 
                        } 
                    $commentsdatabaseconnection = mysql_select_db($commentsdatabase); 
                    if (!$commentsdatabaseconnection)
                        { 
                            die('There was a problem using that mysql database. Error returned: '. mysql_error());
                        }
                    $sql="CREATE TABLE IF NOT EXISTS `$thread`(`id` int(16) NOT NULL AUTO_INCREMENT,`posted` datetime NOT NULL,`user` varchar(16) NOT NULL,`comment` text NOT NULL,`modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,`modifiedby` varchar(16) NOT NULL,PRIMARY KEY (`id`)) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=13;";
                    mysql_query($sql) or die(mysql_error());
                }
    function displaycomments($thread)
        {
        if(isset($_SESSION['user']))
            {
            $sql="SELECT * FROM `$thread` ORDER BY `posted` DESC;";
            $query=mysql_query($sql) or die(mysql_error());
            $commentcount=count($query);
            while($row = mysql_fetch_array($query))
                {
                    $username=$row['user'];
                    $text=$row['comment'];
                    echo "<p class=\"desctxt\"><i>Posted by: <strong>$username</strong> at ".$row['posted']."</i></p><br>";
                    echo "<div class=\"commentdesign\">$text</div>";
                    echo "<p class=\"desctxt\" style=\"margin-bottom: .5em;\"><i>Last Modified: ".$row['modified']."</i></p>";
                    commentoptions($username,$text);
                }
            }
        else
            {
                echo "<i>You must be logged in to view comments.</i>";
            }
        }
    function commentoptions($username,$text)
        {
    ?>
        <div id="commentwrapper" style="position:relative; height:3em; width:25em; margin-right:auto; margin-left:auto; text-align:left;">
            <div style="position: relative; z-index: 1; width: 25em; height: 1em;" id="options">
            <div style="position: relative; z-index: 1; float: left;" id="viewer">
    <?php
                echo "<img alt=\"reply\" src=\"http://".$GLOBALS['domain']."/images/commenticons/comment_add.png\" />";
                echo "<img alt=\"quote\" src=\"http://".$GLOBALS['domain']."/images/commenticons/comment_quote.png\" />";
                echo "<img alt=\"report\" src=\"http://".$GLOBALS['domain']."/images/commenticons/comment_warning.png\" />";
            echo "</div>";
        if($username == $_SESSION['user'])
            {    
            echo "<div style=\"position: relative; z-index: 1; float: left;\" id=\"poster\">";
                echo "<img alt=\"edit\" src=\"http://".$GLOBALS['domain']."/images/commenticons/comment_edit.png\" />";
                echo "<img alt=\"notify\" src=\"http://".$GLOBALS['domain']."/images/commenticons/comment_email.png\" />";
            echo "</div>";
            }
        if($_SESSION['admin'] == 1)        
            {
            echo "<div style=\"position: relative;  z-index: 1; float: right;\" id=\"admin\">";
                echo "<img alt=\"delete\" src=\"http://".$GLOBALS['domain']."/images/commenticons/comment_remove.png\" />";
                echo "<img alt=\"warn\" src=\"http://".$GLOBALS['domain']."/images/commenticons/comment_error.png\" />";
                echo "<img alt=\"lock\" src=\"http://".$GLOBALS['domain']."/images/commenticons/lock.png\" />";
                echo "<img alt=\"sticky\" src=\"http://".$GLOBALS['domain']."/images/commenticons/bulb_on.png\" />";
                echo "<img alt=\"edit\" src=\"http://".$GLOBALS['domain']."/images/commenticons/comment_edit.png\" />";
            echo "</div>";
            }
    ?>
        </div>
    </div>
    where
    $globals['domain'] = my website url (since i test on my computer @ localhost it was the easiest way i could figure out how to rapdily change links)
    $thread = the name of the page it will be displayed on also the name of the table
    $username = the username of whoever is currently posting a comment
    $text = the text of the comment

    and hopefully the rest is self explanatory, thanks again in advance for the help
    Last edited by garrensilverwing; 07-12-2009 at 12:01 AM.

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: mysql table if not exists

    What's the exact error you're getting? Do you have a URI for a live test page?

    Quote Originally Posted by garrensilverwing View Post
    $globals['domain'] = my website url (since i test on my computer @ localhost it was the easiest way i could figure out how to rapdily change links)
    $_SERVER['SERVER_NAME'] holds the server name, but you don't need even that because you can leave off the URI scheme and hostname:
    HTML Code:
    <div style="position: relative; z-index: 1; float: left;" id="viewer">
        <img alt="reply" src="/images/commenticons/comment_add.png" />
        <img alt="quote" src="/images/commenticons/comment_quote.png" />
        <img alt="report" src="/images/commenticons/comment_warning.png" />
    </div>
    Also, if you're not using variable interpolation, use single quotes. Better would have been to use a multiline string:
    PHP Code:
                echo "<img alt=\"reply\" src=\"http://$_SERVER[SERVER_NAME]/images/commenticons/comment_add.png\" />
        <img alt=\"quote\" src=\"http://
    $_SERVER[SERVER_NAME]/images/commenticons/comment_quote.png\" />
        <img alt=\"report\" src=\"http://
    $_SERVER[SERVER_NAME]/images/commenticons/comment_warning.png\" />
    </div>"

    or switch to PHP only to print the server name:
    PHP Code:
    <div style="position: relative; z-index: 1; float: left;" id="viewer">
        <img alt="reply" src="http://<?php echo $_SERVER[SERVER_NAME]; ?>/images/commenticons/comment_add.png" />
        <img alt="quote" src="http://<?php echo $_SERVER[SERVER_NAME]; ?>/images/commenticons/comment_quote.png" />
        <img alt="report" src="http://<?php echo $_SERVER[SERVER_NAME]; ?>/images/commenticons/comment_warning.png" />
    </div>
    Note: the link in your sig doesn't work. It links to "http://www.brianwallchess.x10hosting.com/", which is a PHP "Domain available!" page.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  3. #3
    garrensilverwing's Avatar
    garrensilverwing is offline x10 Sophmore garrensilverwing is an unknown quantity at this point
    Join Date
    Nov 2008
    Posts
    148

    Re: mysql table if not exists

    hey mission, im getting a mysql error saying that the table already exists when i go to the page
    thanks for the $_server['server_name'] tip i didnt know that

    i tried using the URI scheme way originally but when i called the function up on different pages all the links were broken because it isnt the same directory

    ah using php only to print the server name is a better idea, i'm so inefficient huh lol

    ok i will fix the link in my sig

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: mysql table if not exists

    What's the exact error? Better find the file and line the error occurs at as well.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  5. #5
    garrensilverwing's Avatar
    garrensilverwing is offline x10 Sophmore garrensilverwing is an unknown quantity at this point
    Join Date
    Nov 2008
    Posts
    148

    Re: mysql table if not exists

    all i get where the comments are supposed to be it says
    "Table 'fishingpolefirstblood' already exists" (this is from brianwallchess.net/news/news/fishing_pole_first_blood.php) its not a php error or even a mysql error that is all it says

  6. #6
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: mysql table if not exists

    Quote Originally Posted by garrensilverwing View Post
    all i get where the comments are supposed to be it says
    "Table 'fishingpolefirstblood' already exists" (this is from brianwallchess.net/news/news/fishing_pole_first_blood.php) its not a php error or even a mysql error that is all it says
    If the error message is generated by your script, check the test that the query succeeds; it could be that the query succeeds but success isn't tested properly. If the error message turns out to be from MySQL (via mysql_error), then the table creation query doesn't have an "IF NOT EXISTS" clause, despite the sample code you posted.

    While debugging, you should print the query and the line number where the error is printed.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  7. #7
    garrensilverwing's Avatar
    garrensilverwing is offline x10 Sophmore garrensilverwing is an unknown quantity at this point
    Join Date
    Nov 2008
    Posts
    148

    Re: mysql table if not exists

    well that is the thing, it usually gives me all that information but this time all it says is the table already exists, i went in and played around with it and now it doesnt say anything at all lol...

  8. #8
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: mysql table if not exists

    Sounds like the live script wasn't the current version. Good thing it's working now.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  9. #9
    garrensilverwing's Avatar
    garrensilverwing is offline x10 Sophmore garrensilverwing is an unknown quantity at this point
    Join Date
    Nov 2008
    Posts
    148

    Re: mysql table if not exists

    no its not working, it isnt displaying any of the comments and everything after the comments isnt displaying, including the footer which was always there before :'(

  10. #10
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: mysql table if not exists

    That's probably because the script uses "or die()" for error reporting, which it shouldn't.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. [PHP] MySQL and PHP
    By Bryon in forum Tutorials
    Replies: 43
    Last Post: 03-24-2011, 07:27 AM
  2. Replies: 14
    Last Post: 09-29-2008, 07:07 PM
  3. New Site-Suggestions?
    By mnoutside in forum Review My Site
    Replies: 9
    Last Post: 08-27-2008, 07:01 AM
  4. check if mysql database table exists
    By daileyj2 in forum Programming Help
    Replies: 3
    Last Post: 08-14-2008, 08:21 PM
  5. MysQL Query for Master table & child table
    By phpasks in forum Programming Help
    Replies: 8
    Last Post: 08-07-2008, 08:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers