Jump to content

Connection to a DB


snowymasta

Recommended Posts

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?

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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']."'");
 

Link to comment
Share on other sites

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 -

  1. 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.
  2. 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 by mac_gyver
Link to comment
Share on other sites

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 -

  1. 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.
  2. 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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.