/    Sign up×
Community /Pin to ProfileBookmark

random SQL error

I have an ajax call in the middle of a webform to populate a select box based on a previous select box. About 1 out of 5 times I run this, I get a “not a valid mysql result resource” error. However the rest of the time it runs perfectly. It’s random, so I’m baffled on why it’s happening, but I’m hosting a fairly slow server, so I’m wondering if it could trying to return the results slower then it’s trying to display them.

Regardless, I recently switched to MySQL from SQL and there are some slight syntex issues that have hung me up in the past, so I thought I might post my code here and see if anyone spots an error.

[CODE]$num = $_GET[‘num’];
echo “<select name=subcat>”;
echo “<option>”;
$recordset = mysql_query (“SELECT * FROM `subcat` where `category` = ‘$num'”, $handle);
while($arr = mysql_fetch_row($recordset))
{
$style = trim($arr[2]);
echo “<option>$style”;
}
echo “</select>”;[/CODE]

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@kattenJan 16.2008 — Try to replace the follwing it could be the num that is not a valid value everytime.

Replace this
[code=php]
$recordset = mysql_query ("SELECT * FROM subcat where category = '$num'", $handle);
[/code]


With this
[code=php]
$recordset = mysql_query ("SELECT * FROM subcat where category = '$num'", $handle) or die(mysql_error());
[/code]
Copy linkTweet thisAlerts:
@chazzyJan 17.2008 — Edit:

Read your message wrong.

Is it somehow correlated to teh value of $num?
Copy linkTweet thisAlerts:
@ss1289Jan 17.2008 — The only thing I can see is that you could take the ` out of the SELECT statement.

[code=php]$recordset = mysql_query ("SELECT * FROM subcat WHERE category = '$num'", $handle)[/code]
Copy linkTweet thisAlerts:
@kattenJan 17.2008 — If the num value is not of a valid type or an accidential sql injection then the query will not be valid ither thus giving that error.
Copy linkTweet thisAlerts:
@Reli4ntJan 17.2008 — print the sql query to the page (in html comments if the site is live) and check if the value for $num matches an actual category in your db whenever you get the error.

If for example, your categories are actually 0-4 and you randomly set $num to be 1-5 you will get that error 1 in 5 times.
Copy linkTweet thisAlerts:
@Doc_ThirstauthorJan 17.2008 — Good call, I'll check it out and see what its spitting out. $num is dictated by the choice of the category, and I checked the category table to make sure there is not any weirdness in the table, but perhaps I missed something. Thanks to everyone for your replies, you have given me some food for thought.
Copy linkTweet thisAlerts:
@Doc_ThirstauthorJan 17.2008 — Try to replace the follwing it could be the num that is not a valid value everytime.

Replace this
[code=php]
$recordset = mysql_query ("SELECT * FROM subcat where category = '$num'", $handle);
[/code]


With this
[code=php]
$recordset = mysql_query ("SELECT * FROM subcat where category = '$num'", $handle) or die(mysql_error());
[/code]
[/QUOTE]


OK this helped a ton. The page was not grabbing the db select which was specified in the parent page. I have not idea why it was working at all. Added an [I]include 'connection doc';[/I] and we are in good shape. Again, thanks for the replies!
×

Success!

Help @Doc_Thirst 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.19,
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,
)...