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.

// 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)

i’m really quite new at both php and sql so i appologize for being thick, but i’m usually pretty good at figuring this kind of thing out.

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.

Try:


$query = "SELECT * from ".$dbTable." WHERE user = '$clientUser' AND domain = '$clientDomain'";

Sean :slight_smile:

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.

:cool2: 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
// 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\\">\
");
print("<html>\
");
print("<head>\
");
print("	<title>Logging in $clientUser...</title>\
");
print("</head>\
");
print("<body>\
");
// 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>\
");
}
else {
	echo "client user = $clientUser<br>client domain = $clientDomain<br>client id = $row[id]";
}
mysql_close ($link)
?>
</body>
</html>

i love it when a plan comes together :slight_smile: