Jump to content

Nothing being selected from the database


stormx

Recommended Posts

Hello, I cannot pinpoint where the error is on my script:

 

<?php

error_reporting(0);

session_start();

include 'my_sql.php';

$sql_users = mysql_query("SELECT `id` FROM `users`") or die("Error Selecting ID from users.</strong>");

date_default_timezone_set('Australia/Victoria');

while($id = mysql_fetch_array($sql_users)) {

$plan1_sql = mysql_query("SELECT * FROM `plan` WHERE `id` = '$id[plan]'") or die("Error!");
$plans = mysql_fetch_array($plan1_sql);
date_default_timezone_set('Australia/Victoria');
$plan_id = $id["plan"];
$result = mysql_query("SELECT * FROM plan
WHERE id='$plan_id'");
while($plans = mysql_fetch_array($result))
  {
}
$service_number = $id["service"];
$invoice_id = rand();
$amount = $plans['mcharge'];
$userid = $id['id'];
$status_pending = "PENDING";
$acc_rep = $id['representative'];
$addressss = $id['address'];
$acc_add_name = $acc_rep . $addressss;
$today_date = date("Y-m-d");
$notes = "AWAITING AUTO DEBIT";
$month = date("F");
$year = date ("Y");
$item_desc = "Monthly Charge On Plan $plans[name] For $month $year";

mysql_query("INSERT INTO invoices (date, servicenumber, invoiceid, amount, view, userid, status, name_address, item, totalprice, notes) 
VALUES ('$today_date', '$service_number', '$invoice_id', '$amount', '$service_number', '$userid', '$status_pending', '$acc_add_name', '$item_desc', '$amount', '$notes')") or die(mysql_error());

?>

 

The issue is around the

$plan1_sql = mysql_query("SELECT * FROM `plan` WHERE `id` = '$id[plan]'") or die("Error!");
$plans = mysql_fetch_array($plan1_sql);

mark, it's not selecting any data at all..

 

Please help ???

 

Link to comment
Share on other sites

OK, after a more thorough look at the code - it is not amkign any sense. The same query is being run twice - with nothing being doine with the results from the first query not being used. Plus, after the second query, there is a while loop that does nothing inside the loop which leaves only the last record being assinged to the array variable $plans.  Then, that avaraible is only used once for $plans['mcharge']. I have no clue what is trying to be accomplished here. And, why wuld you turn off error reporting when there are problems you are trying to debug?

 

Also, I always create my queries as string variables so I can echo them to the page when there is a problem. It helps to determine that variables have the values I expect them to.

 

Lastly, you are running nested queries. That can be done MUCH more efficiently by using JOINs in your queries.

 

I think much of the problem is due to the fact that the very first query is only returning the id column, but the second query is trying to use the 'plan' value. The first query will need to return that column if you want to use it.

 

I would make an attempt at fixing the code, but again I'm not 100% sure of what the goal is.

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.