+ Reply to Thread
Results 1 to 7 of 7

Thread: Need php help

  1. #1
    thenewprogrammer is offline x10Hosting Member thenewprogrammer is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    45

    Need php help

    trying to grab data from table and than echo it out in profile but its not working or if anyone has better profile way they like to help me with

    Code:
     
    <? 
    $user = $_GET['user']; 
    session_start(); 
     
    if(isset($_SESSION['user'])){ 
    } else { 
    echo " 
    <script language='javascript'> 
    alert('Sorry, but you must login to view the Profile area!');
    </script> 
    <script> 
    window.location='http://thenewprogrammer.x10hosting.com'
    </script> 
    "; } 
     
    
    $dbhost = "localhost"; 
    $dbname = "*****"; 
    $dbuser = "*****"; 
    $dbpass = "*****"; 
    mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error()); 
    mysql_select_db($dbname) or die(mysql_error());
    $query="SELECT * FROM login WHERE username='$username'";
    $result=mysql_query($query);
    $row = mysql_fetch_array ( $result );
    print_r($row);
    ?>
    <html>
    <head>
    User Profile
    </head>
    <body>
    <? echo $username ?>
    </body>
    </html>

  2. #2
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Re: Need php help

    What happens when you run the script? Error, blank page, ect.

  3. #3
    thenewprogrammer is offline x10Hosting Member thenewprogrammer is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    45

    Re: Need php help

    Quote Originally Posted by Twinkie View Post
    What happens when you run the script? Error, blank page, ect.
    it says User Profile like i have in head. When not loged in the java script works and redirects me to main page so nothing wrong with that but i need to get data from a table for a profile. But nothing happens when i echo it out.
    Last edited by thenewprogrammer; 07-27-2009 at 11:47 PM.

  4. #4
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Re: Need php help

    If you are getting no errors, then that means that there is no entry in the database for that username. In this case, you used the wrong variable so the query came back true, with no rows. Look into error handling with the mysql_num_rows function.

    The problem is that you put the $_GET data into a variable named user, then reference it in a variable called username. Simply replace it with $username = $_GET['user'];

    Another thing I recommend is using the mysqli_* family of functions, which allows multiple database connections referenced from a resource variable (Ex. $con = mysqli_connect(...)).
    Last edited by Twinkie; 07-28-2009 at 01:27 AM.

  5. #5
    thenewprogrammer is offline x10Hosting Member thenewprogrammer is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    45

    Re: Need php help

    hmm still not working, replacecd it with username to.

  6. #6
    nexhunter's Avatar
    nexhunter is offline x10 Sophmore nexhunter is an unknown quantity at this point
    Join Date
    Nov 2007
    Location
    millbury,massachusetts
    Posts
    239

    Re: Need php help

    PHP Code:
    $num_rows mysql_num_rows($result);
    if(empty(
    $num_rows)){
    echo 
    'Sorry this user does not exist.';
    }
    else{
    $row mysql_fetch_array($result);
    print_r($row);

    try using mysql_num_rows as twinkie mentioned to see if your pulling any data, which then can help track down the problem if nothing is being pulled.
    Last edited by nexhunter; 07-28-2009 at 06:41 AM.



  7. #7
    zapzack is offline x10 Elder zapzack is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    606

    Re: Need php help

    I dont advise the use of javascript.. I suggest using the php header function..

    Code:
    header("Location: http://google.com");
    Try this:

    Code:
    <? 
    $user = $_GET['user']; 
    session_start(); 
     
    if(!isset($_SESSION['user'])){ 
    header("Location: http://thenewprogrammer.x10hosting.com");
    } 
    
    $dbhost = "localhost"; 
    $dbname = "*****"; 
    $dbuser = "*****"; 
    $dbpass = "*****"; 
    
    mysql_connect($dbhost, $dbuser, $dbpass) or die("Could not connect: ".mysql_error()); 
    mysql_select_db($dbname) or die("Could not select the database: ".mysql_error());
    $username = mysql_real_escape_string($user);
    $query = "SELECT * FROM login WHERE username ='".$username."'";
    $result = mysql_query($query);
    $row = mysql_fetch_array($result);
    ?>
    <html>
    <head>
    User Profile
    </head>
    <body>
    <?php echo($username); ?>
    <br />
    <?php print_r($row); ?>
    </body>
    </html>
    I'm not sure if it will work.. I usually do trial and error.. I stripped characters to prevent SQL Injection btw.. heh
    Last edited by zapzack; 07-28-2009 at 01:43 PM.

+ Reply to Thread

Similar Threads

  1. Ever Been Suspended For Using PHP?
    By dragoneye_xp in forum Off Topic
    Replies: 26
    Last Post: 08-16-2009, 07:17 PM
  2. [PHP] Variables in PHP
    By Bryon in forum Tutorials
    Replies: 15
    Last Post: 01-29-2009, 09:46 AM
  3. currently have an application pending php
    By biomasti in forum Free Hosting
    Replies: 1
    Last Post: 09-03-2008, 01:58 PM
  4. php errors galore
    By DMG Online in forum Scripts & 3rd Party Apps
    Replies: 9
    Last Post: 05-17-2008, 06:23 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