/    Sign up×
Community /Pin to ProfileBookmark

understanding errors…

I’ve put my test script up on the server, and the error I’m getting makes no sense to me:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/mysite/public_html/joe/querytest2.php on line 39

This is the PHP line, with some before and after lines…

[code=php]
function make_fill_table() {
$sql = “SELECT item, picname, thumbname, itemdisc, itemprice, itemstatus, FROM $table_name ORDER BY item DESC”;
$result = mysql_query($sql);
if (!result) {
print mysql_error() . “ERROR – query failed”;
}
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$content .='<tr>’;
$content .='<td align=”center”>’;
[/code]

and here is the DB opening stuff (with personal info xxx’ed)

[code=php]
$dbh=mysql_connect (“localhost”, “xxxxx_joetest”, “password”) or die (‘Cannot connect to Database – error: ‘ . mysql_error () );
mysql_select_db(“xxxxx_joetest1”,$dbh);
[/code]

Any thoughts? What have I done wrong.

I’m trying to select from Database xxxxx_joetest1, from table audubon.

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@AdamGundryFeb 13.2004 — There is an error in your query, so $result is not valid, and the test you put in is not working. You need to remove the bold comma from the following line:

$sql = "SELECT item, picname, thumbname, itemdisc, itemprice, itemstatus[b],[/b] FROM $table_name ORDER BY item DESC";

Also, to detect query errors more easily you could do something like this:

$result = mysql_query($sql) or die("error - query failed: " . mysql_error());

At the moment, your if test has a missing dollar sign, so it is trying to use result instead of $result.

Adam
Copy linkTweet thisAlerts:
@BigjohnauthorFeb 13.2004 — Adam!

Thanks. That, and a spelling typo and I fixed it.

Now can I ask one more dumb question?

Can I set a variable outside the PHP - $table_name

and call it in the PHP?

[code=php]
function make_fill_table() {
$sql = $sql = 'SELECT * '
. ' FROM ornith'
. ' WHERE subclass = "audubon"';
$result = mysql_query($sql);
[/code]


I want to replace /ornith/ with the right table for the page...

If I do that, I can probably build most of this into files that will be php-included, simplifying the site.

Another question - how to add CSS styles to the generated ouput....

John
Copy linkTweet thisAlerts:
@AdamGundryFeb 13.2004 — Normally, you would probably pass $table_name as an argument (see http://www.php.net/manual/en/functions.arguments.php). Alternatively, you can use the global keyword (see http://www.php.net/manual/en/language.variables.scope.php).

To add CSS, simply use echo/print/include/whatever like normal.

Adam
Copy linkTweet thisAlerts:
@BigjohnauthorFeb 13.2004 — [i]Originally posted by AdamGundry [/i]

[B]Normally, you would probably pass $table_name as an argument (see http://www.php.net/manual/en/functions.arguments.php). Alternatively, you can use the global keyword (see http://www.php.net/manual/en/language.variables.scope.php).



To add CSS, simply use echo/print/include/whatever like normal.



Adam [/B]
[/QUOTE]


so if I define a:hover in CSS, my a href ouput via the print function will be formatted? Cool. Any special warnings I should be aware of?

On the variable, then, you're saying that :
[code=php]
function make_fill_table($category, $sub) {
$sql = $sql = 'SELECT * '
. ' FROM $category'
. ' WHERE subclass = $sub';
$result = mysql_query($sql);
if (!$result) {
print mysql_error() . "ERROR - query failed";
}
[/code]


is the way to do it? Do I define the variable right before I do the include?

Thanks AGAIN!

I learn so MUCH!
Copy linkTweet thisAlerts:
@AdamGundryFeb 13.2004 — Yes, but bear in mind you have to use double quotes when you want PHP to parse the string to replace variables.

Adam
Copy linkTweet thisAlerts:
@BigjohnauthorFeb 13.2004 — [i]Originally posted by AdamGundry [/i]

[B]Yes, but bear in mind you have to use double quotes when you want PHP to parse the string to replace variables.



Adam [/B]
[/QUOTE]


An example please? I'm a severe newbie here...

John
Copy linkTweet thisAlerts:
@AdamGundryFeb 13.2004 — With this section:

$sql = $sql = 'SELECT * '

. ' FROM $category'

. ' WHERE subclass = $sub';

you need to use double quotes, so that PHP will replace $category and $sub with the contents of the variables. If you use single quotes, the query will contain the names of the variables instead of their values. So, use this instead:

$sql = "SELECT * "

. " FROM $category"

. " WHERE subclass = $sub";

Adam
Copy linkTweet thisAlerts:
@BigjohnauthorFeb 13.2004 — [i]Originally posted by AdamGundry [/i]

[B]With this section:



$sql = $sql = 'SELECT * '

. ' FROM $category'

. ' WHERE subclass = $sub';



you need to use double quotes, so that PHP will replace $category and $sub with the contents of the variables. If you use single quotes, the query will contain the names of the variables instead of their values. So, use this instead:



$sql = "SELECT * "

. " FROM $category"

. " WHERE subclass = $sub";



Adam [/B]
[/QUOTE]


WOW. Ok, so PHP is context sensitive to the type of QUOTATION MARK? Bet that makes for a lot of headscratching errors out here in newbie land....

Thanks! This, plus the INCLUDE(top.html) type stuff is going to make building this site MUCH easier!!

John
×

Success!

Help @Bigjohn 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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...