Jump to content


Photo

how do i check if a table exists?


  • Please log in to reply
13 replies to this topic

#1 alarik149

alarik149
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 13 March 2006 - 01:39 PM

hi people.I want to know how do I check if a table exists in a certain database?
Tired of math homework ? Just visit http://www.solvemymath.com for a free online math solver and calculator.
Videochat bucuresti studio -> http://www.GirlAddicts.com
About the adult video chat business -> www.girlplan.com

#2 alarik149

alarik149
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 13 March 2006 - 02:42 PM

and another question please.how do I read the value of a certain row of a certain field into a variable.

I mean for example I have tablename named '1' fieldnames are 'a''b''c''d'.How do I get the value of fieldname c,where fieldname a='somecontent',into a variable named $test.I hope i was clear enough.
Thanks a lot
Tired of math homework ? Just visit http://www.solvemymath.com for a free online math solver and calculator.
Videochat bucuresti studio -> http://www.GirlAddicts.com
About the adult video chat business -> www.girlplan.com

#3 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 13 March 2006 - 02:44 PM

$query=mysql_query("sleect count(*) AS 'count' FROM table");
$result=mysql_fetch_array($query);
if ($result['count'] !== '') {
echo("Table exists becuase that was a count returned");
} else {
echo("Table doesn't exist, because no count was returned");
}


I think that should do it..
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#4 Masna

Masna
  • Staff Alumni
  • Advanced Member
  • 288 posts
  • LocationNew York

Posted 13 March 2006 - 02:46 PM

[!--quoteo(post=354508:date=Mar 13 2006, 02:42 PM:name=ADRlAN)--][div class=\'quotetop\']QUOTE(ADRlAN @ Mar 13 2006, 02:42 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
and another question please.how do I read the value of a certain row of a certain field into a variable.

I mean for example I have tablename named '1' fieldnames are 'a''b''c''d'.How do I get the value of fieldname c,where fieldname a='somecontent',into a variable named $test.I hope i was clear enough.
Thanks a lot
[/quote]

To address your first question, just search the manual under MySQL. As for your second...

$query = ("SELECT `c` FROM `1` WHERE `a` = 'somecontent' LIMIT 1");
$fetch = mysql_fetch_assoc($query);
extract($fetch);
echo $c; //will output 'c' content

Good luck.
I like LoL.

#5 alarik149

alarik149
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 13 March 2006 - 02:54 PM

ok i`ll search the manualfor the second question.as for the first i get this error if table dosen`t exist :

"Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\VertrigoServ\_htdocs\championships\table_exists.php on line 5
Table exists becuase that was a count returned"

if The table exists i get the correct answer.
Tired of math homework ? Just visit http://www.solvemymath.com for a free online math solver and calculator.
Videochat bucuresti studio -> http://www.GirlAddicts.com
About the adult video chat business -> www.girlplan.com

#6 Masna

Masna
  • Staff Alumni
  • Advanced Member
  • 288 posts
  • LocationNew York

Posted 13 March 2006 - 03:04 PM

[!--quoteo(post=354518:date=Mar 13 2006, 02:54 PM:name=ADRlAN)--][div class=\'quotetop\']QUOTE(ADRlAN @ Mar 13 2006, 02:54 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
ok i`ll search the manualfor the second question.as for the first i get this error if table dosen`t exist :

"Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\VertrigoServ\_htdocs\championships\table_exists.php on line 5
Table exists becuase that was a count returned"

if The table exists i get the correct answer.
[/quote]

shocerk mispelt SELECT. Just replace sleect with SELECT.

EDIT: And you'll have to change "table" to `1`. And don't forget to try my code out.
I like LoL.

#7 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 13 March 2006 - 03:08 PM

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]shocerk mispelt SELECT. Just replace sleect with SELECT.[/quote]

Opps :) easy mistake :P
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#8 alarik149

alarik149
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 13 March 2006 - 03:15 PM

I noticed the 'select' misstake before posting that error.that`s not why im having problems with this script .Though I don`t understand what you mean by "And you'll have to change "table" to `1` ".I replaced the table name to an unexisting table.The script works only for existing tables or I can`t find the error.her`s what I`ve done:

<?php
$db = mysql_connect('localhost', 'user', 'password);
mysql_select_db('championships', $db);
$query=mysql_query("SELECT count(*) AS 'count' FROM asdd");
$result=mysql_fetch_array($query);
if ($result['count'] !== '') {
echo("Table exists becuase that was a count returned");
} else {
echo("Table doesn't exist, because no count was returned");
}
?>


the connection to the database is done correct.
Tired of math homework ? Just visit http://www.solvemymath.com for a free online math solver and calculator.
Videochat bucuresti studio -> http://www.GirlAddicts.com
About the adult video chat business -> www.girlplan.com

#9 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 13 March 2006 - 03:24 PM

ahh yeah never thought about that.. hmmm
try

<?php
$table='asdd';
$db = mysql_connect('localhost', 'user', 'password');
mysql_select_db('championships', $db);
$query=mysql_query("show tables like "$table");
$table = mysql_fetch_array($query);
if ($table[0] == "$table") {
echo("Table was found");
} else {
echo("Table was NOT found");
}
?>


i think that may work.. but im at work so no way of me testing..
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#10 alarik149

alarik149
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 13 March 2006 - 03:32 PM

i`m not a guru of PHP&MySQL but I see an error here "("show tables like "$table");" i modified it to ("show tables like $table"); and to ("show tables like "$table""); still i get errors like :
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\VertrigoServ\_htdocs\championships\table_exists.php on line 6
Table was found
on line 6 is $table = mysql_fetch_array($query);
I don`t know where " or where ' or where nothing goes,not yet,I need to finish something and I`ll chear the information soon I hope :P.

Tired of math homework ? Just visit http://www.solvemymath.com for a free online math solver and calculator.
Videochat bucuresti studio -> http://www.GirlAddicts.com
About the adult video chat business -> www.girlplan.com

#11 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 13 March 2006 - 03:39 PM

Try this

$query=mysql_query("show tables like '$table'");

that is correct as to the ' and "
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#12 alarik149

alarik149
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 13 March 2006 - 03:50 PM

now i get Table was found nomatter what $table is :P ;)
Tired of math homework ? Just visit http://www.solvemymath.com for a free online math solver and calculator.
Videochat bucuresti studio -> http://www.GirlAddicts.com
About the adult video chat business -> www.girlplan.com

#13 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 13 March 2006 - 04:11 PM

Got it!

well finaly got logged into my online text editor and tested and the working code is...


<?php
$table='jobs';
$table='asdd';
$db = mysql_connect('localhost', 'user', 'password');
mysql_select_db('championships', $db);
$query=mysql_query("show tables like '$table'");
$table = mysql_fetch_array($query);
if ($table[0] = "$table") {
echo("Table was found");
} else {
echo("Table was NOT found");
}
?>


:)

www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#14 alarik149

alarik149
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 13 March 2006 - 04:20 PM

thanks ever so much :) and sorry for bugging you with all my replys and bug issues :).
thanks again:)
Tired of math homework ? Just visit http://www.solvemymath.com for a free online math solver and calculator.
Videochat bucuresti studio -> http://www.GirlAddicts.com
About the adult video chat business -> www.girlplan.com




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users