Help - mysql_fetch_array() expects parameter 1 to be resource

Hi Guys, hopefully a little help again.

The purpose of this learning script is to list the data in my database on the screen.

I am receiving the following error from the script below:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/content/d/e/e/dexxxxxx/html/phptest/newtest.php on line 14

[php]

<?php // Connects to xxxxxdata and looks for data in the table go-gators and displays contents $con = mysql_connect("XXXXXdata.db.XXXX794.hostedresource.com", "XXXXXdata", "XXXXXX"); mysql_select_db("xxxxxdata"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("xxxxxdata", $con); $table="go-gators"; $result = mysql_query("SELECT * FROM $table"); // $result2 = mysql_query($myQuery) or die($myQuery."

".mysql_error()); while($row = mysql_fetch_array($result)) { echo $row['EmailName'] . " " . $row['Domain'] . " " . $row['CustName']; echo "
"; } mysql_close($con); ?>

[/php]
The data in the database is

EmailName Domain CustName ReservedDate
randy bbdawg Randy J 2012-10-12
yourname go-gators Randy J 2012-10-12

I creates this script almost from scratch, but I hit these little things I just can not locate answers for.

(I am thinking it might have something to do with the “-” in the domain go-gators (but this is just a guess))

I hope someone can help,

Thanks
Randy J

give this a shot and let me know what happens :wink:

[php]

[php]

<?php // Connects to xxxxxdata and looks for data in the table go-gators and displays contents $con = mysql_connect("XXXXXdata.db.XXXX794.hostedresource.com", "XXXXXdata", "XXXXXX"); mysql_select_db("xxxxxdata"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("xxxxxdata", $con); $result = mysql_query("SELECT * FROM go-gators") or die(mysql_error()); while($row = mysql_fetch_array($result) ) { echo $row['EmailName'] . " " . $row['Domain'] . " " . $row['CustName']; echo "
"; } mysql_close($con); ?>

[/php][/php]

Well, that got me back to the original problem I had, an error message that says -

“You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘-gators’ at line 1”

I looked at the script and removed the line 1 comments, but that made no difference.

Any other thoughts??
I do appreciate your help!
Thanks
Randy

[/php]

[php]

<?php // Connects to xxxxxdata and looks for data in the table go-gators and displays contents $con = mysql_connect("XXXXXdata.db.XXXX794.hostedresource.com", "XXXXXdata", "XXXXXX"); mysql_select_db("xxxxxdata"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("xxxxxdata", $con); $result = mysql_query("SELECT * FROM `go-gators`") or die(mysql_error()); while($row = mysql_fetch_array($result) ) { echo $row['EmailName'] . " " . $row['Domain'] . " " . $row['CustName']; echo "
"; } mysql_close($con); ?>

[/php]

I was thinking it was an error in php reading the minus sign also, give this a try

:), well once again that Works!
I see the tic’s around the word ‘go-gators’ in the select from command. Amazing, these dang little “ticks”.

Ok, in my attempt, I was trying to make the term “go-gators” a variable, as I will have unknown variations of this search term. The term will be entered by the user attempting to add their email address to the list thru an input, I have yet to create. I have tried with quotes, with tics and without but get errors on each iteration.

here is my last try at it:

[php]
$newdom = “go-gators”;
mysql_select_db(“sengdata”, $con);

$result = mysql_query("SELECT * FROM ‘$newdom’ ") or die(mysql_error());

while($row = mysql_fetch_array($result) )
{
echo $row[‘EmailName’] . " " . $row[‘Domain’] . " " . $row[‘CustName’];
echo “
”;
}
mysql_close($con);
[/php]
last error message was -
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘‘go-gators’’ at line 1

Again, I Thank You!
Randy
(plugging away, I am)

locate the following line of code in your script
[php]
$newdom = “go-gators”;
[/php]

and replace with
[php]
$newdom = “go-gators”;
[/php]

Good luck and let me know if that fixed the issue.

dash(-) is reserved character in SQL so you need to enclose any reserved character in grave sign(`)

Cool Beans thanks Wilson382, I think you are all set now :smiley: what wilson382 offered with the tics/graves in the quotations should be good. Glad we could help!

That just changed the error to :
your MySQL server version for the right syntax to use near ‘’‘go-gators’’’ at line 1"

confused, I remain!!
Randy

sorry here ya go

[php]

<?php // Connects to xxxxxdata and looks for data in the table go-gators and displays contents $con = mysql_connect("XXXXXdata.db.XXXX794.hostedresource.com", "XXXXXdata", "XXXXXX"); mysql_select_db("xxxxxdata"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("xxxxxdata", $con); $newdom = "go-gators"; $result = mysql_query("SELECT * FROM `$newdom`") or die(mysql_error()); while($row = mysql_fetch_array($result) ) { echo $row['EmailName'] . " " . $row['Domain'] . " " . $row['CustName']; echo "
"; } mysql_close($con); ?>

[/php]

once again, that took me back to my original error " server version for the right syntax to use near ‘‘go-gators’’ at line 1"

ggggrrrrrrrrrrrrrrrr

I have tried every iteration, I can think of and both of what you and Wilson382 offered. As soon as I see what you did, and I think I understand, it doesn’t work for me and I am lost again.

That dash in go-gators seems to be killing me, as it works fine without it.

Not giving up yet, but any more ideas?

Thanks for your continued Help,
Randy

Did you copy & paste plintu’s code? His syntax is correct. I just want to be sure you are using backticks and not quotes. e.g.

$newdom and not ‘$newdom’

Based on your error, it is adding single quotes, which there should be none.

right syntax to use near ‘’‘go-gators’’’ at line 1

[Php]
$newdom = “Go-gators”;

$query =“SELECT * FROM $newdom”;

OR

$query =“SELECT * FROM ‘$newdom’”;
[/Php]

Worked for me on my localhost

wilson, you posted invalid syntax with that example. You cannot use single quotes for table names.

This is invalid:
[php]
$newdom = “Go-gators”;
$query = “SELECT * FROM ‘$newdom’”;
[/php]

Well, not only is my brain old, my eyes are deceiving me as well.
Never saw that one comming “back tics”?? I had to get a magnifying glass and find it on my key board - but I did find it and YES IT WORKS!!! :o :o

Wow, that is a tough one!!

Now I have to learn about these new little devils, back tics, as well as tics (front tics I guess) or just single quote? php/sql needs some tic spray!

Oh my, thanks ALL for your quick and very helpful information. Unfortunately, I know I will be back, but I need to go now and play with my new WOKING SCRIPT.

Thanks Again M@tt, Wilson and plintu!!
Randy J

You would use backticks for table or field names and single quotes for strings.

For example:

SELECT `field` FROM `table` WHERE `field` = 'string';

I personally always use backticks on all table and field names, even when they are not necessary.

Thanks M@tt,
I am getting ready to use that same back tick info because I am attempting to do my find in the DB from a form input (get) method.
Once I capture the users desired name, I want to see if it is my database already.

(sure wish someone would read my other (script needed) post and put me out of my misery ;D) At one time i could afford to have people do the coding for me - not now!!

thanks Again
Randy
Unfortunately for You guys, I’ll be back!

Im very glad that i been of help.

Haha lmao good catch on that one m@tt. I got back here too late :smiley: Yeah them back ticks are buggers 8) But I am sure that this lesson taught at least one person about proper syntax of a query!!! Sooo many people get lazy and don’t use the quotes and back ticks as they are supposed to be used and this can cause errors when it comes to funny things being queried such as a minus sign
[php]
mysql_query(“SELECT row FROM table WHERE this=‘that’”);
[/php]

everyone should take a moment to read about the powerful backtick

http://php.net/manual/en/language.operators.execution.php

Sponsor our Newsletter | Privacy Policy | Terms of Service