Log in

View Full Version : Connect Select Echo



AdrielGreene
04-24-2009, 05:03 PM
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:


<?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.

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:

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.

AdrielGreene
04-24-2009, 08:46 PM
Just an update:

I tried to troubleshoot by putting the connection information inside the same file. Same problem. Heres the test 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]";
?>

Roses.Are.Green
10-09-2009, 11:33 AM
The array is formatted like this:



Array

(
[0] => stdClass Object
(
[text] => this is my tribe http://www.sfondideldesktop.com/Images-Animals/Deers/Whitetail-Deer-B-Female-Drinking-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_production/profile_images/115481833/Photo_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_production/profile_images/70035203/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/90047077/dancing-animals-in-love-vancouver-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_production/profile_images/96311877/7enas_normal.jpg
[created_at] => Mon, 06 Apr 2009 18:36:02 +0000
)

Ares (http://www.aresvista.com/) Mobiles Internet (http://www.mobiles-internet-24.de/) Granite Tile (http://www.bathandgranite4less.com/)

NXArmada
10-09-2009, 06:59 PM
Just an update:

I tried to troubleshoot by putting the connection information inside the same file. Same problem. Heres the test 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:

$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:

$q = "SELECT `professional`,`private`, `contact` FROM users WHERE `sub_domain` = $prac_dir;