Question:
PHP sqlquery:what's wrong with this?
sandy360
2011-02-01 17:10:22 UTC
I'm getting an error for this
$query="select * from `bill`";
$result=mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_array($result)){ //this is my line 18
$row_id=$row['row_id'];
.
.
//148 more fields
}

this is the error
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in D:\wamp\www\bill\index.php on line 18
Four answers:
Ratchetr
2011-02-01 18:26:42 UTC
That's generally the error you get if you leave off the or die clause. mysql_query returns FALSE if the query failed, which will then generate that warning when you hit the while loop.



But you have an or die clause, so that's a bit puzzling.



So I can only think of a couple things to try to verify:

Maybe there is a case where mysql_query returns false, but doesn't trigger or die. That would be odd, but a quick test should verify if that is happening:

Add this line right after mysql_query:

if(!$result) die("Yuck:".mysql_error());



If you get the Yuck message, something is odd, but you should at least see the error message.



If you don't get the yuck message, then your loop really should execute (at least once).

Add an Echo statement as the first line of your while loop. Does the loop execute at least once?



One thing that comes to mind is that you might be modifying $result within your while loop. So the loop works the first time, but not the second. Knowing if the loop runs at least once will tell you if that is happening.



I am curious about 1 thing. 148 more fields? That is a ***LOT*** of columns for a database table. I've never worked on a database with that many columns in 1 table, and I've worked on some reasonably complex stuff. That's not to say that there aren't legit cases where that would make sense, but it does raise a yellow flag.

Do any of your columns have numbers as part of the name? num1, num2, num3, etc? Something is wrong if you are doing that.

You posted a question not long ago about creating index names dynamically. That's another yellow flag (actually, that one is more of a red flag).



So...just curious...what exactly is in these 149 columns? There might be better ways of doing things.
?
2011-02-01 17:40:12 UTC
The error means that the query [ $result=mysql_query($query) or die(mysql_error()); ] did NOT return a pointer to a list of results, but just a "false".



1. is your database openned (connected) ?

2. is your database selected?

3. do you have a table called "bill" (case sensitive)

4. is there any entries in that table?
Saurabh
2011-02-04 23:36:15 UTC
make use of this

$con = mysql_connect("hostname","username","password");

mysql_select_db("yourDatabaseName", $con);

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

if($mysql_error)

{

echo 'query failure';

}

else

{

the rest of ur code.

}





$con is the connection object you have used to connect to your database in the first place.
?
2017-01-09 12:17:32 UTC
some errors:

outcomes

"; if ($discover == "") { go out; echo "

You forgot to pass into an alarm code"; } $discover = strtoupper($discover); $discover = strip_tags($discover); $discover = trim ($discover); $archives = mysql_query("decide on * FROM clientele the place top($field) LIKE'%$discover%'"); *** ??? this is a badly formatted question! => define "$fieldname" one after the different: i'm not sure you get it top! $sq. = "decide on * from `clientele` the place ` " . $fieldname . " ` like ' % " . $discover . " %' "; $archives = mysql_query($sq.); // pay interest to ` ' " ! areas extra for readability. at a similar time as($effect = mysql_fetch_array($archives)) echo ( $effect['codes'] . " " . $effect['description'] . "

n"; *** returned, no brackets required: one line basically. echo ("looked for: " .$discover . "n" ) ; // } *** one bracket too many. *** you forgot to "mysql_free_result" after your enquiries, to rid of $archives *** you forgot to close the database ("mysql_close") ?>



This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...