daniel0816 Posted August 30, 2013 Share Posted August 30, 2013 I am trying to write a query that executes when the user types in a number into a textbox and then clicks the search button. The query is supposed to get the JS_Status from the JobStatus table with the JS_ID that matches the value that has been submitted through the textbox and display it in an alert box. The code I have runs but it gives me this weird message in the alert box saying Resource id#3 which I am hoping someone could tell me what that is. Here is my code: <?phperror_reporting(E_ALL);$connect = mysql_connect("dbinfo", "dbinfo", "dbinfo");//select databasemysql_select_db("dbinfo", $connect); $refNum = $_POST["refNum"];$status = mysql_query("SELECT JS_Status FROM JobStatus WHERE $refNum = JS_ID");if($status){ echo '<script language="javascript" type="text/javascript"> alert(\''.$status .'\'); </script>';}else { die ("could not run query"); } ?> Thanks again Link to comment https://forums.phpfreaks.com/topic/281699-query-from-a-textbox/ Share on other sites More sharing options...
cyberRobot Posted August 30, 2013 Share Posted August 30, 2013 Have you tried echoing $status on the PHP side to see what it contains? Side note: perhaps you were going to do this later, but it's recommended that you sanitize/validate any user-supplied information before using it in things like MySQL queries. Since $_POST["refNum"] is supposed to be a number, you could utilize ctype_digit(): http://php.net/manual/en/function.ctype-digit.php Link to comment https://forums.phpfreaks.com/topic/281699-query-from-a-textbox/#findComment-1447449 Share on other sites More sharing options...
Psycho Posted August 30, 2013 Share Posted August 30, 2013 When you execute a query, the return value is a resource identifier that points to the results of the query. You need to use one of the mysql_fetch functions to get the actual data. And, that data would be an array - so it would not be usable in JavaScript until you get the value you need from the array first. The less used method would be to mysql_result() to get a particular value from the resource identifier. <?php error_reporting(E_ALL); $connect = mysql_connect("dbinfo", "dbinfo", "dbinfo"); //select database mysql_select_db("dbinfo", $connect); $refNum = intval($_POST["refNum"]); $query = "SELECT JS_Status FROM JobStatus WHERE JS_ID = $refNum"; $result = mysql_query($query); if(!$result) { $message = "There was a problem getting the results"; } elseif(!mysql_num_rows($result)) { $message = "There was no matching record"; } else { $status = mysql_result($result, 0); } echo "<script language=\"javascript\" type=\"text/javascript\"> alert('{$status}'); </script>"; ?> Link to comment https://forums.phpfreaks.com/topic/281699-query-from-a-textbox/#findComment-1447453 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.