Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test for an empty SQL result in ASP

I am running a query from ASP using a MySQL database, I want to create a variable (ssResult) based on the result with a person's name (fullname), if the record does not exist I want to assign the text 'N/A' to the variable, code below, I currently use a function getOther for my database connections which passes the column name "fullname":

ssResult = getOtherElse("SELECT fullname FROM table WHERE id=" & inArr(j), "fullname")

Below is the code for the function getOtherElse which only works when a result is returned but not when there is an empty result:

Function getOtherElse(inSQL, getColumn)
    Dim conn, rstemp
    Set conn = Server.CreateObject("ADODB.Connection")
    conn.open myDSN
    Set Session("lp_conn") = conn
    Set rstemp = Server.CreateObject("ADODB.Recordset")
    rstemp.Open inSQL, conn 
    if not rstemp.eof then
        rstemp.movefirst
        getOtherElse=rstemp.fields(getColumn)
    else
        getOtherElse="N/A"
    end if
    rstemp.close
    set rstemp=nothing
    conn.close
    set conn=nothing
End Function

Thanks!

like image 625
Stuart Avatar asked Nov 18 '25 21:11

Stuart


1 Answers

You could try changing the line

if not rstemp.eof then

with

if rstemp.RecordCount > 0 then
like image 66
ARemesal Avatar answered Nov 20 '25 14:11

ARemesal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!