SitePoint Sponsor |
|
User Tag List
Results 1 to 5 of 5
Thread: php variables within mysql query
-
Jan 15, 2002, 05:43 #1
- Join Date
- Jun 2001
- Location
- vancouver.bc.ca
- Posts
- 96
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
php variables within mysql query
is it possible to make a mysql query dynamic in that it will only look for what the refering page tells it to? i imagine it should, but i can't get it to work.
submitted for your approval:
$clientLogin is a single textbox that is passed to clientLogin.php via index.php and is then broken into two parts. i havn't really tested this part yet, but to the untrained eye it looks like it'd work fine. the problem is how below in the query i want to dynamically insert the info passed to this page. to test i have two entries in my database, and if i manually put in my search criteria it will work fine, but that's as far as i've gotten.
PHP Code:// login info broken down into useful chunks, the user and their domain
$clientLogin = strtolower($clientLogin);
$clientLoginArray = explode(".", $clientLogin);
$clientLoginArray[0] = $clientUser;
$clientDomain = $clientLoginArray[1] . $clientLoginArray[2];
$clientUser = trim($clientUser);
$clientDomain = trim($clientDomain);
// database connetion variables
$host = "localhost";
$user = "root";
// $password = "";
$dbName = "clientData";
$dbTable = "clientInfo";
$link = mysql_connect ($host, $user, $password);
$query = "SELECT * from $dbTable WHERE user = '$clientUser' AND domain = '$clientDomain'";
$result = mysql_db_query($dbName, $query, $link);
while ($row = mysql_fetch_array($result)) {
print("$row[user]<br>");
print("$row[domain]<br>");
}
mysql_close ($link)
it's amazing what velocity can do
when human beings are in season
-
Jan 15, 2002, 06:13 #2
Your SQL query should work! A good way of verifying if it will work is to put an echo statement after the query and see what the SQL looks like. You might want to comment out the result part temproarily.
Also, posting the error message you get might help.Last edited by Husain; Jan 15, 2002 at 06:15.
-
Jan 15, 2002, 06:15 #3
Try:
PHP Code:$query = "SELECT * from ".$dbTable." WHERE user = '$clientUser' AND domain = '$clientDomain'";
Harry Potter
-- You lived inside my world so softly
-- Protected only by the kindness of your nature
-
Jan 15, 2002, 22:48 #4
- Join Date
- Jun 2001
- Location
- vancouver.bc.ca
- Posts
- 96
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
thanks for the suggestions.
after posting this last night i managed to get rid of the error (slight sytaxual error, my bad), however, all is still not well. both my and seanf's query return the same results. that is, nothing. no errors, mind you, just nothing. if i hardcode in the WHERE/AND statements it works fine, but this is no real solution. i might as well use html. i tried the echo statement as husain suggested and i get "Resource id #2" printed out. now, table clientInfo has only two rows, user and domain. should i add a primary key id row? i'll give this a whirl anyway, but for my needs it's not necessary.it's amazing what velocity can do
when human beings are in season
-
Jan 16, 2002, 03:51 #5
- Join Date
- Jun 2001
- Location
- vancouver.bc.ca
- Posts
- 96
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
i am quite pleased with myself. mostly, anyway. as it turns out there was some stupid error in the processing of the $clientLogin variable. if you look at the way i handle $clientUser you'll see i've got the expression set up backwards. a common mistake for me that i always seem to catch quicky, but i missed it this time. here is my revised clientLogin.php complete with a tiny bit of error control:
PHP Code:<?php
// login info broken down into useful chunks, the user and their domain
$clientLogin = strtolower($clientLogin);
$clientLoginArray = explode(".", $clientLogin);
$clientUser = $clientLoginArray[0];
$clientDomain = $clientLoginArray[1] . $clientLoginArray[2];
$clientUser = trim($clientUser);
$clientDomain = trim($clientDomain);
// html header stuff
print("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n");
print("<html>\n");
print("<head>\n");
print(" <title>Logging in $clientUser...</title>\n");
print("</head>\n");
print("<body>\n");
// database connetion variables
$host = "localhost";
$user = "root";
$dbName = "clientData";
$dbTable = "clientInfo";
$link = mysql_connect ($host, $user, $password);
$query = "SELECT * from $dbTable WHERE user = '$clientUser' AND domain = '$clientDomain'";
$result = mysql_db_query($dbName, $query, $link);
$row = mysql_fetch_array($result);
if ($row[user] != $clientUser || $row[domain] != $clientDomain) {
print("<h1>NOPE!</h1>\n");
}
else {
echo "client user = $clientUser<br>client domain = $clientDomain<br>client id = $row[id]";
}
mysql_close ($link)
?>
</body>
</html>it's amazing what velocity can do
when human beings are in season
Bookmarks