snowymasta Posted November 20, 2020 Share Posted November 20, 2020 Hi Guys, need a little help and drawn a blank. The code that is not working is below $result = $conn->query("SELECT * FROM MK_migration_details WHERE mig_bid='".$_SESSION['bid']."'"); I can confirm the $_SESSION['bid'] is working as echo's out on another part of the page. and the above $result works when i manually type the search criteria like below. $result = $conn->query("SELECT * FROM MK_migration_details WHERE mig_bid='300101'"); Basically no result turn up on the page. Any ideas or advise how I can diagnose? Quote Link to comment https://forums.phpfreaks.com/topic/311746-connection-to-a-db/ Share on other sites More sharing options...
Barand Posted November 20, 2020 Share Posted November 20, 2020 Check for mysql error messages print_r($conn->errorInfo()) // PDO echo $conn->error; // mysqli Quote Link to comment https://forums.phpfreaks.com/topic/311746-connection-to-a-db/#findComment-1582534 Share on other sites More sharing options...
snowymasta Posted November 20, 2020 Author Share Posted November 20, 2020 tried it and nothing throws out Quote Link to comment https://forums.phpfreaks.com/topic/311746-connection-to-a-db/#findComment-1582535 Share on other sites More sharing options...
Barand Posted November 20, 2020 Share Posted November 20, 2020 (edited) What if you echo the query string to examine the syntax? echo "SELECT * FROM MK_migration_details WHERE mig_bid='".$_SESSION['bid']."'"; Edited November 20, 2020 by Barand 1 Quote Link to comment https://forums.phpfreaks.com/topic/311746-connection-to-a-db/#findComment-1582536 Share on other sites More sharing options...
snowymasta Posted November 20, 2020 Author Share Posted November 20, 2020 Ok that worked and it does exactly what it should, so assume that means the error is elsewhere! Whole Script below <?php require_once("db_connection.php"); $conn = new mysqli($servername, $username, $password, $dbname) or die("Connect failed: %s\n". $conn -> error); $result = $conn->query("SELECT * FROM MK_migration_details WHERE mig_bid='".$_SESSION['bid']."'"); $data = array(); while($row = $result->fetch_assoc()) { $data[] = $row; } echo json_encode($data); ?> Quote Link to comment https://forums.phpfreaks.com/topic/311746-connection-to-a-db/#findComment-1582537 Share on other sites More sharing options...
Barand Posted November 20, 2020 Share Posted November 20, 2020 I don't see any error check after the query() call. Easier than checking all mysqli calls is to put this line of code before the "new mysqli()" line ... mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); Quote Link to comment https://forums.phpfreaks.com/topic/311746-connection-to-a-db/#findComment-1582538 Share on other sites More sharing options...
snowymasta Posted November 20, 2020 Author Share Posted November 20, 2020 you mean like the below? mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); $conn = new mysqli($servername, $username, $password, $dbname) or die("Connect failed: %s\n". $conn -> error); $result = $conn->query("SELECT * FROM MK_migration_details WHERE mig_bid = '".$_SESSION['bid']."'"); Quote Link to comment https://forums.phpfreaks.com/topic/311746-connection-to-a-db/#findComment-1582539 Share on other sites More sharing options...
Barand Posted November 20, 2020 Share Posted November 20, 2020 yes, and make sure your php error reporting is on. Quote Link to comment https://forums.phpfreaks.com/topic/311746-connection-to-a-db/#findComment-1582540 Share on other sites More sharing options...
mac_gyver Posted November 20, 2020 Share Posted November 20, 2020 the value probably contains some non-printing character(s). what does var_dump($_SESSION['bid']); show? Quote Link to comment https://forums.phpfreaks.com/topic/311746-connection-to-a-db/#findComment-1582541 Share on other sites More sharing options...
snowymasta Posted November 20, 2020 Author Share Posted November 20, 2020 (edited) Hi Mac_gyver it shows this string(6) "300101" The DB structure is mig_bidvarchar(255)utf8_general_ciNoNone Edited November 20, 2020 by snowymasta Quote Link to comment https://forums.phpfreaks.com/topic/311746-connection-to-a-db/#findComment-1582542 Share on other sites More sharing options...
mac_gyver Posted November 21, 2020 Share Posted November 21, 2020 (edited) that's an acceptable var_dump() output. the length matches the number of displayed characters. i suspect you are seeing the result of two (or more) requests to the page. for the first request, there is an input value and when you are using echo/var_dump to display it, this is preventing a header() redirect, that's causing additional request(s), which don't have the expected input. you are seeing the displayed result from the last request made. this 'works' when you hard-code the value, since all requests have the input value. so, two things - if it sounds like your code is capable of doing this, you will need to find and fix what's causing the additional request(s) and if you cannot determine what's causing the problem, you will need to post all of the code needed to reproduce the problem. you should always validate all inputs before using them (yes a session variable is an input to your code) and setup/display a message letting the visitor know that a 'required' input is not present. lastly, why is this value in a session variable at all. that's generally a sign that you are doing something in the hardest way possible. Edited November 21, 2020 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/311746-connection-to-a-db/#findComment-1582543 Share on other sites More sharing options...
snowymasta Posted November 24, 2020 Author Share Posted November 24, 2020 On 11/21/2020 at 1:13 AM, mac_gyver said: that's an acceptable var_dump() output. the length matches the number of displayed characters. i suspect you are seeing the result of two (or more) requests to the page. for the first request, there is an input value and when you are using echo/var_dump to display it, this is preventing a header() redirect, that's causing additional request(s), which don't have the expected input. you are seeing the displayed result from the last request made. this 'works' when you hard-code the value, since all requests have the input value. so, two things - if it sounds like your code is capable of doing this, you will need to find and fix what's causing the additional request(s) and if you cannot determine what's causing the problem, you will need to post all of the code needed to reproduce the problem. you should always validate all inputs before using them (yes a session variable is an input to your code) and setup/display a message letting the visitor know that a 'required' input is not present. lastly, why is this value in a session variable at all. that's generally a sign that you are doing something in the hardest way possible. Thank you for your advise on this really appreciated, The session is being created at a login script I have and then passes that business ID through the pages. Quote Link to comment https://forums.phpfreaks.com/topic/311746-connection-to-a-db/#findComment-1582637 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.