/    Sign up×
Community /Pin to ProfileBookmark

Help to return true/false from function

Hi

I have tried to make a simple function that checks if a table in database exists, and return true or false, but I can’t seem to make it work!

Any ideas why??

[code=php]function check_table($table){
$table_check = “SHOW TABLES LIKE ‘$table'”;
$table_check_result = mysql_query($table_check)
or die ($table_check_error = mysql_error());

if (empty($table_check_result)){
return true;
}
else{
return false;
}

}[/code]

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 31.2010 — Assuming the query syntax is valid, [b]$table_check_result[/b] will always be true (actually a valid query result resource), regardless of whether the table exists. You probably can do a mysql_num_rows() to find out if there was a non-zero number of matches returned by the query.
Copy linkTweet thisAlerts:
@dbkauthorOct 31.2010 — Hi NogDog

Yes.. your absolute right, and this is how I made it work:

[code=php]function check_table($table){
$table_check = "SHOW TABLES LIKE '$table'";
$table_check_result = mysql_query($table_check);
//or die ($table_check_error = mysql_error());
$table_check_count = mysql_num_rows($table_check_result);

if ($table_check_count == 0){
return true;
}
else{
return false;

}
}[/code]


Thanks!
Copy linkTweet thisAlerts:
@CharlesNov 01.2010 —  if ($table_check_count == 0){
return true;
}
else{
return false; <br/>
}
Is a lot like writing [font=monospace]10 * 10[/font] instead of [font=monospace]100[/font]. Better to use return $table_check_count == 0;
Copy linkTweet thisAlerts:
@NogDogNov 01.2010 — You could even cut out another step:
[code=php]
return mysql_num_rows($table_check_result) == 0;
[/code]

?
Copy linkTweet thisAlerts:
@CharlesNov 01.2010 — Myself, I would make that whole function a one-liner. But that's just me.
Copy linkTweet thisAlerts:
@NogDogNov 01.2010 — Myself, I would make that whole function a one-liner. But that's just me.[/QUOTE]

I thought about it, but sometimes you can get too much of a good thing, impacting readability and modifiability (to coin a word). ? (Especially since I'd probably add some sort of error-checking after the query, just in case.)
×

Success!

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