Jump to content

how do i check if a table exists?


alarik149

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
$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..
[!--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.
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.
[!--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.
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.
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..
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.
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");
}
?>


:)

Archived

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

×
×
  • Create New...

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.