php/mysql question

Discussion in 'PHP' started by GPGrieco, Nov 28, 2007.

  1. #1
    hello, I am having some trobble with a short script I made. I am trying to call some text from a database. I need to call "fulltext" when "ID" is 1. Here is what I have

    
    <?php
    
    $con = mysql_connect("localhost","xxxxxx","xxxxxx");
    
    if (!$con)
    
      {
    
      die('Could not connect: ' . mysql_error());
    
      }
    
    
    mysql_select_db("xxxxx", $con);
    
    
    
    $result = mysql_query("SELECT * FROM xxxxx
    
    WHERE ID='1'");
    
    while($row = mysql_fetch_array($result))
    
      {
    
      echo $row['fulltext '];
    
      }
    
    mysql_close($con);
    
    
    
    ?>
    
    Code (markup):
    Dose anyone see anything wrong?
     
    GPGrieco, Nov 28, 2007 IP
  2. theOtherOne

    theOtherOne Well-Known Member

    Messages:
    112
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    108
    #2
    yeah...
      echo $row['fulltext '];
    PHP:
    there might be a whitespace too much in text ']

    removing that space it should work
     
    theOtherOne, Nov 28, 2007 IP
  3. cheerio

    cheerio Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    TheOtherOne is right you seem to have some whitespace there.

    Also try changing WHERE ID='1' to WHERE `ID`='1'
     
    cheerio, Nov 28, 2007 IP
  4. imvain2

    imvain2 Peon

    Messages:
    218
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Also, even though this won't affect the end result, its always best to "ask" for just the fields that you want, instead of pulling every single field in your table.

    in other words use:

    SELECT FullText FROM xxxxx WHERE ID='1'

    instead of

    SELECT * FROM xxxxx WHERE ID='1'

    and if ID is an integer, wouldn't you leave out the apostrophes?
     
    imvain2, Nov 28, 2007 IP
  5. GPGrieco

    GPGrieco Peon

    Messages:
    102
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ok, now I am getting a diffrent error. realtyworlddeserthomes.com/test2.php

    Now there is a diffrent error. What is it now?

    (I took the white space out of fulltext
     
    GPGrieco, Nov 28, 2007 IP
  6. imvain2

    imvain2 Peon

    Messages:
    218
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I don't know if it was a problem when you copied over or what, but try this change. Put your mysql query on one line

    From:
    $result = mysql_query("SELECT * FROM xxxxx

    WHERE ID='1'");
    to
    $result = mysql_query("SELECT * FROM xxxxx WHERE ID='1'");
     
    imvain2, Nov 28, 2007 IP
  7. GPGrieco

    GPGrieco Peon

    Messages:
    102
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I put it on one line, but it still doesen't work.

    Here is what I have now:

    
    <?php
    $con = mysql_connect("xxxxxx","xxxxx","xxxxxx");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    
    
    
    
    mysql_select_db("xxxxx", $con);
    
    $result = mysql_query("SELECT * FROM xxxxx WHERE `ID`='1');
    
    
    while($row = mysql_fetch_array($result))
      {
      echo $row['fulltext'];
      }
    
    
    mysql_close($con);
    
    ?>
    
    Code (markup):
    It seems like I never get it right, even though I copied it from a site that shows you php/mysql (w2schools). I just copied it, and put my info in it instead of their stuff.
     
    GPGrieco, Nov 28, 2007 IP
  8. dannet

    dannet Well-Known Member

    Messages:
    864
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    153
    #8
    You can try replacing:

    $result = mysql_query("SELECT * FROM xxxxx WHERE `ID`='1');

    with:

    $result = mysql_query("SELECT * FROM xxxxx WHERE ID='1'");
     
    dannet, Nov 28, 2007 IP
  9. GPGrieco

    GPGrieco Peon

    Messages:
    102
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    ok, took out the quotes, and nothing happend. Then I put the dobble quote at the end, and it gave me an error that there was a problem with that line of code. Maby this would be eaiser. Could someone re-write this block of code? Its not that long and it would really help. I need to take fulltext with ID of one and display it.

    Thanks in advanced.
     
    GPGrieco, Nov 28, 2007 IP
  10. imvain2

    imvain2 Peon

    Messages:
    218
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Ahh, sometimes I forget to use hakim's razor.

    Try renaming the fulltext field to something else. it looks like fulltext actually means something in mysQL

    http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html

    Here is my code that I used and that works. You will notice my code is more complicated. That is because I like to use functions for tasks that I will use a lot. Normally, my vars and my function will be store in an include file and included on every page.

    
        $GLOBALS["mysql_server"] = "localhost";
        $GLOBALS["mysql_username"] = "test";
        $GLOBALS["mysql_password"] = "test";
        $GLOBALS["mysql_db"] = "temptb";
    
        function OpenDB()
        {
            global $link;
            $link=mysql_connect($GLOBALS["mysql_server"],$GLOBALS["mysql_username"],$GLOBALS["mysql_password"]);
            if(!$link){die("Could not connect to MySQL");}
            mysql_select_db($GLOBALS["mysql_db"],$link) or die ("could not open db".mysql_error());
        }
    
        OpenDB();
    
        $sql = "select temptext from temptable where ID = 1;";
        $result = mysql_query($sql);
        echo mysql_error();
        while($row = mysql_fetch_array($result))
              {
                  $fulltext = $row["temptext"];
                  echo $fulltext;
              }
            
        mysql_close($link);
    
    PHP:
     
    imvain2, Nov 28, 2007 IP
  11. GPGrieco

    GPGrieco Peon

    Messages:
    102
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Well, the reason I am using fulltext is because I am using, my html template, and using a joomla admin panel, I was just going to call the id of that page, and have it display the text (fulltext). So without re-building the database and recoding the admin panel, I can't do that. It must work since it works with joomla with that name. I am getting diffrent errors now with the script you just provided me with. again the site is realtyworlddeserthomes.com/test2.php.
     
    GPGrieco, Nov 29, 2007 IP
  12. dannet

    dannet Well-Known Member

    Messages:
    864
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    153
    #12
    Change where ID = 1' to where ID='1'...

    Edit: If you are using the imvain2 script, remove the ; after 1 in the line:

    $sql = "select temptext from temptable where ID = 1;";
    Code (markup):
     
    dannet, Nov 29, 2007 IP
  13. imvain2

    imvain2 Peon

    Messages:
    218
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #13
    The semi colon is valid SQL statement.

    GPG, try this. Echo out your SQL command before it is actually being ran. Then copy and paste it in here, because without seeing the complete SQL command, its hard to determine what the actual problem is. Right now its just educated guesses.
     
    imvain2, Nov 29, 2007 IP
  14. GPGrieco

    GPGrieco Peon

    Messages:
    102
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    well, I'm not that good with php/mysql, so I am not exactally sure what you mean. I think I am just going to give up on this script. Thank you for your help. I'll let you know if I have any more questions.
     
    GPGrieco, Nov 30, 2007 IP
  15. imvain2

    imvain2 Peon

    Messages:
    218
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #15
    basically I was wanting to see what the exact sql command was being ran, as mySQL was seeing it.

    right before
    $result = mysql_query($sql);

    add
    echo $sql;
     
    imvain2, Nov 30, 2007 IP