|
|
|
|
Thread title: Mysql error (mysql_fetch_array doesn't work?) |
|
|
|
|
|
Thread tools
Search this thread
Display Modes
|
|
09-30-2009, 07:44 PM
|
#1
|
Status: Senior Member
Join date: Mar 2006
Location: Stockholm,Sweden
Expertise: Web Design, Coding
Software: Phoshop CS3
Posts: 757
|
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"
|
|
09-30-2009, 08:03 PM
|
#2
|
Status: Member
Join date: Mar 2009
Location: Yorkshire
Expertise: Web Development
Software:
Posts: 275
|
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.
|
|
09-30-2009, 08:14 PM
|
#3
|
Status: I love this place
Join date: Apr 2007
Location: Chi-Town
Expertise: Java/C#/C++ Programmer
Software:
Posts: 543
|
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)) {
|
|
09-30-2009, 08:22 PM
|
#4
|
Status: Geek
Join date: Apr 2006
Location: Denver, CO
Expertise: Software
Software: Chrome, Notepad++
Posts: 6,894
|
As a reference, here is now to debug SQL errors: - mysql_query error
- 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.
|
|
09-30-2009, 08:30 PM
|
#5
|
Status: Member
Join date: Mar 2009
Location: Yorkshire
Expertise: Web Development
Software:
Posts: 275
|
Originally Posted by Patrick
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.
|
|
09-30-2009, 08:54 PM
|
#6
|
Status: Senior Member
Join date: Mar 2006
Location: Stockholm,Sweden
Expertise: Web Design, Coding
Software: Phoshop CS3
Posts: 757
|
Thanks guys, but I'm afraid none of your tips helped very much, I still get the same error =/
|
|
09-30-2009, 11:39 PM
|
#7
|
Status: Request a custom title
Join date: Apr 2007
Location: UK
Expertise: Code & Programming
Software: Coda, TextMate, Sublime 2
Posts: 2,097
|
|
|
10-01-2009, 01:07 PM
|
#8
|
Status: Senior Member
Join date: Mar 2006
Location: Stockholm,Sweden
Expertise: Web Design, Coding
Software: Phoshop CS3
Posts: 757
|
Problem solved guys, used UNION to join the tables together. Thanks for the help though!
|
|
|
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
|