New Question: Checking for a table

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
XxLaSeRxX
Forum Newbie
Posts: 7
Joined: Sat Jun 19, 2004 8:24 pm

New Question: Checking for a table

Post by XxLaSeRxX »

Sorry guys Im really new to this. I did searches on google and found lots of stuff on how to create a table in my sql. Im trying to create a table yet its not working and Im getting an error

Code: Select all

<?php
$sql = "CREATE TABLE '$id' (username text(16),rating int(1));";
mysql_query($sql,$lk) or die(mysql_error());
?>
it says my syntax is wrong somehow.. I tried many variants of the create table command, and its still not working.. I know most of you will probably look at this code and just think "is he retarded?". Im sorry for that lol.

Thanks in advance.
Last edited by XxLaSeRxX on Mon Jun 21, 2004 3:54 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$sql= "CREATE TABLE `$id`(`username` varchar(16), `rating` int(1));";

try that..
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

No semicolon need in your query string. The type TEXT doesn't have any options, like length. The type INT is ok but you may consider using TINYINT to save space.

http://dev.mysql.com/doc/mysql/en/CREATE_TABLE.html
http://dev.mysql.com/doc/mysql/en/Numeric_types.html

Try this?

Code: Select all

$sql = "CREATE TABLE '$id' (username text, rating int(1))"; 
mysql_query($sql,$lk) or die(mysql_error());
probably look at this code and just think "is he retarded?"
Never crossed my mind.
XxLaSeRxX
Forum Newbie
Posts: 7
Joined: Sat Jun 19, 2004 8:24 pm

Post by XxLaSeRxX »

For some reason my code for checking if a table is in the database was working last night and stopped working today..

Code: Select all

<?php
$tExist = "SHOW TABLES LIKE '$id'";
if(mysql_query($tExist,$lk))
     { 
      echo "Here are your ratings";
     }
?>
For some reason this if statement is always true. Maybe my code is totally wrong but Im not sure..

Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

uh.. you might want to check if any rows were returned.. the if just checks if mysql_query() succeded in running.. doesn't mean the table is used or not
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

XxLaSeRxX
Forum Newbie
Posts: 7
Joined: Sat Jun 19, 2004 8:24 pm

Post by XxLaSeRxX »

Im not checking if the table is being used.. I just want to check to see if the table is even there.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if the table isn't there, you should get 0 rows back.
XxLaSeRxX
Forum Newbie
Posts: 7
Joined: Sat Jun 19, 2004 8:24 pm

Post by XxLaSeRxX »

I tried the num_rows command and it works but I am getting a warning.

Heres my code:

Code: Select all

<?php
$tExist = "SHOW TABLES LIKE '$id'";
//$tExist = "SELECT * FROM '$id'";
$temp = mysql_query($tExist,$lk);
if(mysql_num_rows($temp, $lk))
{ 
	echo "Here are your ratings";
	exit;
}
?>
the warning I am getting is:Warning: Wrong parameter count for mysql_num_rows() in \admin.php on line 23

I tried show and select, but gave errors.

Thanks,
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

You dont need the $lk var in it, only the database result:

mysql_num_rows -- Get number of rows in result
Description
int mysql_num_rows ( resource result)
Post Reply