Jump to content

PHP & MYSQL


Recommended Posts

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?

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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: /');

?>

Link to comment
Share on other sites

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: /');



?>

Link to comment
Share on other sites

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();





?>

Link to comment
Share on other sites

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();




?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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`)
)';

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.