Table mysqli

Codes here !

Moderators: egami, macek, gesf

Post Reply
maxone981
New php-forum User
New php-forum User
Posts: 1
Joined: Tue Feb 16, 2021 4:52 am

Tue Feb 16, 2021 4:55 am

Hi everyone,
I've a problem creating my table, can someone help me please?

$mydb = "dbprova";
$con = mysqli_connect("localhost", "root", "",);
$db = mysqli_select_db($con, $mydb);

if (!$db) {
echo "Database not exists";
mysqli_query($con, "CREATE DATABASE dbprova");
$newcon= mysqli_connect("localhost", "root", "", $mydb);
mysqli_query($newcon,"CREATE TABLE Employees
(employee_id INT, first_name VARCHAR(50), last_name VARCHAR(50))");
}else {
echo "Database found";
}
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 1190
Joined: Mon Feb 22, 2016 5:52 pm

Tue Feb 16, 2021 10:46 am

Use PDO:

Something like this (not tested)-

Code: Select all

<?php
$servername = "localhost";
$username = "username";
$password = "password";

try {
  $conn = new PDO("mysql:host=$servername", $username, $password);
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $sql = "CREATE DATABASE IF NOT EXISTS dbprova";
  $conn->exec($sql);
  echo "Yay, database created";
} catch(PDOException $e) {
  echo "Doh! " . $e->getMessage();
}
Then have a look here for how to insert data etc. if you are looking at anything else, forget about it and use PDO, wherever you got that code is out of date and touch.
Post Reply