MySQLi Question

I am attempting to create a table in sql using a php script but I am receiving this message.

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Createtable\index.php on line 7

Warning: mysqli_error() expects parameter 1 to be mysqli, null given in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Createtable\index.php on line 9

What do I need to do with this?

Thanks

It would appear you are not providing a 1st parameter for mysqli_query, or an incorrect parameter.

The correct way to do it:


$db = mysqli_connect('host','username','password','database_name');
$result = mysqli_query($db, 'SELECT something FROM some_table');

learning as I go (using Build your own database driven web site using php & mysql) so not solid on what a 1st parameter is - and - the database is not created yet so I cannot select from it.

This is the script I used:

<?php
$sql = ‘CREATE TABLE joke(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
joketext TEXT,
jokedate DATE NOT NULL
) DEFAULT CHARACTER SET utf8’;
if (!mysqli_query($link, $sql))
{
$output = 'Error creating joke table: ’ . mysqli_error($link);
include ‘output.html.php’;
exit();
}

$output = ‘Joke table successfuly created.’;
include ‘output.html.php’;
?>

And - this is the error received:

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Createtable\index.php on line 7

Warning: mysqli_error() expects parameter 1 to be mysqli, null given in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Createtable\index.php on line 9

Error creating msp table:

End of Error message

$link you use in your script is not yet defined. You should do something like:


$link = mysqli_connect('host','username','password','database_name');
$sql = 'CREATE TABLE joke(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
joketext TEXT,
jokedate DATE NOT NULL
) DEFAULT CHARACTER SET utf8';
if (!mysqli_query($link, $sql))
{
$output = 'Error creating joke table: ' . mysqli_error($link);
include 'output.html.php';
exit();
}

$output = 'Joke table successfuly created.';
include 'output.html.php';

(only added first line)

If you haven’t created the database that’s a problem. You can’t create a table without there being a database to put it in. Got phpMyAdmin?

this is my point…so what I need is the command or program to create a database in sql with php. I have phpmyadmin but I would like to know how to use php program to do this.

Thanks

http://dev.mysql.com/doc/refman/5.0/en/create-database.html