alarik149 Posted March 13, 2006 Share Posted March 13, 2006 hi people.I want to know how do I check if a table exists in a certain database? Link to comment https://forums.phpfreaks.com/topic/4825-how-do-i-check-if-a-table-exists/ Share on other sites More sharing options...
alarik149 Posted March 13, 2006 Author Share Posted March 13, 2006 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 Link to comment https://forums.phpfreaks.com/topic/4825-how-do-i-check-if-a-table-exists/#findComment-16992 Share on other sites More sharing options...
shocker-z Posted March 13, 2006 Share Posted March 13, 2006 $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.. Link to comment https://forums.phpfreaks.com/topic/4825-how-do-i-check-if-a-table-exists/#findComment-16993 Share on other sites More sharing options...
Masna Posted March 13, 2006 Share Posted March 13, 2006 [!--quoteo(post=354508:date=Mar 13 2006, 02:42 PM:name=ADRlAN)--][div class=\'quotetop\']QUOTE(ADRlAN @ Mar 13 2006, 02:42 PM) [snapback]354508[/snapback][/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...[code]$query = ("SELECT `c` FROM `1` WHERE `a` = 'somecontent' LIMIT 1");$fetch = mysql_fetch_assoc($query);extract($fetch);echo $c; //will output 'c' content[/code]Good luck. Link to comment https://forums.phpfreaks.com/topic/4825-how-do-i-check-if-a-table-exists/#findComment-16996 Share on other sites More sharing options...
alarik149 Posted March 13, 2006 Author Share Posted March 13, 2006 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 5Table exists becuase that was a count returned"if The table exists i get the correct answer. Link to comment https://forums.phpfreaks.com/topic/4825-how-do-i-check-if-a-table-exists/#findComment-17002 Share on other sites More sharing options...
Masna Posted March 13, 2006 Share Posted March 13, 2006 [!--quoteo(post=354518:date=Mar 13 2006, 02:54 PM:name=ADRlAN)--][div class=\'quotetop\']QUOTE(ADRlAN @ Mar 13 2006, 02:54 PM) [snapback]354518[/snapback][/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 5Table 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.[b]EDIT:[/b] And you'll have to change "table" to `1`. And don't forget to try my code out. Link to comment https://forums.phpfreaks.com/topic/4825-how-do-i-check-if-a-table-exists/#findComment-17006 Share on other sites More sharing options...
shocker-z Posted March 13, 2006 Share Posted March 13, 2006 [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]shocerk mispelt SELECT. Just replace sleect with SELECT.[/quote]Opps :) easy mistake :P Link to comment https://forums.phpfreaks.com/topic/4825-how-do-i-check-if-a-table-exists/#findComment-17008 Share on other sites More sharing options...
alarik149 Posted March 13, 2006 Author Share Posted March 13, 2006 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. Link to comment https://forums.phpfreaks.com/topic/4825-how-do-i-check-if-a-table-exists/#findComment-17010 Share on other sites More sharing options...
shocker-z Posted March 13, 2006 Share Posted March 13, 2006 ahh yeah never thought about that.. hmmmtry<?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.. Link to comment https://forums.phpfreaks.com/topic/4825-how-do-i-check-if-a-table-exists/#findComment-17015 Share on other sites More sharing options...
alarik149 Posted March 13, 2006 Author Share Posted March 13, 2006 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 6Table was foundon 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. Link to comment https://forums.phpfreaks.com/topic/4825-how-do-i-check-if-a-table-exists/#findComment-17017 Share on other sites More sharing options...
shocker-z Posted March 13, 2006 Share Posted March 13, 2006 Try this$query=mysql_query("show tables like '$table'");that is correct as to the ' and " Link to comment https://forums.phpfreaks.com/topic/4825-how-do-i-check-if-a-table-exists/#findComment-17020 Share on other sites More sharing options...
alarik149 Posted March 13, 2006 Author Share Posted March 13, 2006 now i get Table was found nomatter what $table is :p ;) Link to comment https://forums.phpfreaks.com/topic/4825-how-do-i-check-if-a-table-exists/#findComment-17024 Share on other sites More sharing options...
shocker-z Posted March 13, 2006 Share Posted March 13, 2006 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");}?>:) Link to comment https://forums.phpfreaks.com/topic/4825-how-do-i-check-if-a-table-exists/#findComment-17031 Share on other sites More sharing options...
alarik149 Posted March 13, 2006 Author Share Posted March 13, 2006 thanks ever so much :) and sorry for bugging you with all my replys and bug issues :).thanks again:) Link to comment https://forums.phpfreaks.com/topic/4825-how-do-i-check-if-a-table-exists/#findComment-17033 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.