/    Sign up×
Community /Pin to ProfileBookmark

Warning: mysql_fetch_array() expects parameter 1 to be resource, string given

Hi guys, I am building a mini financial Data entry and computation app for my office. Please I have little issue with getting the current balance of account holders, i.e Sum(credit)-Sum(debit) of an account holder. Its displaying the history perfectly, but current balance only gives an error(Warning: mysql_fetch_array() expects parameter 1 to be resource, string given). Kindly help me with this, here’s the code below.

[code]
<table width=”250″ border=”1″ cellspacing=”1″ cellpadding=”1″>
<tr style=”background-color: skyblue” >
<td><a href=”customer.php”> Go Back </a></td><br>
<td><a href=”main_login.php”> Log-Out </a></td>
</tr>
</table>
<table width=”250″ border=”1″ cellspacing=”1″ cellpadding=”1″>
<tr>
<?php

$cname=$_POST[‘cname’];
$cnumber=$_POST[‘cnumber’];

include(‘includes/connection1.php’);
$result1 = “SELECT ((SELECT sum(`cred_amt`) WHERE `accountnumber`= ‘$cnumber’) – (SELECT sum(`deb_amt`) WHERE `accountnumber`= ‘$cnumber’)) AS `tbalance` FROM tbl_totaltransact”;
//$get=mysql_query($result1);
//$get2=mysql_query();
//$get3=[$get-$get2];
while($row = mysql_fetch_array($result1)){
//$row=mysql_fetch_array($result1);
$tbalance=$row[“tbalance”];

?>

<td>Current Balance</td>
<td><?php echo $tbalance ;?></td>
<?php } ?>
</tr>
</table>
<table width=”1000″ border=”1″ cellspacing=”1″ cellpadding=”1″>
<tr> <h2 align=”center”><?php echo $cname.”:”.” “.”History”; ?></h2></tr>
<tr>
<td>Credit Amount</td>
<td>Credit Discription</td>
<td>Date</td>
<td>Debit Amount</td>
<td>Debit Discription</td>
</tr>

<?php

//while($row=mysql_fetch_array($result)){

include(‘includes/connection1.php’);
$result = $db->prepare(“SELECT * FROM tbl_totaltransact WHERE accountname=’$cname’ AND accountnumber=’$cnumber’ ORDER BY date DESC”);
$result->execute();
for($i=0; $row = $result->fetch(); $i++){

?>
<tr>
<td><?php echo $row[‘cred_amt’]?></td>
<td><?php echo $row[‘cred_disc’]?></td>
<td><?php echo $row[‘date’]?></td>
<td><?php echo $row[‘deb_amt’]?></td>
<td><?php echo $row[‘deb_disc’]?></td>
<?php } ?>
</tr>
</table>
[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@eklibzauthorJul 30.2015 — Please I need urgent help with the issue I posted earlier. Anyone to help please...
Copy linkTweet thisAlerts:
@ginerjmJul 30.2015 — If you googled that message you would get dozens of hits I'm sure.

The reason for that message is that the query you ran did not produce the proper output. IE, the variable is not a resource (the normal result of a query) but is a Boolean containing the value False because it failed. Of course in your case it might not even be a 'False' as you'll see in my points below.

Things I'd change in this script:

1 - turn on php error checking (see my signature)

2 - you don't need to include the connection module multiple times. Once is enough since you only need to make the connection once.

3 - you have a query statement followed by a fetch but there is no execution of that query in between

4 - that same query - more complex than I've ever written - has 3 selects in it but only one table reference. Perhaps you need two more?

5 - you have a prepare statement for a query that has no substitute parms in it. Why do you feel you need to do a prepare here?

6 - you have a for statement using a fetch as the upper limit argument. Does that really work? Kind of a strange use when a much simpler to read and follow 'while' statement would do what you want and all you have to do is increment your $i var manually in the loop. Of course - I don't even know why you have the $i variable and for loop since you don't use $i in it at all.

7 - if you had done ANY READING at all on your problem and bothered to look up the fetch function you would have noticed the LARGE-type warning in the manual telling you to cease using the MySQL_* functions since they are about to be removed from php. I'd hate to see you solve this problem only to have to revisit your script in a few months to change to a different sql interface.


Lastly - I have to ask: Been programming long?
Copy linkTweet thisAlerts:
@eklibzauthorJul 31.2015 — Tnx @ginerjm. But I've only been programming a few months now.
Copy linkTweet thisAlerts:
@ginerjmJul 31.2015 — Looks like you have not done any kind of real "learning". Find a book and start over. that will help you do things that make sense to you and to your goal. At the very least get familiar with the php manual.
×

Success!

Help @eklibz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...