0

I have a website with the following code in the header - but the PHP echos in the body are returning anything:

<?php
session_start();
print_r($_SESSION);

$user = $_SESSION['email'];
$query = "SELECT * FROM first_page_data WHERE email_address= '$user' ";

$result = mysql_query($query);
$row_buyerdetails = mysql_fetch_assoc($result);
?>

The following returns nothing:

   <?php echo $row_buyerdetails['phone_number'] ?>

I know the session variable named 'email' is receiving a value from the previous page from the print_r function on line 3. Variable $user is also getting the correct email address.

The database is set up correctly (ive been able to access successfully in other ways, but im trying to modify it to access the data related to a particular email address as shown).

If somebody could point me in the right direction id apprectiate it! Also as a side, how would people suggest debugging PHP other than littering the code with echos and print_r functions? Is there even a way to put breakpoints in for example?


EDITED FOR HELP IN THE ANSWER BELOW

As requested, this is the code with the alterations requested:

<?php
$hostname_first_data = "*****";
$database_first_data = "*****";
$username_first_data = "*****";
$password_first_data = "*****";
$first_data = mysql_pconnect($hostname_first_data, $username_first_data, $password_first_data) or trigger_error(mysql_error(),E_USER_ERROR); 

echo mysql_errno($first_data) . ": " . mysql_error($first_data). "\n";

session_start();

print_r($_SESSION);
$user = $_SESSION['email'];
echo $user;
$query = "SELECT * FROM first_page_data WHERE email_address= '$user' ";

$result = mysql_query($query, $first_data);

$row_buyerdetails = mysql_fetch_assoc($result);

print_r($row_buyerdetails);

?>
6
  • 1
    Could you provide the return value for: print_r($row_buyerdetails); ? Commented Feb 5, 2014 at 22:32
  • 2
    Also, have you checked that for that specific user the query returns something? Commented Feb 5, 2014 at 22:35
  • @codeblur tried that, nothing! :) Commented Feb 5, 2014 at 22:36
  • Well, if print_r($row_buyerdetails); doesn't return anything... There's something wrong with your query :) Commented Feb 5, 2014 at 22:38
  • the page before is where the data for that particular email address is entered and it shows in PHPmyadmin as a new row so im presuming that the query returns something, (doing an echo on $result gives Resource id #12 but as I mentioned in the original post I dont know how to debug PHP so I dont know how to check it returns correctly! Commented Feb 5, 2014 at 22:40

1 Answer 1

1

Aren't you missing a mysql_connect call in your header or includes?

Try adding:

echo mysql_errno($link) . ": " . mysql_error($link). "\n";

$link being the resource you get from mysql_connect.

To debug PHP you have to install or activate an extension that is called Xdebug and use a nice IDE like PHPStorm, then Bob's your uncle :)

You can also use the Zend Debugger but I have limited experience with it.

You can (and should) also have full error reporting on when you are developing. It would tell you for example that the mysql_* functions are deprecated.

If you do not want the errors to appear on your page, you can choose to write to a log file and keep a tail open on that file.

Update for code:

<?php
$hostname_first_data = "*****";
$database_first_data = "*****";
$username_first_data = "*****";
$password_first_data = "*****";

$first_data = mysql_pconnect($hostname_first_data, $username_first_data, $password_first_data) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database_first_data, $first_data);

session_start();

print_r($_SESSION);
$user = $_SESSION['email'];
echo $user;
$query = "SELECT * FROM first_page_data WHERE email_address= '$user' ";

$result = mysql_query($query, $first_data);

echo mysql_errno($first_data) . ": " . mysql_error($first_data). "\n";

$row_buyerdetails = mysql_fetch_assoc($result);

print_r($row_buyerdetails);

?>

Tell me what that version outputs...

Sign up to request clarification or add additional context in comments.

12 Comments

sorry - thats in a separate file called using <?php require_once('Connections/first_data.php'); ?> This definitely works correctly, as the data submission form on the previous page puts data into the database using the same file
What does print_r($row_buyerdetails); outputs?
Can you try the mysql error functions added in the answer? Also can you rewrite your code to use the specific resource returned from mysql_connect? i.e. mysql_query($query, $link);
added to the original question - im ready for you to pick my programming 'skills' to pieces! :)
lol! Could you try my version? I moved the error reporting line to after the call to mysql_query.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.