Can't create table in oop mysqli script

I can create a table in phpMyAdmin but not within oop mysqli script. Can someone pls straighten me out?
[php] include_once ($_SERVER[‘DOCUMENT_ROOT’] . ‘/cgi-bin/cnnct2mysqli.php’);
$mysqli = new mysqli($DBServer, $DBUser, $DBPass, $DBName);

if ($mysqli->connect_errno) {
echo “Failed to connect to MySQL: (” . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

$id	= '';
$hits	= 223;  

If(!$mysqli->query("CREATE TABLE tstTB (
id int(6) unsigned NOT NULL auto_increment,//i
hits int(6) unsigned NOT NULL default ‘0’, //d
PRIMARY KEY (id),
")) echo “$mysqli not an object.”;

$stmnt = $mysqli->prepare(“INSERT INTO tstTB(id, hits) VALUES (?, ?)”)

$stmnt->bind_param(‘id’, $id, $hits); //This Line: “syntax error, unexpected T_VARIABLE”

if (!$stmnt->execute()) {echo “Execute failed: (” . $stmt->errno . ") " . $stmnt->error;}

$stmt->close();[/php]

Help appreciated.
usit

You are specifying ‘id’ which means the 2 parameters are you passing in should be an integer and a decimal

$id = a space and it’s also an auto_increment field and you should remove that from the insert statement

And hits is a integer not a decimal

And the cause of your error is because you forgot the “;” on the end of the insert line.

[php]
$stmnt = $mysqli->prepare(“INSERT INTO tstTB(id, hits) VALUES (?, ?)”)
$stmnt->bind_param(‘id’, $id, $hits); //This Line: “syntax error, unexpected T_VARIABLE” [/php]

I would change the statements above to:

[php]$stmnt = $mysqli->prepare(“INSERT INTO tstTB(‘hits’) VALUES (?)”);
$stmnt->bind_param(‘i’, $hits); //This Line: “syntax error, unexpected T_VARIABLE” [/php]

Back in town, at it again, and stuck again! Here’s the corrected version:
[php]//Test for a Table to be scaled to a dozen or more columns//
include_once ($_SERVER[‘DOCUMENT_ROOT’] . ‘/cgi-bin/cnnct2mysqli.php’);
$mysqli = new mysqli($DBServer, $DBUser, $DBPass, $DBName);

if ($mysqli->connect_errno) {echo “MySQL Connection Failed: (”.$mysqli->connect_errno.")".$mysqli->connect_error;}

$hits		= 1000*RAND();  

If(!$mysqli->query(“CREATE TABLE tstTB (
id SMALLINT(6) unsigned NOT NULL auto_increment,
hits VARCHAR(6) unsigned NOT NULL default ‘0’,
PRIMARY KEY (id)”)) ;

$stmnt = $mysqli->prepare(“INSERT INTO tstTB(‘hits’’) VALUES (?)”);

$stmnt->bind_param(‘i’, $hits);
//<<Line above. THROWS: 'Call to a member function bind_param() on a non-object ’

if (!$stmnt->execute()) {echo “Execute failed: (” . $stmt->errno . ") " . $stmnt->error;}

$stmnt->close();
?> [/php]

Suggestions greatly appreciated
usit

You have a double ’

This:

[php]$stmnt = $mysqli->prepare(“INSERT INTO tstTB(‘hits’’) VALUES (?)”);[/php]

Should Be:
[php]$stmnt = $mysqli->prepare(“INSERT INTO tstTB(‘hits’) VALUES (?)”);[/php]

Thanks for the sharp eye Topcoder.
But, correcting ‘’ to ’ didn’t chang the error thrown.
usit

PS:
I commented out the prepare, bind, execute, and close statements to see if there is a problem creating the table tstTB, which would cause an error. No error was thrown, but no table was created either.

The If statement you’re wrapping the Create statement in does nothing

[php] If(!$mysqli->query(“CREATE TABLE tstTB (
id SMALLINT(6) unsigned NOT NULL auto_increment,
hits VARCHAR(6) unsigned NOT NULL default ‘0’,
PRIMARY KEY (id)”)) ;[/php]

Code something like this
[php]
if ($mysqli->query(“CREATE TABLE tstTB (
id SMALLINT(6) unsigned NOT NULL auto_increment,
hits VARCHAR(6) unsigned NOT NULL default ‘0’,
PRIMARY KEY (id)”) === TRUE) {
printf(“Table successfully created.\n”);
}[/php]

See if it says the table was created.

Sponsor our Newsletter | Privacy Policy | Terms of Service