Jump to content

PHP Mysql query returning blank lines


Dakkadakka

Recommended Posts

I must be making some kind of silly mistake, but I have some php and sql intended to retrieve information on a customer for a shopping cart. In particular, their contact into and invoice history. For the life of me I cannot figure out why the invoice history is printing out empty blank rows. This is the code. There are two different queries going on. One for the contact info, and one of their invoices. Here is the code. <?php $dbuser = ""; $dbpass = ""; $host = "localhost"; $dbname = ""; // database connection mysql_connect("localhost", $dbuser, $dbpass) or die(mysql_error()); mysql_select_db($dbname) or die("Unable to select database"); //This query grabs all the puchases going back up to three months //This debug line displays the query $query = "SELECT * FROM customers WHERE customer_id = '".$customer_id."'"; echo $query."<br>"; $result = mysql_query($query); echo '<h3 class = "splitter">CUSTOMER ID - '.$customer_id.'</h3>'; //-create while loop and loop through result set while($row=mysql_fetch_array($result)) { $customer_id =$row['customer_id']; $first_name =$row['first_name']; $last_name =$row['last_name']; $company_name =$row['company_name']; $phone =$row['phone']; $alt_phone =$row['alt_phone']; $email =$row['email']; $price_level = $row['price_level']; echo 'First Name - '.$first_name.'<br>'; echo 'Last Name - '.$last_name.'<br>'; echo 'Company Name - '.$company_name.'<br>'; echo 'Phone - '.$phone.'<br>'; echo 'Alt Phone - '.$alt_phone.'<br>'; echo 'Email - '.$email.'<br>'; echo 'Individual Price Level - '.$price_level.'<br>'; //echo 'Click to make changes here -<a href="editcust.php?customer_id="'.$customer_id.'">Edit</a><br>'; } //Show their invoice history $invquery = "SELECT customer_id, transaction_id, time_created FROM invoices WHERE customer_id = '".$customer_id."'"; echo $invquery."<br>"; $invresult = mysql_query($invquery); echo '<h3 class = "splitter">Transactions</h3>'; //-create while loop and loop through result set while($row=mysql_fetch_array($invresult)) { $customer_id = $row['customer_id']; $transaction_id = $row['$transaction_id']; $time_created = $row['$time_created']; echo 'ID - '.$transaction_id.' Date/time - '.$time_created.' - <a href="viewinvoice.php?transaction_id="'.$transaction_id.'&&customer_id="'.$customer_id.'">View</a><br>'; } ?> The first query successfully gives me the contact information. The second query is doing something strange that I would ordinarily know how to debug. The echo statement prints blanks where the variables should be printing, yet I can tell the data is being read because if I enter the same query directly into the MySQL database, I get the proper rows with all the data visible. In PHP however, I get the rows with nothing printed on them. The number of rows match! Why would it not print the values?

Edited by Dakkadakka
Link to comment
Share on other sites

add some debug statements.check $row using var_dump(). you will also want to check querry getting success or not. you can do that by using mysql_error() if $invresult is false

while($row=mysql_fetch_array($invresult)){ var_dump($row); //<============debug statement$customer_id = $row['customer_id'];$transaction_id = $row['$transaction_id'];$time_created = $row['$time_created'];echo 'ID - '.$transaction_id.' Date/time - '.$time_created.' - <a href="viewinvoice.php?transaction_id="'.$transaction_id.'&&customer_id="'.$customer_id.'">View</a><br>';}

Link to comment
Share on other sites

add some debug statements.check $row using var_dump(). you will also want to check querry getting success or not. you can do that by using mysql_error() if $invresult is false
while($row=mysql_fetch_array($invresult)){ var_dump($row); //<============debug statement$customer_id = $row['customer_id'];$transaction_id = $row['$transaction_id'];$time_created = $row['$time_created'];echo 'ID - '.$transaction_id.' Date/time - '.$time_created.' - <a href="viewinvoice.php?transaction_id="'.$transaction_id.'&&customer_id="'.$customer_id.'">View</a><br>';}

That's a fantastic trick. I found the problem too. Thanks!
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...