Results 1 to 4 of 4

Thread: Connect Select Echo

  1. #1
    Join Date
    Mar 2009
    Location
    NJ, USA
    Posts
    32
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Connect Select Echo

    My goal with this site is to display a profile which members create to users outside of my login system. My file system has the main folder and within it is the login(control panel) folder, and a folder for each member. When the member registers (through the control panel) it creates a member folder that shares a name with the 'sub_domain' field in their database. When you go to the profile page in the members folder PHP looks at the containing directory and compares it to the 'sub_domain' field. It then displays their profile. As of now I receive no errors, however only the '$prac_dir' string displays in the echoes. Here is my code:

    Code:
    <?php
    $prac_dir = basename(dirname(__FILE__));
    require_once ('/home/myname/mysite.com/login/includes/config.inc.php');
    
    require_once (MYSQL);
    $q = "SELECT 'professional','private', 'contact' FROM users WHERE 'sub_domain' = '$prac_dir'";
    $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
    $_display = mysqli_fetch_array ($r, MYSQLI_ASSOC);
    
    echo "$_display[private]";
    echo "$prac_dir";
    echo "$_display[professional]"; 
    echo "$_display[contact]";
    ?>
    I assume that the reason the $_display[] array does not echo is because it will not properly connect to my database... but then it would display an error.

    Code explanation:

    This gets the name of the containing directory
    $prac_dir = basename(dirname(__FILE__));

    This gets the config file which associates 'MYSQL' with the mysql_connect file
    require_once ('/home/myname/mysite.com/login/includes/config.inc.php');

    This uses the value from the previous line
    require_once (MYSQL);

    These Select the fields from the database where 'sub-domain' field equals the containing directory. Then it puts them in an array.
    Code:
    q = "SELECT 'professional','private', 'contact' FROM users WHERE 'sub_domain' = '$prac_dir'";
    $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
    $_display = mysqli_fetch_array ($r, MYSQLI_ASSOC);
    Echo the array fields and the string from line one:
    Code:
    echo "$_display[private]";
    echo "$prac_dir";
    echo "$_display[professional]"; 
    echo "$_display[contact]";
    I will be playing around with the code for a half hour or so before my next class. After my class I'll come back and see if I can figure out my error, but right now I'm fairly stumped(I cant seem to find my error). If I figure it out I'll post asap. Any advice would be wonderful.

  2. #2
    Join Date
    Mar 2009
    Location
    NJ, USA
    Posts
    32
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    Just an update:

    I tried to troubleshoot by putting the connection information inside the same file. Same problem. Heres the test code:

    Code:
    <?php
    $prac_dir = basename(dirname(__FILE__));
    
    DEFINE ('DB_USER', 'user');
    DEFINE ('DB_PASSWORD', 'password');
    DEFINE ('DB_HOST', 'prac.mysite.com');
    DEFINE ('DB_NAME', 'databasename');
    
    // Make the connection:
    $dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    
    if (!$dbc) {
    	trigger_error ('Could not connect to MySQL: ' . mysqli_connect_error() );
    }
    
    $q = "SELECT 'professional','private', 'contact' FROM users WHERE 'sub_domain' = '$prac_dir'";
    $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
    $_display = mysqli_fetch_array ($r, MYSQLI_ASSOC);
    
    echo "$_display[private]";
    echo "$prac_dir";
    echo "$_display[professional]"; 
    echo "$_display[contact]";
    ?>

  3. #3
    Join Date
    Sep 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    The array is formatted like this:



    Array

    (
    [0] => stdClass Object
    (
    [text] => this is my tribe http://www.sfondideldesktop.com/Imag...king-Water.Jpg
    [to_user_id] =>
    [from_user] => laurenestelle
    [id] => 1464496735
    [from_user_id] => 5519148
    [iso_language_code] => en
    [source] => &lt;a href=&quot;http://twitter.com/&quot;&gt;web&lt;/a&gt;
    [profile_image_url] => http://s3.amazonaws.com/twitter_prod...100_normal.jpg
    [created_at] => Mon, 06 Apr 2009 18:37:14 +0000
    )

    [1] => stdClass Object
    (
    [text] => man, sara is stubborn, she is still fighting her nap after a diaper change and being put back in bed with all her stuffed animals and bl ...
    [to_user_id] =>
    [from_user] => jenseibel
    [id] => 1464495282
    [from_user_id] => 2231331
    [iso_language_code] => en
    [source] => &lt;a href=&quot;http://www.friendbar.com&quot;&gt;Friendbar&lt;/a&gt;
    [profile_image_url] => http://s3.amazonaws.com/twitter_prod...jen_normal.JPG
    [created_at] => Mon, 06 Apr 2009 18:36:57 +0000
    )

    [2] => stdClass Object
    (
    [text] => @kraupu ftw, kraupuk, ftw! http://septyni.tumblr.com/post/90047...er-film-school
    [to_user_id] => 55168
    [to_user] => kraupu
    [from_user] => septyni
    [id] => 1464489944
    [from_user_id] => 2950730
    [iso_language_code] => is
    [source] => &lt;a href=&quot;http://twitter.com/&quot;&gt;web&lt;/a&gt;
    [profile_image_url] => http://s3.amazonaws.com/twitter_prod...nas_normal.jpg
    [created_at] => Mon, 06 Apr 2009 18:36:02 +0000
    )

    Ares Mobiles Internet Granite Tile
    Last edited by Roses.Are.Green; 10-18-2009 at 05:31 PM.

  4. #4
    Join Date
    Apr 2006
    Posts
    190
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default

    Quote Originally Posted by AdrielGreene View Post
    Just an update:

    I tried to troubleshoot by putting the connection information inside the same file. Same problem. Heres the test code:

    Code:
    <?php
    $prac_dir = basename(dirname(__FILE__));
    
    DEFINE ('DB_USER', 'user');
    DEFINE ('DB_PASSWORD', 'password');
    DEFINE ('DB_HOST', 'prac.mysite.com');
    DEFINE ('DB_NAME', 'databasename');
    
    // Make the connection:
    $dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    
    if (!$dbc) {
    	trigger_error ('Could not connect to MySQL: ' . mysqli_connect_error() );
    }
    
    $q = "SELECT 'professional','private', 'contact' FROM users WHERE 'sub_domain' = '$prac_dir'";
    $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
    $_display = mysqli_fetch_array ($r, MYSQLI_ASSOC);
    
    echo "$_display[private]";
    echo "$prac_dir";
    echo "$_display[professional]"; 
    echo "$_display[contact]";
    ?>
    Your problem is this line:
    Code:
    $q = "SELECT 'professional','private', 'contact' FROM users WHERE 'sub_domain' = '$prac_dir';
    you can not use quotes to define each field name. It needs to be this:
    Code:
    $q = "SELECT `professional`,`private`, `contact` FROM users WHERE `sub_domain` = $prac_dir;
    Ryan
    Sevierville, TN

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •