0
$dbHeader = mysqli_connect("host", "username", "password", "db"); 

I use this to connect to database and it works. I'd tried this

$table = "users";
$query = "
    CREATE TABLE IF NOT EXISTS $table(
      id int(0) AUTO_INCREMENT,
      userId bigint(0),
      username varchar(200),
      firstName varchar(200),
      lastName varchar(200),
      PRIMARY KEY (id)
    )
";
$qu = mysqli_query($dbHeader, $query);

and it doesn't work so I'd tried with directly the name of the table and it works. i just tried with other queries and various methods like

$stmt = mysqli_prepare($dbHeader, "INSERT INTO users (userId, username, firstName, lastName) values (?,?,?,?)");
mysqli_stmt_bind_param($stmt, $userId, $username, $firstName, $lastName);
mysqli_stmt_execute($stmt);

but it still doesn't work.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • Just a tentative, add a space after $table, before the ( ...? – Nic3500 Oct 21 '17 at 17:49
  • Note: The object-oriented interface to `mysqli` is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete `mysql_query` interface. Before you get too invested in the procedural style it’s worth switching over. Example: `$db = new mysqli(…)` and `$db->prepare("…")` The procedural interface is an artifact from the PHP 4 era when `mysqli` API was introduced and should not be used in new code. – tadman Oct 21 '17 at 19:00
  • Hint: Read up on how `bind_param` actually works. You're missing an argument. – tadman Oct 21 '17 at 19:00
  • It's also *extremely* odd that you're declaring values as `INT(0)`. You really should leave that as `INT`. Likewise, using `BIGINT` is probably over-kill for most applications. It's unlikely that you'll regsister in excess of *two billion* users on this code. Is there any reason why `id` and `userId` are different things? – tadman Oct 21 '17 at 19:01

0 Answers0