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? Quote Link to comment 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 Quote Link to comment 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.. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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.. Quote Link to comment 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. Quote Link to comment 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 " Quote Link to comment 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 ;) Quote Link to comment 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");}?>:) Quote Link to comment 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:) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.