1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Fetch page text from DB

Discussion in 'PHP' started by RogueLayered, Apr 8, 2012.

  1. #1
    I'm coding a site right now, I'm new to php so obviously I've ran into some troubles. I am using the database for all the page's text "e.g. Welcome to my site" all text instead of it physically being in the php files. My question is how do you have the php file fetch the information for the DB? Here's the code I have so far and I continue to run into errors:

    
    <?
    include("dbinfo.inc.php");
    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    $query="SELECT * FROM info";
    
    
    mysql_close();
    
    
    mysql_result($result,"text");
    
    
    echo "$text";
    ?>
    
    Code (markup):
    My database has 2 columns, an id column which is set as the auto_increment and the text column. Is it possible just to have the file select which text to display by just using the id column?
     
    RogueLayered, Apr 8, 2012 IP
  2. e-abi

    e-abi Member

    Messages:
    122
    Likes Received:
    1
    Best Answers:
    3
    Trophy Points:
    38
    #2
    <?
    include("dbinfo.inc.php");
    $db = mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    $query="SELECT * FROM info";
    
    $queryResult = mysql_query($query,$db);
    
    while($myrow = mysql_fetch_array($queryResult)) {
         echo $myRow['text'];  //if the column name is text, if it is not, then change it.
    }
    
    
    mysql_close();
    
    
    
    
    ?>
    PHP:
    Haven't tested, but in theory it should work.
     
    e-abi, Apr 9, 2012 IP
  3. RogueLayered

    RogueLayered Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It ran the:
    
    
    [B]Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in [B]/home/rogue/public_html/site/index.php on line [B]12
    
    [B]Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in [B]/home/rogue/public_html/site/index.php on line [B]14
    [/B][/B][/B]
    Code (markup):
    [/B][/B][/B]
     
    RogueLayered, Apr 9, 2012 IP
  4. e-abi

    e-abi Member

    Messages:
    122
    Likes Received:
    1
    Best Answers:
    3
    Trophy Points:
    38
    #4
    Ok, did not check the code:


    $db = mysql_connect('localhost',$username,$password);
    
    
    PHP:
    In short mysql connect failed and that is why the latest errors occurred. I presume you want to connect to localhost, and it needs to be surrounded with quotes.
    You can always use this statement:
    echo '<pre>'.htmlspecialchars(print_r($db, true)).'</pre>';

    Right after the $db = mysql .... line
    This statement prints out the contents of the $db variable. To check if it says something like "Resource #xx" if it doesn't then database connection itself if not correct.

    You can print out contents of any variable with the same statement.
     
    e-abi, Apr 9, 2012 IP
  5. RogueLayered

    RogueLayered Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Now it comes up blank, I'm not sure why. Perhaps I should use a backend.php or something and just use $text1, $headertext, etc. instead of taking the information from the db. I figured it would be easier to just edit the text later on by just going into the db and editing the column however I have been reading up on this and apparently using the php files is better than just relaying on the server for all the information. What's your opinion?
     
    RogueLayered, Apr 9, 2012 IP
  6. e-abi

    e-abi Member

    Messages:
    122
    Likes Received:
    1
    Best Answers:
    3
    Trophy Points:
    38
    #6
    put this line as the first line of code in your script:

    ini_set('display_errors', 1);
    error_reporting(E_ALL);

    and then remove the @ sign from the mysql_select_db row. (This @ sign tells to hide the error, if database selection fails).

    I do believe that holding information itself in database is better, since then they can be edited.
    But short knowledge of php and mysql can lead to nasty security flaws, which in the easiest cases ends with your site hacked.

    You could try out this tutorial:
    http://php.about.com/od/phpwithmysql/ss/mysql_php.htm
    PHP:
    At least it explains, what each row does (this is very crucial).
     
    e-abi, Apr 9, 2012 IP