/    Sign up×
Community /Pin to ProfileBookmark

What if a MySQL query target does not exist?

If I’m selecting a row that has a specific property1 and property2, but that row does not exist…….

Can I put this:

[code=php]
$sql = mysql_fetch_array(mysql_query(“SELECT property3 FROM table WHERE table.property1 = ‘blah’ AND table.property2 = ‘blah2’;”));

if(“Empty Nested Set”!=$sql) {

do this if it does exist }

else {

do this if it doesn’t exist }
[/code]

Would that work? Thanks in Advance!

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@ShrineDesignsJul 28.2005 — example[code=php]$result = @mysql_query("SELECT property3 FROM table WHERE table.property1 = 'blah' AND table.property2 = 'blah2'");

if(!$result || !mysql_num_rows($result))
{
// query failed
}
else
{
// query successful
$row = mysql_fetch_array($result, MYSQL_ASSOC);
// ...
}
[/code]
Copy linkTweet thisAlerts:
@theuedimasterauthorJul 28.2005 — Wow, thanks a lot man! I have just one question, what is the @ for before the mysql_query? Also, do you have to pu that MYSQL_ASSOC in the mysql_fetch_array function?
Copy linkTweet thisAlerts:
@NogDogJul 28.2005 — The '@' suppresses error reporting on the function call. It's OK to do that as long as you have the code check the result of the query and handle any false condition as appropriate. My personal stylistic preference (not better, just different) would be to do:
[code=php]
$query = "SELECT property3 FROM table WHERE table.property1 = 'blah' AND table.property2 = 'blah2'";
$result = mysql_query($query) or die("Query failed: ".mysql_error()." -- ".$query);
# the die() message will show the MySQL error message and the exact query text
# once you've debugged the script, remove the stuff after "Query failed" before
# putting the script in production, as such info could be a security hole
if(mysql_num_rows($result) == 0)
{
# handle no rows returned situation
}
else
{
# handle 1 or more rows returned
}
[/code]

The MYSQL_ASSOC constant tells mysql_fetch_array to only return the associative array for that result row. You can do the same with less typing as:
[code=php]
mysql_fetch_assoc($result);
[/code]

Without the 2nd parameter to mysql_fetch_array(), you get both a numerically indexed array plus the associative array.
Copy linkTweet thisAlerts:
@psyberdemonDec 25.2007 — [code=php]if(($_GET[m] == "rateup" || $_GET[m] == "ratedown") && $_GET[link]) {
$findlink = mysql_query("SELECT * FROM db_links.links WHERE id='$_GET[link]'") or die(mysql_error());
$findrate = mysql_query("SELECT * FROM db_links.ratings WHERE linkid='$_GET[link]' AND username='$_SESSION[username]'") or die(mysql_error());
//$findrate is NOT SUPPOSED to find anything. the whole purpose of this function is to only work if the user has NOT rated the link with id of $_GET[link]
if(mysql_num_rows($findlink) == 1 && (!$findrate || mysql_num_rows($findrate) == 0)) {
//IF breaks here to ELSE for unknown reason unless i have this IF statement in its place
// if(mysql_num_rows($findlink) == 1) {
// it refuses to do both mysql_num_row functions.
$rating = mysql_query("INSERT INTO shoutbze_links.ratings(username,linkid) VALUES('$_SESSION[login_user]','$_GET[link]')") or die(mysql_error());

if($_GET[m] == "rateup") {
$rcount = mysql_query("UPDATE shoutbze_links.links SET rateups=rateups+1, rates=rates+1 WHERE id='$_GET[link]'") or die(mysql_error());
}
if($_GET[m] == "ratedown") {
$rcount = mysql_query("UPDATE shoutbze_links.links SET ratedowns=ratedowns+1, rates=rates+1 WHERE id='$_GET[link]'") or die(mysql_error());
}
if(mysql_affected_rows($rating) == 1 && mysql_affected_rows($rcount) == 1) {
echo '<div class="ptitle"><p>success</p></div>';
echo '<div class="content"><p>The link has been rated by you and has been calculated into the database.</p></div>';
}
if(mysql_affected_rows($rating) == 1 && mysql_affected_rows($rcount) == 1) {
//mysql breaks here (line 237) with this error
// "Warning: mysql_affected_rows(): supplied argument is not a valid MySQL-Link resource in /home/dir/html/*file*.php on line 237


echo '<div class="title"><p>success</p></div>';
echo '<div class="content"><p>The link has been rated by you and has been calculated into the database.</p></div>';
}
}
else {
echo '<div class="title"><p>failure</p></div>';
echo '<div class="content"><p>Your link (id#'.$_GET[link].') could not be found in the database</p></div>';
echo '<meta http-equiv="Refresh" content="2;url='.$refer.'" />';
}

}[/code]


:mad:
×

Success!

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