PDA

View Full Version : PHP scripts using CL



heroine
08-16-2007, 09:31 AM
Hi All,

I have this scenario, hopefully it can be possible to implement it in php scripting using command line.

I have a datatbase (MySQL) to connect.

By using command prompt:
- So i would like to have a php script to connect to my mysql, and it says it's connected successfully...(i have this code done) additionally, I want this script to allow the end user to enter his/her own database name...be able to create a name which will automatically be generated into mysql. Another script to drop the database ?????? and also give options to user....do you really want to delete or drop ...????
Can both php (connect & create name) be in the same script? how?

Once that's done, the user must be able also to connect through command prompt to a URL, in order to retrieve data and store them into mysql.

Can I find some sample working codes to illustrate all?

Hope to get your comments soooooonnnnn...
thanks a lot for reading....

Chris
08-16-2007, 12:14 PM
PHP can natively interface with MySQL databases.

http://us3.php.net/manual/en/ref.mysql.php

If you feel you really must work with the shell you may need to set special permissions with your php scripts to allow them to run as a superuser (risky) or you'll need to use suexec or something in the script.

http://us3.php.net/manual/en/function.exec.php

But really, it can all be done with those native PHP functions.

heroine
08-18-2007, 05:28 AM
thanks for the reply....

I am running this current script but it can only create the first table in the database...I don't know why.
Am I missing something here?

PHP code

<?php
$link = mysql_connect('localhost', 'username', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}

$sql = 'CREATE DATABASE test2';
if (mysql_query($sql, $link)) {
echo "Database test2 created successfully\n";
} else {
echo 'Error creating database: ' . mysql_error() . "\n";
}
// Create tables in test2 database

mysql_select_db("test2", $link);

$sql = "CREATE TABLE car (
title varchar(255) NOT NULL,
description varchar(255) NOT NULL,
priority tinyint(2) NOT NULL default '0'
) TYPE=InnoDB\n";

mysql_query($sql, $link);
if (mysql_error()) echo __line__.") Error creating table: ".mysql_error();

//Create links table
$sql = "CREATE TABLE links (
type varchar(20) NOT NULL default '',
resource varchar(255) NOT NULL,
) TYPE=InnoDB\n";

mysql_query($sql, $link);
if (mysql_error()) echo __line__.") Error creating table: ".mysql_error();

//Create home table
$sql = "CREATE TABLE home (
name varchar(255) NOT NULL,
title varchar(255) NOT NULL default '',
description varchar(255) NOT NULL default '',
) TYPE=InnoDB\n";

mysql_query($sql, $link);
if (mysql_error()) echo __line__.") Error creating table: ".mysql_error();

//Create people table
$sql = "CREATE TABLE people (
name varchar(20) NOT NULL default '',
address varchar(255) NOT NULL
resource varchar(255) NOT NULL,

) TYPE=InnoDB\n";

echo "car,home,links, & people tables in the relational database (".test2.") have been successfully created!\n";
mysql_query($sql, $link);
if (mysql_error()) echo __line__.") Error creating table: ".mysql_error();
mysql_close($link);//Close connection
?>

rpanella
08-18-2007, 01:41 PM
The problem is for all the rest of the tables after the first, you have a comma (,) after the last field, before the closing ")".


//Create links table
$sql = "CREATE TABLE links (
type varchar(20) NOT NULL default '',
resource varchar(255) NOT NULL,
) TYPE=InnoDB\n";

You have to remove that last extra comma (the red one) from each of them and it should work.
________
328 (http://www.bmw-tech.org/wiki/BMW_328)