adhbvklwqdbviabjiawdnbij Posted July 27, 2011 Share Posted July 27, 2011 This code is not working. Why? <?php include('mysql.php'); $con = mysql_connect("$host", "$user", "$password"); $sql1 = 'CREATE DATABASE IF NOT EXISTS `' . $database . '`'; $sql2 = 'CREATE TABLE IF NOT EXISTS `battles` ( `battle_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `winner` bigint(20) unsigned NOT NULL, `loser` bigint(20) unsigned NOT NULL, )'; $sql3 = 'CREATE TABLE IF NOT EXISTS `images` ( `image_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `filename` varchar(255) NOT NULL, `score` int(10) unsigned NOT NULL DEFAULT '1500', `wins` int(10) unsigned NOT NULL DEFAULT '0', `losses` int(10) unsigned NOT NULL DEFAULT '0', )'; if ($handle = opendir('images')) { while (false !== ($file = readdir($handle))) { if($file!='.' && $file!='..') { $images[] = "('".$file."')"; } } closedir($handle); } $sql4 = "INSERT INTO images (filename) VALUES ".implode(',', $images)." "; mysql_query($sql1, $sql2, $sql3, $sql4, $con); mysql_close(); header('location: /'); ?> I would like to "clean" the tables if exist, how can i do this? Quote Link to comment https://forums.phpfreaks.com/topic/243030-php-mysql/ Share on other sites More sharing options...
DavidAM Posted July 27, 2011 Share Posted July 27, 2011 mysql_query($sql1, $sql2, $sql3, $sql4, $con); mysql_query accepts 2 parameters. The first is the SQl to be executed and the second is the connection. You cannot pass it 4 SQL statements like that. You will need to execute each one independently. mysql_query($sql1); mysql_query($sql2); mysql_query($sql3); mysql_query($sql4); Note: The connect parameter is optional, I personally never supply it; but that is up to you. Note: You should probably add some error checking in between there and print appropriate messages. Quote Link to comment https://forums.phpfreaks.com/topic/243030-php-mysql/#findComment-1248214 Share on other sites More sharing options...
adhbvklwqdbviabjiawdnbij Posted July 28, 2011 Author Share Posted July 28, 2011 The database is not being created. <?php include('mysql.php'); $con = mysql_connect("$host", "$user", "$password"); if (!$con) { die('Fail. - ' . mysql_error()); } $sql1 = 'CREATE DATABASE IF NOT EXISTS `' . $database . '`'; mysql_query($sql1, $con); $sql2 = 'CREATE TABLE IF NOT EXISTS `battles` ( `battle_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `winner` bigint(20) unsigned NOT NULL, `loser` bigint(20) unsigned NOT NULL, )'; mysql_query($sql2, $con); $sql3 = 'CREATE TABLE IF NOT EXISTS `images` ( `image_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `filename` varchar(255) NOT NULL, `score` int(10) unsigned NOT NULL DEFAULT '1500', `wins` int(10) unsigned NOT NULL DEFAULT '0', `losses` int(10) unsigned NOT NULL DEFAULT '0', )'; mysql_query($sql3, $con); if ($handle = opendir('images')) { while (false !== ($file = readdir($handle))) { if($file!='.' && $file!='..') { $images[] = "('".$file."')"; } } closedir($handle); } $sql4 = "INSERT INTO images (filename) VALUES ".implode(',', $images)." "; mysql_query($sql4, $con); mysql_close(); header('location: /'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/243030-php-mysql/#findComment-1248250 Share on other sites More sharing options...
premiso Posted July 28, 2011 Share Posted July 28, 2011 If you would display the errors I am sure you would see why. mysql_query($sql1, $con) or trigger_error('Error Creating Database: ' . mysql_error()); See what error is being thrown. Quote Link to comment https://forums.phpfreaks.com/topic/243030-php-mysql/#findComment-1248251 Share on other sites More sharing options...
adhbvklwqdbviabjiawdnbij Posted July 28, 2011 Author Share Posted July 28, 2011 I did. Nothing happened. Quote Link to comment https://forums.phpfreaks.com/topic/243030-php-mysql/#findComment-1248255 Share on other sites More sharing options...
PFMaBiSmAd Posted July 28, 2011 Share Posted July 28, 2011 What trigger_error shows is dependent on the error_reporting/display_errors settings. Add the following two lines of code immediately after your first opening <?php tag - ini_set("display_errors", "1"); error_reporting(-1); Quote Link to comment https://forums.phpfreaks.com/topic/243030-php-mysql/#findComment-1248256 Share on other sites More sharing options...
adhbvklwqdbviabjiawdnbij Posted July 28, 2011 Author Share Posted July 28, 2011 Again, nothing happened... <?php ini_set("display_errors", "1"); error_reporting(-1); include('mysql.php'); $con = mysql_connect("$host", "$user", "$password"); if (!$con) { die('Falha Na Conexão: ' . mysql_error()); } $sql1 = 'CREATE DATABASE IF NOT EXISTS `' . $database . '`'; mysql_query($sql1, $con) or trigger_error('Error Creating Database: ' . mysql_error()); $sql2 = 'CREATE TABLE IF NOT EXISTS `battles` ( `battle_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `winner` bigint(20) unsigned NOT NULL, `loser` bigint(20) unsigned NOT NULL, )'; mysql_query($sql2, $con); $sql3 = 'CREATE TABLE IF NOT EXISTS `images` ( `image_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `filename` varchar(255) NOT NULL, `score` int(10) unsigned NOT NULL DEFAULT '1500', `wins` int(10) unsigned NOT NULL DEFAULT '0', `losses` int(10) unsigned NOT NULL DEFAULT '0', )'; mysql_query($sql3, $con); if ($handle = opendir('images')) { while (false !== ($file = readdir($handle))) { if($file!='.' && $file!='..') { $images[] = "('".$file."')"; } } closedir($handle); } $sql4 = "INSERT INTO images (filename) VALUES ".implode(',', $images)." "; mysql_query($sql4, $con); mysql_close(); header('location: /'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/243030-php-mysql/#findComment-1248257 Share on other sites More sharing options...
PFMaBiSmAd Posted July 28, 2011 Share Posted July 28, 2011 Comment out the header() redirect near the end of the code. Quote Link to comment https://forums.phpfreaks.com/topic/243030-php-mysql/#findComment-1248258 Share on other sites More sharing options...
adhbvklwqdbviabjiawdnbij Posted July 28, 2011 Author Share Posted July 28, 2011 Nothing... =// I commented out all the rest, let only this. Didnt work. <?php ini_set("display_errors", "1"); error_reporting(-1); include('mysql.php'); $con = mysql_connect("$host", "$user", "$password"); if (!$con) { die('Falha Na Conexão: ' . mysql_error()); } $sql1 = 'CREATE DATABASE IF NOT EXISTS `' . $database . '`'; mysql_query($sql1, $con) or trigger_error('Error Creating Database: ' . mysql_error()); mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/243030-php-mysql/#findComment-1248259 Share on other sites More sharing options...
PFMaBiSmAd Posted July 28, 2011 Share Posted July 28, 2011 What URL are you putting into your browser to request the file and what exactly do you get as output in your browser? If you get a blank page, what does a 'view source' in your browser show? Quote Link to comment https://forums.phpfreaks.com/topic/243030-php-mysql/#findComment-1248262 Share on other sites More sharing options...
adhbvklwqdbviabjiawdnbij Posted July 28, 2011 Author Share Posted July 28, 2011 http://localhost/.../install.php Yes, show up a blank page. And in "view source" theres nothing. install.php <?php ini_set("display_errors", "1"); error_reporting(-1); include('mysql.php'); $con = mysql_connect("$host", "$user", "$password"); if (!$con) { die('Falha Na Conexão: ' . mysql_error()); } $sql1 = 'CREATE DATABASE IF NOT EXISTS `' . $database . '`'; mysql_query($sql1, $con) or trigger_error('Error Creating Database: ' . mysql_error()); $sql2 = 'CREATE TABLE IF NOT EXISTS `battles` ( `battle_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `winner` bigint(20) unsigned NOT NULL, `loser` bigint(20) unsigned NOT NULL, )'; mysql_query($sql2, $con); $sql3 = 'CREATE TABLE IF NOT EXISTS `images` ( `image_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `filename` varchar(255) NOT NULL, `score` int(10) unsigned NOT NULL DEFAULT '1500', `wins` int(10) unsigned NOT NULL DEFAULT '0', `losses` int(10) unsigned NOT NULL DEFAULT '0', )'; mysql_query($sql3, $con); if ($handle = opendir('images')) { while (false !== ($file = readdir($handle))) { if($file!='.' && $file!='..') { $images[] = "('".$file."')"; } } closedir($handle); } $sql4 = "INSERT INTO images (filename) VALUES ".implode(',', $images)." "; mysql_query($sql4, $con); mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/243030-php-mysql/#findComment-1248266 Share on other sites More sharing options...
PFMaBiSmAd Posted July 28, 2011 Share Posted July 28, 2011 Add the following, right after the $sql1 = '.....' statement to see if your code is even running - echo $sql1; If you don't get anything echoed, it is likely that your mysql.php code is dieing. Do you have the mysql extension installed in your php installation? Have you successfully executed any mysql_ statements in a php scirpt? Quote Link to comment https://forums.phpfreaks.com/topic/243030-php-mysql/#findComment-1248270 Share on other sites More sharing options...
adhbvklwqdbviabjiawdnbij Posted July 28, 2011 Author Share Posted July 28, 2011 CREATE DATABASE IF NOT EXISTS `my_db` Wait. I wll edit it. Quote Link to comment https://forums.phpfreaks.com/topic/243030-php-mysql/#findComment-1248274 Share on other sites More sharing options...
PFMaBiSmAd Posted July 28, 2011 Share Posted July 28, 2011 You need to use the or trigger_error('Error Creating Database: ' . mysql_error()); debugging logic on all your queries. You also need to select the database you just created before you can create any tables in it or you need to specify the database name in the queries. Quote Link to comment https://forums.phpfreaks.com/topic/243030-php-mysql/#findComment-1248276 Share on other sites More sharing options...
adhbvklwqdbviabjiawdnbij Posted July 28, 2011 Author Share Posted July 28, 2011 I did it. <?php ini_set("display_errors", "1"); error_reporting(-1); include('mysql.php'); $con = mysql_connect("$host", "$user", "$password"); if (!$con) { die('Fail: ' . mysql_error()); } $sql1 = 'CREATE DATABASE IF NOT EXISTS `' . $database . '`'; mysql_query($sql1, $con) or trigger_error('Fail Connecting To Database: ' . mysql_error()); mysql_select_db("$database") or trigger_error('Fail Selecting Database: ' . mysql_error()); $sql2 = 'CREATE TABLE IF NOT EXISTS `battles` ( `battle_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `winner` bigint(20) unsigned NOT NULL, `loser` bigint(20) unsigned NOT NULL, )'; mysql_query($sql2, $con) or trigger_error('Error Creating Table battles: ' . mysql_error()); $sql3 = 'CREATE TABLE IF NOT EXISTS `images` ( `image_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `filename` varchar(255) NOT NULL, `score` int(10) unsigned NOT NULL DEFAULT '1500', `wins` int(10) unsigned NOT NULL DEFAULT '0', `losses` int(10) unsigned NOT NULL DEFAULT '0', )'; mysql_query($sql3, $con) or trigger_error('Error Creating Table images: ' . mysql_error()); mysql_close(); ?> But it's not working. Quote Link to comment https://forums.phpfreaks.com/topic/243030-php-mysql/#findComment-1248287 Share on other sites More sharing options...
Pikachu2000 Posted July 28, 2011 Share Posted July 28, 2011 You have fatal parse errors in the $sql3 query string, and those won't be reported unless error reporting is on in your php.ini file. You can't use the same type of quote within a string as the quotes that enclose the string without escaping them with a backslash. In this case, since the values are integers, you can drop the quotes entirely. You also need to define the auto_increment field as an index. $sql3 = 'CREATE TABLE IF NOT EXISTS `images` ( `image_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `filename` varchar(255) NOT NULL, `score` int(10) unsigned NOT NULL DEFAULT 1500, `wins` int(10) unsigned NOT NULL DEFAULT 0, `losses` int(10) unsigned NOT NULL DEFAULT 0, primary key (`image_id`) )'; Quote Link to comment https://forums.phpfreaks.com/topic/243030-php-mysql/#findComment-1248312 Share on other sites More sharing options...
WebStyles Posted July 28, 2011 Share Posted July 28, 2011 sounds like a user permissions thing... Are you sure the user has enough privileges to create databases? also, you're trying to create a databse based on the value of $database, but I don't see that variable defined anywhere. (I'm assuming it's in mysql.php ??) anyway, just to debug, change: $sql1 = 'CREATE DATABASE IF NOT EXISTS `' . $database . '`'; mysql_query($sql1, $con) or trigger_error('Fail Connecting To Database: ' . mysql_error()); to something like: $database = 'phpFreaksTestDB'; $sql1 = 'CREATE DATABASE ' . $database; if(mysql_query($sql1, $con)){ echo 'database created'; }else{ echo 'failed '.mysql_error(); } and comment out everything that comes after that. This will at least tell you if the user has permissions. Quote Link to comment https://forums.phpfreaks.com/topic/243030-php-mysql/#findComment-1248408 Share on other sites More sharing options...
adhbvklwqdbviabjiawdnbij Posted July 28, 2011 Author Share Posted July 28, 2011 Now everything in this part is ok. Thanks. <?php ini_set("display_errors", "1"); error_reporting(-1); include('mysql.php'); $con = mysql_connect("$host", "$user", "$password"); if (!$con) { die('Fail: ' . mysql_error()); } $sql1 = 'CREATE DATABASE IF NOT EXISTS ' . $database; mysql_query($sql1, $con) or trigger_error('Fail Creating Database: ' . mysql_error()); mysql_select_db("$database") or trigger_error('Fail Selecting Database: ' . mysql_error()); $sql2 = 'CREATE TABLE IF NOT EXISTS `battles` ( `battle_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `winner` bigint(20) unsigned NOT NULL DEFAULT 0, `loser` bigint(20) unsigned NOT NULL DEFAULT 0, primary key (`battle_id`) )'; mysql_query($sql2, $con) or trigger_error('Error Creating Table battles: ' . mysql_error()); $sql3 = 'CREATE TABLE IF NOT EXISTS `images` ( `image_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `filename` varchar(255) NOT NULL, `score` int(10) unsigned NOT NULL DEFAULT 1500, `wins` int(10) unsigned NOT NULL DEFAULT 0, `losses` int(10) unsigned NOT NULL DEFAULT 0, primary key (`image_id`) )'; mysql_query($sql3, $con) or trigger_error('Error Creating Table images: ' . mysql_error()); if ($handle = opendir('images')) { while (false !== ($file = readdir($handle))) { if($file!='.' && $file!='..') { $images[] = "('".$file."')"; } } closedir($handle); } $sql4 = "INSERT INTO images (filename) VALUES ".implode(',', $images)." "; mysql_query($sql4, $con) or trigger_error('Error Inserting The Images: ' . mysql_error()); mysql_close(); ?> But now i'm having problem in index.php. =/ Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /var/www/.../index.php on line 26 Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /var/www/.../index.php on line 33 Just another question. I would like also if that tables exist, to replace it. Like "cleaning" the tables. Quote Link to comment https://forums.phpfreaks.com/topic/243030-php-mysql/#findComment-1248530 Share on other sites More sharing options...
WebStyles Posted July 28, 2011 Share Posted July 28, 2011 so... it works now, but what was the problem, wanna share? (man your username really bugs me... how do you even remember that when you need to login?) Quote Link to comment https://forums.phpfreaks.com/topic/243030-php-mysql/#findComment-1248532 Share on other sites More sharing options...
adhbvklwqdbviabjiawdnbij Posted July 28, 2011 Author Share Posted July 28, 2011 But now i'm having problem in index.php. =/ Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /var/www/.../index.php on line 26 Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /var/www/.../index.php on line 33 Just another question. I would like also if that tables exist, to replace it. Like "cleaning" the tables. Quote Link to comment https://forums.phpfreaks.com/topic/243030-php-mysql/#findComment-1248547 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.