473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Warning: extract(): First argument should be an array in

Geeza1973
Below is the code I have:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include("db.php");
  3. mysql_connect ($host, $user, $pass);
  4. mysql_select_db ($database);
  5. $result = mysql_query ("SELECT * FROM `highscore` ORDER BY score DESC LIMIT 10");
  6. $row = @mysql_fetch_array ($result);
  7. extract ($row);
  8. echo "$player : $score ";
  9. ?>
  10.  
The db.php file has my username, password, host, database name and table set to variables, which are correct.

I keep getting this error though:
Expand|Select|Wrap|Line Numbers
  1. Warning: extract(): First argument should be an array in folder/folder/folder/display_scores.php on line 7
  2.  
Which is the extract ($row); line

Can anybody help me or point me in the right direction? It would be greatly appreciated. Thanks in advance.
Sep 12 '07 #1
22 4426
Atli
5,058 Expert 4TB
Hi Geeza. Welcome to The Scripts!

The mysql_fetch_array() function is likely not able to read a row from the mysql_query() result resource. When that happens it returns FALSE, which triggers the warning you get when the extract() function tries to use it's result.

Try doing something more like this:
Expand|Select|Wrap|Line Numbers
  1. if($row = @mysql_fetch_array()) {
  2.   extract($row);
  3.   echo "$player - $score";
  4. }
  5. else {
  6.   # MySQL query failed or returned no rows
  7. }
  8.  
Or if you want to show all the rows your query returned:
Expand|Select|Wrap|Line Numbers
  1. while($row = @mysql_fetch_array()) {
  2.   extract($row);
  3.   echo "<br />$player - $score";
  4. }
  5.  
Sep 12 '07 #2
I tried both of your examples Atli. The good news is that both of you're examples got rid of the error. The bad news is that now I am getting nothing. I'm assuming there must be a problem in my db.php file . Its as if the database isn't even being accessed. I'm not 100% sure that what I set the $host variable to was correct. I'm waiting to hear back from my host company to find out the host name.
Sep 12 '07 #3
Atli
5,058 Expert 4TB
Try turning on php error messgaes.
Sep 12 '07 #4
I added the lines to display any errors. Still nothing comes up. Because I was unsure of the host name in the db.php file I changed it to something else to see if that would cause errors. It did. When I changed it back to what it was originally the errors disappeared, so I'm guessing what I have as the host name is correct. I'm still waiting to hear back from my host company.
Sep 12 '07 #5
pbmods
5,821 Expert 4TB
Heya, Geeza.

Try
Expand|Select|Wrap|Line Numbers
  1. var_dump($row);
to make sure you're working with what you think you're working with.

If it's false, make sure that your sql query isn't generating an error:
Expand|Select|Wrap|Line Numbers
  1. echo mysql_error();
  2.  
Sep 12 '07 #6
Hi pbmods,
I first added the line: var_dump($row); to the bottom of my file and u/l it. In my browser I got the result NULL
I then added the line: echo mysql_error(); to the bottom of the file and u/l it. I just got the same result of NULL on the screen.

If it helps at all, my file now looks like this:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. error_reporting(E_ALL);
  3. ini_set('display_errors', True);
  4. include("db.php");
  5. mysql_connect ($host, $user, $pass);
  6. mysql_select_db ($database);
  7. while($row = @mysql_fetch_array()) {
  8.    extract($row);
  9.    echo "<br />$player - $score";
  10. }
  11. var_dump($row);
  12. echo mysql_error();
  13. ?>
Sep 12 '07 #7
pbmods
5,821 Expert 4TB
Heya, Geeza.

Please use CODE tags when posting source code:

[CODE=php]
PHP code goes here.
[/CODE]
Sep 12 '07 #8
pbmods
5,821 Expert 4TB
Is this line in there somewhere, too?
Expand|Select|Wrap|Line Numbers
  1. $result = mysql_query ("SELECT * FROM `highscore` ORDER BY score DESC LIMIT 10");
  2.  
Sep 12 '07 #9
Ok I'll start using the [code] tags, sorry I didn't realise. I also didn't realise I'd deleted the line:
Expand|Select|Wrap|Line Numbers
  1. $result = mysql_query ("SELECT * FROM `highscore` ORDER BY score DESC LIMIT 10");
  2.  
So I put that line back in and it now comes up saying:
NULL Table 'highscoreDB.highscore' doesn't exist
I checked to make sure all the spelling is correct and it is. I tried renaming highscoreDB to see what would happen. Half expecting it to say the same thing but instead it came back saying: NULL No Database Selected.

Now I am really confused why it says highscoreDB doesn't exist when I've set it up in phpMyAdmin. But yet when I rename highscoreDB to something completely different it doesn't say that that doesn't exist, it comes back saying 'No Database Selected'.
Sep 13 '07 #10
pbmods
5,821 Expert 4TB
Heya, Geeza.

You're getting a 'no database selected' error because on this line:
Expand|Select|Wrap|Line Numbers
  1. mysql_select_db ($database);
  2.  
$database is still set to the previous name of the database. Because there is no database with the old name anymore, MySQL does not select a database.
Sep 13 '07 #11
I changed the value of $database from "highscoreDB.highscore" to something else like "scoreDB.highscore". Not the name of the actual database itself. That is what is confusing me. Why is it when I change the value of $database to "scoreDB.highscore" does it not say "scoreDB.highscore doesn't exist", like it does with the correct name "highscoreDB.highscore" Instead it just says "database not selected."
Sep 13 '07 #12
I found out what was causing the errors 'database not selected' and 'database not found'. In the table name of the database I had 'highscore' in one place and 'highScore' in another place. So I no longer get those errors but I still only have 'NULL' come up in my browser without any results from my database.
Sep 13 '07 #13
pbmods
5,821 Expert 4TB
Heya, Geeza.

Ooh, good call.

Despite Blizzard's incessant warnings to the otherwise (I will have a good laugh if this link gets removed), case-sensitive filesystems can be your friend.

And in this particular case, your worst enemy.
Sep 13 '07 #14
Atli
5,058 Expert 4TB
Heya, Geeza.

Ooh, good call.

Despite Blizzard's incessant warnings to the otherwise (I will have a good laugh if this link gets removed), case-sensitive filesystems can be your friend.

And in this particular case, your worst enemy.
Hehe, you got to love Blizzard :)
Weird that they wont just fix it. Looks like it's only the Installer that's having problems. Shouldn't be that hard either. Bit boring maybe :P
Sep 14 '07 #15
Yeah I nearly kicked myself when I found it was something as simple as having an 's' when it should have been an 'S'. Maybe I'll stick to all lowercase from now on, at least while I'm still learning :) I'm still not getting any results from my database though.
Sep 14 '07 #16
pbmods
5,821 Expert 4TB
Heya, Geeza.

Is mysql_error() spitting anything out at you?
Sep 14 '07 #17
No pbmods, its not giving me anything. The whole line I have is:
Expand|Select|Wrap|Line Numbers
  1. echo mysql_error();
  2.  
And I have it right at the end of my code.
Sep 15 '07 #18
pbmods
5,821 Expert 4TB
Heya, Geeza.

Sounds like you might be good to go. What does this output?
Expand|Select|Wrap|Line Numbers
  1. $row = mysql_fetch_assoc($result);
  2. print_r($row);
  3.  
Note that I removed the '@' so as to make it easier to debug. I also changed mysql_fetch_array() to mysql_fetch_assoc() for greater efficiency.
Sep 15 '07 #19
Hey pbmods,
Thanks man!
I added that code you gave me and I finally got a result. I got:

Array ( [player] => E-L-T [score] => 15000 )

As soon as I got that result I took another section of code you gave me:
Expand|Select|Wrap|Line Numbers
  1. while($row = @mysql_fetch_array()) {
  2.    extract($row);
  3.    echo "<br />$player - $score";
  4. }
  5.  
And edited it to:
Expand|Select|Wrap|Line Numbers
  1. while($row = mysql_fetch_assoc($result)) {
  2.     print_r($row);
  3. }
  4.  
Which I'm quietly proud of :)
And got all the results from the database. The only difference between my results and the results in the tutorial I was following when this forum topic began. Is that my results have the 'Array' and [] => come up etc. Where as the results in the tutorial just had 'player_name : score'. Is that easy to fix?
Sep 15 '07 #20
pbmods
5,821 Expert 4TB
Heya, Geeza.

print_r() is a fantastic debugging function, but you are correct; its output is seldom useful for deployment.

The good news is that where you call print_r(), $row now contains all the data for one row in your MySQL result set.

This gives you two options:
  • In the spirit of eventually creating a Data Abstraction Layer, you can copy your MySQL result set into an associative array:
    Expand|Select|Wrap|Line Numbers
    1. $resultSet = array();
    2. while( $_row = mysql_fetch_assoc($result) )
    3. {
    4.     $resultSet[] = $_row;
    5. }
    6.  
  • Or you can just go ahead and process your data in the loop itself:
    Expand|Select|Wrap|Line Numbers
    1. while( $row = mysql_fetch_assoc($result) )
    2. {
    3.     echo "
    4.         <tr>
    5.             <td>{$row['id']}</td>
    6.             <td>{$row['score']}</td>
    7.         </tr>";
    8. }
    9.  
Sep 15 '07 #21
Hey pbmods,
Thanks so much for all your help man!
I tried your first example and didn't get a result although I haven't read all of the info from the link you gave me.

The second example worked a treat, gave me the exact results I was looking for.

Thanks again for all your help dude, you're a lifesaver! Now I can carry on watching the tutorial I was working along with.
Thanks.
Sep 15 '07 #22
pbmods
5,821 Expert 4TB
Heya, Geeza.

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Sep 15 '07 #23

Sign in to post your reply or Sign up for a free account.

Similar topics

35
by: jerrygarciuh | last post by:
Hi all, I was just wondering what popular opinion is on PHP giving this warning: Warning: Invalid argument supplied for foreach() in /home/boogerpic/public_html/my.php on line 6 when...
4
by: Pushkar Pradhan | last post by:
I have some functions which take as i/p a buffer (it can be float, char, or 16 bit, int etc.). The result is another o/p buffer, its type is also flexible (it could be a float, char etc.). I try...
29
by: junky_fellow | last post by:
Consider the following piece of code: struct junk { int i_val; int i_val1; char c_val; }; int main(void) {
29
by: Daniel Rudy | last post by:
Hello, Consider the following code fragment: fileptr = fopen(param->filename, "r"); if (fileptr == NULL) { error("digest.c: digest_file: Open File ", errno); return(-2); }
3
by: zek2005 | last post by:
Hi friends! I have a varchar field in my DB with numeric values separates by spaces. I need to extract the numbers to create an array. Example 1: 1820 1823 1825 --> need to be transform into ...
5
by: =?Utf-8?B?aWxy?= | last post by:
Hi This is probably fairly simple but I am newish at programming and was wondering if someone can give me some advice on handling the following. I have an array with a large number of elements...
3
by: prix prad | last post by:
Hi All, I encountered the above error when I tried to expand a macro as follows: #define EXPAND(array) int M_ ## array The problem occurs when we have 'array' itself as a macro: say...
2
by: poreko | last post by:
I am connecting to my database using Object oriented PHP. My query is returning results but at the end of my table,at the bottom of the page I keep having this error when I run my program: Warning:...
2
by: StevenHu | last post by:
I do not want my iPhone slider to return simple numbers like 1, 2, 3, etc. to the text label, but 15mm, 15.5mm, 16mm, etc., from 15mm to 45mm in increments of .5. So I thought I would put all the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.