Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,471
There are 1462 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

Mysql error (mysql_fetch_array doesn't work?)

Thread title: Mysql error (mysql_fetch_array doesn't work?)
Reply    
    Thread tools Search this thread Display Modes  
09-30-2009, 07:44 PM
#1
.Cyanide is offline .Cyanide
Status: Senior Member
Join date: Mar 2006
Location: Stockholm,Sweden
Expertise: Web Design, Coding
Software: Phoshop CS3
 
Posts: 757
iTrader: 4 / 100%
 

.Cyanide is an unknown quantity at this point

Send a message via MSN to .Cyanide

  Old  Mysql error (mysql_fetch_array doesn't work?)

Hey

I have this problem with me php code, I have no idea what the problem is, so I was hoping you guys could help me out.

Here's the code
PHP Code:
<html>
<head>
<title>Lab 4</title>
</head>
<body>
<?
$connection 
mysql_connect("xxx""xxx""xxx") or die ("Kunde inte an****a!");

mysql_select_db("xxx"$connection);

$input $_POST['revealThis'];

if (
$input == debattartiklar
    
$sql "SELECT * FROM debatt";
else if(
$input == eva)
    
$sql "SELECT * FROM debatt, sports WHERE forfattare='Eva Svensson'";
else
    echo 
"Du valde inget alternativ, vänligen välj ett";
    
    
$result mysql_query($sql$connection);

while (
$myRow mysql_fetch_array($result)) {
        echo 
"<h3 style=\"lineheight:1.5em;margin:0;padding:0;\">" $myRow['rubrik'] . "</h3>";
        echo 
"<strong>" $myRow['ingress'] . "</strong><br />";
        echo 
$myRow['brodtext'] . "<br />";
        echo 
$myRow['forfattare'] . ", " $myRow['datum'] . "<br /><br />";
}

mysql_close($connection);

?>
</body>
</html>
And this is the error i get:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in D:\FTPROOT\KTD\sh09hf1763\www\webbapplikationer09\ lab4\lab.php on line 23

Here's the script in action if you want to have a look at it. The input-radio-option that does not work is the second one, the first works perfectly.

http://student.ktd.sh.se/~sh09hf1763...lab4/form.html

Thanks alot!

EDIT: I realized you can't fetch something from a table where the fetch-item does not exist. However, I now have no idea how I'm going to make it possible to search all tables for posts by the author "Eva Svensson"

Reply With Quote
09-30-2009, 08:03 PM
#2
46Bit is offline 46Bit
Status: Member
Join date: Mar 2009
Location: Yorkshire
Expertise: Web Development
Software:
 
Posts: 275
iTrader: 10 / 100%
 

46Bit is on a distinguished road

Send a message via MSN to 46Bit Send a message via Skype™ to 46Bit

  Old

I've not got time to go through the various PHP and CSS errors in this but most importantly - remove your database credentials from the code.

Reply With Quote
09-30-2009, 08:14 PM
#3
Patrick is offline Patrick
Status: I love this place
Join date: Apr 2007
Location: Chi-Town
Expertise: Java/C#/C++ Programmer
Software:
 
Posts: 543
iTrader: 5 / 100%
 

Patrick is an unknown quantity at this point

  Old

Change:

Code:
$result = mysql_query($sql, $connection);
To:

Code:
$res = mysql_query($sql, $connection);
And:

Code:
while ($myRow = mysql_fetch_array($result)) {
To:

Code:
while ($myRow = mysql_fetch_array($res)) {

Reply With Quote
09-30-2009, 08:22 PM
#4
Village Genius is offline Village Genius
Village Genius's Avatar
Status: Geek
Join date: Apr 2006
Location: Denver, CO
Expertise: Software
Software: Chrome, Notepad++
 
Posts: 6,894
iTrader: 18 / 100%
 

Village Genius will become famous soon enough

  Old

As a reference, here is now to debug SQL errors:
  • mysql_query error
    • Check connection handler
  • mysql_result or other reader error
    • Check query for errors
    • Make sure correct variables are being passed
  • mysql connection error
    • Check connection credentials
    • Check database

This coveres 99% if SQL related errors in PHP.

Reply With Quote
09-30-2009, 08:30 PM
#5
46Bit is offline 46Bit
Status: Member
Join date: Mar 2009
Location: Yorkshire
Expertise: Web Development
Software:
 
Posts: 275
iTrader: 10 / 100%
 

46Bit is on a distinguished road

Send a message via MSN to 46Bit Send a message via Skype™ to 46Bit

  Old

Originally Posted by Patrick View Post
Change:

Code:
$result = mysql_query($sql, $connection);
To:

Code:
$res = mysql_query($sql, $connection);
And:

Code:
while ($myRow = mysql_fetch_array($result)) {
To:

Code:
while ($myRow = mysql_fetch_array($res)) {
Erm, that won't achieve anything...

Anyway, here's a version that's fixed in various places, and has some minor code changes that I'd personally consider more best practices.
PHP Code:
<html>
<head>
<title>Lab 4</title>
</head>
<body>
<?
$connection 
mysql_connect("HOST_HERE""USER_HERE""PASSWORD_HERE") or die ("Kunde inte an****a!");

mysql_select_db("DB_NAME_HERE"$connection);

$input $_POST['revealThis'];

if (
$input == 'debattartiklar') {
    
$sql "SELECT * FROM debatt";
} elseif (
$input == 'eva') {
    
$sql "SELECT * FROM debatt, sports WHERE forfattare='Eva Svensson'";
} else {
    echo 
"Du valde inget alternativ, vänligen välj ett";
}
    
$result mysql_query($sql$connection);

while (
$myRow mysql_fetch_assoc($result)) {
        echo 
"<h3 style=\"line-height:1.5em; margin:0; padding:0;\">{$myRow['rubrik']}</h3>";
        echo 
"<strong>{$myRow['ingress']}</strong><br />";
        echo 
"{$myRow['brodtext']}<br />";
        echo 
"{$myRow['forfattare']}{$myRow['datum']}<br /><br />";
}

mysql_close($connection);

?>
</body>
</html>
Also take a look into mysqli instead of mysql.

Reply With Quote
09-30-2009, 08:54 PM
#6
.Cyanide is offline .Cyanide
Status: Senior Member
Join date: Mar 2006
Location: Stockholm,Sweden
Expertise: Web Design, Coding
Software: Phoshop CS3
 
Posts: 757
iTrader: 4 / 100%
 

.Cyanide is an unknown quantity at this point

Send a message via MSN to .Cyanide

  Old

Thanks guys, but I'm afraid none of your tips helped very much, I still get the same error =/

Reply With Quote
09-30-2009, 11:39 PM
#7
Gaz is offline Gaz
Gaz's Avatar
Status: Request a custom title
Join date: Apr 2007
Location: UK
Expertise: Code & Programming
Software: Coda, TextMate, Sublime 2
 
Posts: 2,097
iTrader: 26 / 100%
 

Gaz will become famous soon enough Gaz will become famous soon enough

Send a message via Skype™ to Gaz

  Old

Hang on

Reply With Quote
10-01-2009, 01:07 PM
#8
.Cyanide is offline .Cyanide
Status: Senior Member
Join date: Mar 2006
Location: Stockholm,Sweden
Expertise: Web Design, Coding
Software: Phoshop CS3
 
Posts: 757
iTrader: 4 / 100%
 

.Cyanide is an unknown quantity at this point

Send a message via MSN to .Cyanide

  Old

Problem solved guys, used UNION to join the tables together. Thanks for the help though!

Reply With Quote
Reply    


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed