/    Sign up×
Community /Pin to ProfileBookmark

Querystrings, SQL… getting error message

Where did I go wrong? I’m getting the following error message: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in (…)/dbtest2.php on line 24

Line 24 is this: while($row = mysql_fetch_array($rs, MYSQL_ASSOC))

dbtest2.php:

[code=php]
<?php
(my databse connection details go here…)

$sql = “SELECT id, test1, test2 FROM testtabell WHERE test1={$_GET[‘test1’]}”;
$rs = mysql_query($sql);
echo “<table><tr>”;
echo “<th>id</th>”;
echo “<th>test1</th>”;
echo “<th>test2</th></tr>”;
while($row = mysql_fetch_array($rs, MYSQL_ASSOC))
{
echo “<tr><td>{$row[‘id’]}</td>”;
echo “<td>{$row[‘test1’]}</td>”;
echo “<td>{$row[‘test2’]}</td></tr>”;
}
mysql_close($conn);
echo “</table>”;
?>
[/code]

The above page get the querystring from the links created in the following page, dbtest.php (this page seems to be working just fine, by the way — only the above-mentioned page, dbtest2.php, throws an error):

[code=php]
<?php
(again, my databse connection details go here…)

$sql = ‘SELECT id, test1, test2 FROM testtabell’;
$rs = mysql_query($sql);
echo “<table><tr>”;
echo “<th>id</th>”;
echo “<th>test1</th>”;
echo “<th>test2</th></tr>”;
while($row = mysql_fetch_array($rs, MYSQL_ASSOC))
{
echo “<tr><td>{$row[‘id’]}</a></td>”;
echo “<td><a href=”dbtest2.php?test1={$row[‘test1’]}”>{$row[‘test1’]}</a></td>”;
echo “<td>{$row[‘test2’]}</td></tr>”;
}
mysql_close($conn);
echo “</table>”;
?>
[/code]

Now, it works as it should if I use the ID part as a querystring but it doesn’t work with test1. So, once again: where did I go wrong?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@chazzyMar 15.2006 — you're never checking that your query's working

replace
[code=php]
$sql = "SELECT id, test1, test2 FROM testtabell WHERE test1={$_GET['test1']}";
$rs = mysql_query($sql);
[/code]

with
[code=php]
$sql = "SELECT id, test1, test2 FROM testtabell WHERE test1={$_GET['test1']}";
$rs = mysql_query($sql) or die("Unable to perform query. Reason: ".mysql_error()); [/code]
Copy linkTweet thisAlerts:
@J_KarlssonauthorMar 15.2006 — Thanks for the reply!

Now it says "Unknown column 'qwe' in 'where clause'" (when I go to the page dbtest2.php?test1=qwe). The thing is, and I double-checked the database, that I do have a field called test1 in which the value of the first row is qwe. Any ideas?
Copy linkTweet thisAlerts:
@chazzyMar 15.2006 — for non-int columns, the data should be encapsulated in single quotes (')

I'm assuming that test1 isn't an int column, so you should use:

[code=php]
$sql = "SELECT id, test1, test2 FROM testtabell WHERE test1='".$_GET['test1']."'";
$rs = mysql_query($sql);
[/code]


Just curious, why are you selecting test1 if you already know what it is?
Copy linkTweet thisAlerts:
@J_KarlssonauthorMar 15.2006 — Thank you so much, that did the trick!

And I don't actually know what test1 is (eh...I think), because it's not always the same value, plus that I build the links to dbtest2.php dynamically (in dbtest.php, as I showed in my first post). Anyway, I'm just testing here, and I have some ideas for a real-world project, where my SQL statements (hopefully) would make sense.
Copy linkTweet thisAlerts:
@chazzyMar 15.2006 — just to be technical....

since you have:

[colorcode=php] $sql = "SELECT id, test1, test2 FROM testtabell WHERE test1='".$_GET['test1']."'";[/colorcode]

You actually always know that test1 is $_GET['test1'] so instead of select test1, if you want to display it, you can just do:

[colorcode=php] $sql = "SELECT id, test2 FROM testtabell WHERE test1='".$_
GET['test1']."'";[/colorcode]

and in your loop use (for page 2):

[colorcode=php]

<?php

//(my databse connection details go here...)

$sql = "SELECT id, test2 FROM testtabell WHERE test1='".$_GET['test1']."'";

$rs = mysql_query($sql);

echo "<table><tr>";

echo "<th>id</th>";

echo "<th>test1</th>";

echo "<th>test2</th></tr>";

while($row = mysql_fetch_array($rs, MYSQL_ASSOC))

{

echo "<tr><td>{$row['id']}</td>";

echo "<td>".$_
GET['test1']."</td>";

echo "<td>{$row['test2']}</td></tr>";

}

mysql_close($conn);

echo "</table>";

?>

[/colorcode]

It'll save you a little bit on packet transmission.
Copy linkTweet thisAlerts:
@J_KarlssonauthorMar 15.2006 — Oh, now I see it! Thanks for the tip! I have a lot to learn...
×

Success!

Help @J_Karlsson 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.20,
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,
)...