PHP newbie - help!

Discussion in 'PHP' started by oswaldpeters, Feb 4, 2011.

  1. #1
    Hi there chaps.

    I've decided to learn PHP - I can already edit it pretty well from making wordpress themes / alterations.

    I'm stuck with a piece of sample coding. I've got a centos box setup with PHP (latest) and mysql with apache installed.

    If I try the standard 'Hello world' script it works fine.

    But if I try this:

    <html>
    <head>
    <title>Sample Connection</title>
    </head>
    <body><br><br>
    <div align="center">
    <?php
      $db = mysql_pconnect("localhost", "root", 
    "password");
    
      if (!db)
    
      {
      print "Error: Could not connect to database.";
      exit;
    }
    
      mysql_select_db("programming");
      $query = "select * from languages order by name 
    asc";
      $result = mysql_query($query);
      $num_results = mysql_num_rows($result);
    
      print "Today's web developers should be familiar 
    with<br><br>";
    
      for ($i=0; $i<$num_results; $i++) {
      $row = mysql_fetch_array($result);
      print ($row["name"]);
      print " on ";
      print ($row["platform"]);
      print "<br>";
    }
    
    ?>
    </div>
    </body>
    </html>
    Code (markup):
    It displays the page title in the header but doesn't do anything from then on. It just returns a blank page. I've checked and double checked the mysql details (obviously in the piece of code I'm using I've changed it to suit my setup).

    Any help?!
     
    oswaldpeters, Feb 4, 2011 IP
  2. srisen2

    srisen2 Peon

    Messages:
    359
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try it like this

    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");



    $query = "select * from languages order by name
    asc'";
    $result = mysql_query ($query);
    if ($result) {
    while($row=mysql_fetch_array($result))
    {
    $name = $row["name"];
    $platform = $row["platform"];

    echo "$name on $platform";
    }
    }
     
    srisen2, Feb 4, 2011 IP
  3. oswaldpeters

    oswaldpeters Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Nope - didn't work. Just a blank page.

    Must be a server issue.
     
    oswaldpeters, Feb 4, 2011 IP
  4. jkl6

    jkl6 Peon

    Messages:
    70
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Maybe your php.ini file is set to not display PHP errors, that's why the page is blank. Check that.
     
    jkl6, Feb 4, 2011 IP
    jitendraag likes this.
  5. oswaldpeters

    oswaldpeters Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ok, sorted that. error displayed: Fatal error: Call to undefined function mysql_pconnect() in /var/www/html/index.php on line 8
     
    oswaldpeters, Feb 4, 2011 IP
  6. jkl6

    jkl6 Peon

    Messages:
    70
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Replace mysql_pconnect() with mysql_connect()
     
    jkl6, Feb 4, 2011 IP
  7. srisen2

    srisen2 Peon

    Messages:
    359
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    there you go that sounds like your solution
     
    srisen2, Feb 4, 2011 IP
  8. oswaldpeters

    oswaldpeters Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    It's still spewing out the same error, chaps. Just without the 'p' prefix.
     
    oswaldpeters, Feb 4, 2011 IP
  9. jhine

    jhine Active Member

    Messages:
    25
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    53
    #9
    You haven't loaded the PHP MySQL extension in your PHP.INI file.

    Edit: With your OS you'll have to install the MYSQL extension, I have only experienced on Windows and Ubuntu/Debian. I believe the package is php-mysql and mysql-server.
     
    Last edited: Feb 4, 2011
    jhine, Feb 4, 2011 IP
  10. srisen2

    srisen2 Peon

    Messages:
    359
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #10
    you can test your mysql connection by creating a info.php file.

    As long as you have php loading create a file with

    <?php

    phpinfo();

    ?>

    this will display all your php information and show you is you have mysql working with your php.
     
    srisen2, Feb 7, 2011 IP