MySQL/PHP ? Trying to fetch

Discussion in 'PHP' started by bdunndchi, Jul 7, 2009.

  1. #1
    Database table name is 'users'...
    Database set up as MySQL 4.0...
    User enters their info on a different page in a form and puts it into the db (this part works fine)...

    On the My Account page when I'm trying to echo out the details that they entered into the form, I get the "couldn't execute query" error. I have no idea what the problem is.

    Here is my code:

    <?php
    session_start();
    if (!isset($_SESSION['user']))
    {
    die ("Access Denied");
    }
    //Database Code
    $host = "localhost"; //Host Name
    $dbuser = "**********"; //User Name
    $password = "******"; //Password
    $dbname = "********"; //Database Name
    $cxn = mysql_connect("$host","$dbuser","$password","$dbname")
    or die ("Couldn't connect to server.");

    $useit = $_SESSION['user'];

    $query = "SELECT * FROM users WHERE user_email=$useit";
    $result = mysql_query($cxn,$query)
    or die ("Couldn't execute query");
    $row = mysql_fetch_assoc($result);
    extract($row);

    ?>
    <h2>My Account </h2>
    <?php if (isset($_SESSION['user'])) { ?>
    <p>Logged as <?php echo $_SESSION['user']; ?> | <a href="settings.php">Settings</a>
    | <a href="logout.php">Logout</a> </p>


    <p>Country: <?php echo $country; ?>
    </p>

    <?php } ?>



    Anyone that could give me any tips would be greatly appreciated. Thanks.
     
    bdunndchi, Jul 7, 2009 IP
  2. Nick Nitro

    Nick Nitro Greenhorn

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #2
    Hi, I think that you need to change this lines

    
    $cxn = mysql_connect("$host","$dbuser","$password","$dbname")
    or die ("Couldn't connect to server.");
    Code (markup):
    to

    
    $cxn = mysql_connect ($host, $dbuser, $password) or die .('I cannot connect to the database because: ' . 
    		mysql_error());
    mysql_select_db ($dbname); 
    Code (markup):
     
    Nick Nitro, Jul 8, 2009 IP
  3. bird.23

    bird.23 Peon

    Messages:
    13
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Try This Code :

     
    bird.23, Jul 8, 2009 IP
  4. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yeh you need to select a database

    and also shouldn't

    $result = mysql_query($cxn,$query)
    be
    $result = mysql_query($query,$cxn)
     
    wd_2k6, Jul 8, 2009 IP
  5. anthonywebs

    anthonywebs Banned

    Messages:
    657
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #5
    $cxn = mysql_connect("$host","$dbuser","$password","$dbname")
    or die ("Couldn't connect to server.");
    PHP:
    change the cxn to that
     
    anthonywebs, Jul 8, 2009 IP
  6. rsmehdihasan

    rsmehdihasan Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    $cxn = mysql_connect($host,$dbuser,$password);
    or die ("Couldn't connect to server.");
    mysql_select_db($dbname );
     
    rsmehdihasan, Jul 8, 2009 IP