Jump to content

Archived

This topic is now archived and is closed to further replies.

alarik149

how do i check if a table exists?

Recommended Posts

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

Share this post


Link to post
Share on other sites
$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..

Share this post


Link to post
Share on other sites
[!--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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[!--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 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.

[b]EDIT:[/b] And you'll have to change "table" to `1`. And don't forget to try my code out.

Share this post


Link to post
Share on other sites
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]shocerk mispelt SELECT. Just replace sleect with SELECT.[/quote]

Opps :) easy mistake :P

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Try this

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

that is correct as to the ' and "

Share this post


Link to post
Share on other sites
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");
}
?>


:)

Share this post


Link to post
Share on other sites
thanks ever so much :) and sorry for bugging you with all my replys and bug issues :).
thanks again:)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.