Page 1 of 1

New and hatin' it

Posted: Thu Aug 15, 2002 3:50 pm
by Rasta
I just started learning php/mysql last week, and w/ some background in Flash AS, have been able to pick it up, albeit slowly. I'm having trouble writing to the database that I've got hosted on an outside server. I've tried everything I can, and have tried running the most simple scripts, but w/ no success. I'm wondering if there's a problem w/ my permissions, and wanted to know if there was a function out there that lets you see what a certain user's permission is?

Otherwise, can you tell me if it's something that's wrong w/ my script?

<?


$dbhost = "xxxxxxxx";
$dbuser = "xxxxxxxx";
$dbpass = "xxxxxxxx";

$db = mysql_connect( $dbhost, $dbuser, $dbpass);


if(!$db)
{
echo "<p>UNsuccessful connection to database</p>";
}else {
echo "<p> Connection Successful.</p>";
}


// Build table creation query
$query = "CREATE TABLE test(
pageid INTEGER AUTO_INCREMENT PRIMARY KEY,
pagetitle VARCHAR(100)
)";

// Attempt to create table. If successful...
if (@mysql_query($query)) {

// Inform user of success
print "Table created successfully";
} else {

// Otherwise, tell user there was an error
print "Error creating table";
}

// Close the connection
mysql_close($db);

?>


The error I get back says:

Connection Successful.

Error creating table



Thanks for any help

Posted: Thu Aug 15, 2002 3:55 pm
by twigletmac

Code: Select all

<?php

$dbhost = "xxxxxxxx"; 
$dbuser = "xxxxxxxx"; 
$dbpass = "xxxxxxxx"; 

$db = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error()); 

mysql_select_db('database_name') or die(mysql_error());

// Build table creation query 
$query = "CREATE TABLE test(pageid INTEGER AUTO_INCREMENT PRIMARY KEY, pagetitle VARCHAR(100))"; 

// Attempt to create table. If successful... 
mysql_query($query) or die(mysql_error().'<p>'.$query.'</pre>');

// Close the connection 
mysql_close($db) or die(mysql_error()); 

?>
Try this code I've just shortened it a bit and put in some calls to the mysql_error() function to give better error reporting. I think that the problem was that you didn't have a database selected, that's what the mysql_select_db() line is.

Mac

Posted: Thu Aug 15, 2002 5:29 pm
by Rasta
Thanks twigletmac,

That was totally it. I hadn't selected the db. Also, the error(); was totally helpful, as I found I'd put in $pageid, $pagetitle inside the sql script. I didn't know you couldn't add the '$' inside the sql statement.

Thanks again.