Php table not creating

PHP MySQL (PDO) CREATE TABLE does not create tables (and in fact, does nothing)

I have been struggling since this morning, trying to write an install.php file that will insert three empty tables (users, sessions, posts) into a database. The SQL code I’m using is valid when I inject it with PHPMyAdmin, but apparently the way I’m handling it in PHP is wrong, because when I run my install.php file my database remains empty. Here’s my code:

setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $con->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $q="CREATE TABLE posts ( title varchar(150) NOT NULL, body text NOT NULL, created varchar(100) NOT NULL, user varchar(40) NOT NULL, id int(20) NOT NULL AUTO_INCREMENT, PRIMARY KEY (id), UNIQUE KEY id (id) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ; CREATE TABLE sessions ( session_id varchar(40) NOT NULL, data text NOT NULL, last_activity int(11) NOT NULL, PRIMARY KEY (session_id) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; CREATE TABLE users ( id int(11) NOT NULL AUTO_INCREMENT, username varchar(40) NOT NULL, password varbinary(250) NOT NULL, email varchar(40) NOT NULL, salt varchar(20) NOT NULL, name varchar(50) NOT NULL DEFAULT '', PRIMARY KEY (id), UNIQUE KEY email (email), UNIQUE KEY username (username), UNIQUE KEY id (id) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 "; $stmt = $con->prepare($q); $stmt->execute(); echo "success"; > catch (PDOException $e) < $e->getMessage(); > ?> 

I am assuming that it’s some idiotic mistake on my part (since my last 3 headaches were also quite silly) but at this moment I really can’t figure it out. Any ideas?

Читайте также:  Css have multiple transitions

Источник

tables not being created in database

Ok, I’m following a youtube guide on how to create a very simple blogging system using PHP/MySQL as I’d like to get to learn these 2 languages a bit more. I’m creating this in my localhost, permissions set-up correctly. The problem is, when I go onto localhost/tables.php, it comes up as white screen which it’s supposed to, but it’s not creating the relevant tables within the database? Here’s the code I’m using: mysql.php

Nothing is appearing in the error log which is frustrating and not helping me diagnose the problem. Any help would be much appreciated! Thanks.

try echoing mysql error using mysql_connect() or die(mysql_error()) and mysql_select_db() or die(mysql_error())

@MohammadFaisal I tried this but nothing got threw up in the error log nor in the browser, just a white screen.

2 Answers 2

As you have no errors run this code to show if you have created created the table ENTRIES.

 if (mysql_num_rows($result) > 0) < while ($row = mysql_fetch_assoc($result)) < print_r($row); >> ?> 

NOTE As you are starting with MySQL you would be advised to use PDO in place of the deprecated mysql_.

EDIT Following comments the following code lists databases and tables on server(Note uses deprecated mysql_ function).

Ensure that the proper parameters replace «XXX».

Database . "\n"; echo ""; > echo ""; $result = mysql_list_tables($database); $num_rows = mysql_num_rows($result); for ($i = 0; $i < $num_rows; $i++) < echo "Table: ", mysql_tablename($result, $i), "\n"; echo ""; > ?> 

It’s saying that I haven’t selected a database? Although in mysql.php it clearly has the select_db command? I haven’t got any spelling errors either.. hmm!

OK it’s now saying ENTRIES doesn’t exist however I thought that the first query in tables.php tells it to create a table called «ENTRIES» if it didn’t exist?

@AndrewMatthew I tried your table.php with my own connection parameters and it creates table «ENTRIES». I have added code to list databases in your server and tables in selected database

Check the the table was not created by an earlier attempt. Table names are case sensitive in most Unixes, which you seem to be using.

Nothing is appearing in the error log which is frustrating and not helping me diagnose the problem

So your first poblem is to find out why it’s not logging any errors. BTW it would also be a good idea to inject some echo / print statements to find out where it’s failing.

Forget about the MySQL stuff and try:

Even though (once you’ve got the error reporting sorted out) you should get an error logged it will likely only contain a limited amount of information. Any time you call a mysql function, be via mysql_, mysqli or PDO, you should poll the return value and handle any error — even if it’s just to echo the value to the screen.

It’s saying that I haven’t selected a database

Possibly the user account you are connecting as does not have permission to access the database.

Источник

Why does this not create a table in mysql

As stated, use CREATE (if you wish to create), and then use what I also said in my comment above in regards to the quotes.

3 Answers 3

The key thing is to use CREATE. This code should do the trick:

If you want to CREATE a table, then both the ALTER and the ADD command are nonsense, try

CREATE TABLE $table (TITLE TEXT NOT NULL); 

This feature does not seems currently implemented in MySQL (at least 5.2): bugs.mysql.com/bug.php?id=3706 . You should go trough the hard way of reading the columns from the INFORMATION_SCHEMA. see here: cryer.co.uk/brian/mysql/howto_add_column_unless_exists.htm

Use the following if you wish to «create» a table if it does not exist, and alter it if it does exist.

Edit

The following has been tested and working on my server (using the mysqli_* function):

This question is in a collective: a subcommunity defined by tags with relevant content and experts.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.25.43544

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

PHP Table Not Created

I am using php to query a sql server database and write the results onscreen. I thought all my echo statements with table & tr/td were creating a table to display the results in, however when the data is shown on screen it appears run together like the below:

Employee EmployeeIDPrice1Price2Price3Price4Price5 

etc with no table structure. What should I change in this syntax so that an actual table is created to display results in?

if ($result = mssql_execute($proc)) < $number_of_rows = mssql_num_rows($result); if($number_of_rows >0) < echo ''; echo ''; echo '  '; echo '  '; echo '  '; echo '  '; echo '  '; echo '  '; echo '  '; echo '  '; echo ' '; echo ' '; echo ' '; echo ' '; echo ' '; echo ''; while ($Row = mssql_fetch_assoc($result)) < echo ''; > echo '
EmployeeEmployeeIDPrice1Price2Price3Price4Price5Price6Price7Price8Price9Price10Price11
'.$Row['Employee'] . ''.$Row['EmployeeID'] . ''."$".round($Row['Price1']) . ''."$".round($Row['Price2']) . ''."$".round($Row['Price3']) . ''."$".round($Row['Price4']) . ''."$".round($Row['Price5']) . ''."$".round($Row['Price6']) . ''."$".round($Row['Price7']) . ''."$".round($Row['Price8']) . ''."$".round($Row['Price9']) . ''."$".round($Row['Price10']) . ''."$".round($Row['Price11']) . '
'; >

Picture

EDIT
This is what my output looks like, even after adding in a cellpadding=»5″ . not a clearly defined table like i expected when using the html words table and td tr — meaning all the data just bleeds together, instead of having a table set-up like I thought I defined.

Источник

MySQL table not creating when using php

Note: The mysql_* functions your are using are becoming deprecated and will be removed in future versions of PHP. You should not use them to write new code, use mysqli_* or PDO instead.

3 Answers 3

You are not executing the mysql query using mysql_query() . The query will get executed/runned only if you use this function. Also you have to enclose the query within quotes.

 $sql="CREATE TABLE `voting` ( `id` int(5) NOT NULL AUTO_INCREMENT, `username` varchar(45) NOT NULL, `fyrir` varchar(45) NOT NULL, `more` text NOT NULL, `vote` int(8) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) )"; mysql_query($sql); 

Still nothing using that. I’m using XAMPP to host the webserver. Am I doing it right by opening it through localhost/PATH to run it or is there some other fancy way to do it?

After adding the $sql variable, you have to call dbConnection inside the file to open the database connection and then you add: mysql_query($sql) . That means:

 $sql="CREATE TABLE `voting` ( `id` int(5) NOT NULL AUTO_INCREMENT, `username` varchar(45) NOT NULL, `fyrir` varchar(45) NOT NULL, `more` text NOT NULL, `vote` int(8) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) )"; /* call dbConnect to open db connectioin */ dbConnect(); /* Execute the query */ if(!mysql_query($sql)) die(mysql_error()); else echo 'table creation successful'; ?> 

But, why don’t you use OOP? It will make things easier.

Источник

Оцените статью