need help with this select code

Discussion in 'PHP' started by dougvcd, Jul 10, 2008.

  1. #1
    got this code from internet and is suppose to show records from database
    but all i get is a white blank screen if any one can help
    cheers
    Doug
    
    <?
    error_reporting(E_ALL);
    // Connect database 
    dbh=mysql_connect("localhost", "car", "57") or die('I cannot connect to database because: ' .mysql_error()) ; 
    mysql_select_db("users"); 
    
    // Get all records in all columns from table and put it in $result.
    $result=mysql_query("select * from phonebook");
    
    while($row=mysql_fetch_assoc($result)){
    
    // Output 
    Echo "ID : $row['id'] <br/>"; 
    Echo "Name : $row['name'] <br/>";
    Echo "Email : $row['email'] <br/>";
    Echo "Tel : $row['tel'] <hr>";
    
    // Add a link with a parameter(id) and it's value.
    Echo '<a href="update.php?id='.$row['id'].'">Update</a>';
    
    }
    
    mysql_close();
    ?> 
    PHP:

     
    dougvcd, Jul 10, 2008 IP
  2. JLEville

    JLEville Peon

    Messages:
    147
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Did you totally copy this code or edit it for your own database?
     
    JLEville, Jul 10, 2008 IP
  3. revvi

    revvi Peon

    Messages:
    58
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi,

    It may be some missing letters and case sensitive issues.
    Please have a look at my code:

    
    <?php
    error_reporting(E_ALL); 
    // Connect database 
    $dbh=mysql_connect("localhost", "car", "57") or die('I cannot connect to database because: ' .mysql_error()) ; 
    mysql_select_db("users"); 
    // Get all records in all columns from table and put it in $result.$result=mysql_query("select * from phonebook");
    while($row=mysql_fetch_assoc($result))
    {
    // Output Echo "ID : $row['id'] <br/>"; 
    echo "Name : $row['name'] <br/>";
    echo "Email : $row['email'] <br/>";
    echo "Tel : $row['tel'] <hr>";
    // Add a link with a parameter(id) and it's value.
    echo '<a href="update.php?id='.$row['id'].'">Update</a>';
    }
    mysql_close($dbh);
    ?>
    
    PHP:
     
    revvi, Jul 10, 2008 IP
  4. dougvcd

    dougvcd Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thanks but still the same
    cheers
    doug
     
    dougvcd, Jul 10, 2008 IP
  5. Dondon2d

    Dondon2d Peon

    Messages:
    3,193
    Likes Received:
    146
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Did you change the variables to match yours? Username, password, table name, field name, etc.
     
    Dondon2d, Jul 10, 2008 IP
  6. dougvcd

    dougvcd Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    no left them as is
    was going to get it to work
    then adapt it
    cheers
    doug
     
    dougvcd, Jul 10, 2008 IP
  7. revvi

    revvi Peon

    Messages:
    58
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    What kind of web server are you using (xampp, etc)?
    And what is your OS (Windows, Mac, Linux)?

    Perhaps there is something wrong with the configuration.
     
    revvi, Jul 10, 2008 IP
  8. dougvcd

    dougvcd Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    os is windows xp
    and server says linux
    company is 3ix
    cheers
    Doug
     
    dougvcd, Jul 10, 2008 IP
  9. dougvcd

    dougvcd Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    this code work for display on screen
    //Retrieves data from MySQL
      $data = mysql_query("SELECT * FROM members WHERE region='exchange' OR region='EXCHANGE/HIRE' or region='EXCHANGE' or region='HIRE'") or die(mysql_error());
      if(mysql_num_rows( $data ) == 0) {
         echo 'No Results';
     } else {
     	 
    //Puts it into an array 
    $i=1;  
    echo "<table>";
    while($info = mysql_fetch_array( $data ))   { 
    
    echo "<tr>
              <td>$i</td>
              <td>". $info['name'] ."</td>
    		  <td>". $info['username'] ."</td>
              <td>". $info['email'] ."</td>
       </tr>";
     $i++;
    }
    echo "</table>";  
    }
    
    ?> 
    PHP:
    but hasnt got the update on it
    echo '<a href="update.php?id='.$row['id'].'">Update</a>';
    how do i code this line to fit in with above
     
    dougvcd, Jul 10, 2008 IP
  10. revvi

    revvi Peon

    Messages:
    58
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    you can do like this:

    //Retrieves data from MySQL
      $data = mysql_query("SELECT * FROM members WHERE region='exchange' OR region='EXCHANGE/HIRE' or region='EXCHANGE' or region='HIRE'") or die(mysql_error());
      if(mysql_num_rows( $data ) == 0) {
         echo 'No Results';
     } else {
         
    //Puts it into an array 
    $i=1;  
    echo "<table>";
    while($info = mysql_fetch_array( $data ))   { 
    
    echo "<tr>
              <td>$i</td>
              <td>". $info['name'] ."</td>
              <td>". $info['username'] ."</td>
              <td>". $info['email'] ."</td>";
    
    echo '<td><a href="update.php?id='.$row['id'].'">Update</a></td>';
    
    echo "</tr>";
     $i++;
    }
    echo "</table>";  
    }
    PHP:
     
    revvi, Jul 10, 2008 IP
  11. dougvcd

    dougvcd Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #11
    cheers buddy that seems to work at the moment
    will let you know
    Doug
     
    dougvcd, Jul 10, 2008 IP