Click to See Complete Forum and Search --> : sql server help
jrthor2
05-21-2003, 12:45 PM
I have a search page where you select the table you want to search, enter in a customer id and a package id. On my search_results page, I have the folowing code to connect to our sql server database, run a sql query and display the results. I am getting connected (I believe, not getting any errors), but I am not getting any data. Here is my code if someone could please help. Thanks.
<?
include "DB.php";
include "dbconn.php";
include "config.php";
function sqllookup() {
$conn_id = odbc_connect("sqlserverNGPS","appngph","vorfa");
$sql = "SELECT * FROM " . $table . "WHERE CustNbr = $cust_nbr";
print $sql;
$result=@odbc_do($conn_id, $sql);
for ($i=1; $i<= $numrows; $i++) {
odbc_fetch_row($result,$i);
$sql_last_nme = odbc_result($result,"LST_NME");
}
}
?>
It si not printing my $sql code, or anything. Even if I put in a print statement right after the function sqllookup() code, it doesn't print. I kind of need this fast, so any sppedy help would be greatly appreciated.
thanks again!!
Where you connect to the DB I would add a die statement, so you know if you are getting connected. Something like or die ('Did not connect to the database');
also, you sql statement should probably be:
$sql = "SELECT * FROM '$table' WHERE CustNbr = '$cust_nbr'";
and then, $numrows doesn't equal anything (in your for loop) unless it is defined in one of your included pages, which I doubt...
jrthor2
05-21-2003, 01:27 PM
Here is my updated code. I am not hitting the or die part like you suggested I put in, so I must be connecting, but not getting anything.
function sqllookup() {
$conn_id = odbc_connect("sqlserverNGPS","appngph","vorfa") or die ('Did not connect to the database');
$sql = "SELECT * FROM '$table' WHERE CustNbr = $cust_nbr";
print $sql;
$result=@odbc_do($conn_id, $sql);
$numrows = "0";
for ($i=1; $i<= $numrows; $i++) {
odbc_fetch_row($result,$i);
$sql_last_nme = odbc_result($result,"LST_NME");
}
}
?>
I've never used this type of database, so I'm more or less guessing here. I use mySQL...
Anyway, I looked up some of the functions on php.net, and I'd give this a try. But, be warned... it's a shot in the dark.
<?PHP
function sqllookup() {
$conn_id = odbc_connect("sqlserverNGPS","appngph","vorfa") or die ('Did not connect to the database');
$sql = "SELECT * FROM '$table' WHERE CustNbr = '$cust_nbr'";
print $sql;
$result = odbc_do($conn_id, $sql) or die ('No results');
while (odbc_fetch_row($result)) {
$sql_last_name = odbc_result($result,"LST_NME");
echo $sql_last_name;
}
}
?>
jrthor2
05-21-2003, 01:55 PM
I tried that, and I still get nothing??? No print $sql showing or anything. Do I need to call the function somewhere else??
I've attached my entire page code.
I don't see where you are calling the function at all, so either remove the function and just have that run right away, or add this sqllookup(); to your code somewhere...
jrthor2
05-21-2003, 02:05 PM
Yeah, I got that. I added sqllookup() and now it prints my sql, but the table and cust_nbr are not there in my sql statement, so I tried this:
function sqllookup($table,$cust_nbr) {
$conn_id = odbc_connect("sqlserverNGPS","appngph","vorfa") or die ('Did not connect to the database');
print "Hello";
$sql = "SELECT * FROM " . $table . " WHERE CustNbr = " . $cust_nbr;
print $sql;
$result=odbc_do($conn_id, $sql) or die ('No results');
while (odbc_fetch_row($result)) {
$sql_last_name = odbc_result($result,"LST_NME");
echo $sql_last_name;
}
}
sqllookup($table,$cust_nbr)
but now I don't get anything on my page, just a blank page. Is it dying somewhere?????
Sorry to just keep guessing here, but you could try this:
$sql = "SELECT * FROM $table WHERE CustNbr = '$cust_nbr'";
Also, where are you defining $table and $cust_nbr? I didn't see those in the file you uploaded.
If that doesn't work, try a SQL command without any variable. Try putting the name of the table and a real CustNb in...
jrthor2
05-21-2003, 02:14 PM
Well, now I'm jsut getting a blank page, nothing on it. I am posting the table and cust/_nbr from a form. Here is the updated code (with the table and cust_nbr hard coded):
function sqllookup() {
$conn_id = odbc_connect("sqlserverNGPS","appngph","vorfa") or die ('Did not connect to the database');
$sql = "SELECT * FROM PNCST_CUSTOMER_TBL WHERE CustNbr = '000000001'";
print $sql;
$result=odbc_do($conn_id, $sql) or die ('No results');
while (odbc_fetch_row($result)) {
$sql_last_name = odbc_result($result,"LST_NME");
echo $sql_last_name;
}
}
sqllookup()
Unfortunately, I don't know odbc enough to help you. Try posting here: http://www.phpbuilder.com/board/forumdisplay.php?s=&forumid=6 Sorry about that...
jrthor2
05-22-2003, 07:57 AM
Well, I took our the loop statement and I now at least get mys sql printed along with an error:
Warning: SQL error: , SQL state 00000 in SQLExecDirect in /home/prgjr1/public_html/NGPH-doc/mainframe/table_search_results.php on line 23
No results
Line 23 is the $result = odbc_do($conn_id,$sql) or die ('No results');