Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

PHP 

Error creating a table in mysql through php

I'm having trouble creating a table through php in mysql.
FYI: The database connects successfully.

This is the code that is to supposed to send the query
Code
<?php
    $con = mysqli_connect("localhost", "username", "password", "database");

    if(!$con)
    {
        echo "failed to connect to the database.";
    }
    else
    {
    echo '<script>alert("success, connected to database");</script>';
    }

    $stmt = "CREATE TABLE database.table(name TEXT, number INT)";
        if(!mysqli_query($stmt))
        {
                echo "could not create the tutors table " . mysqli_error();
        }
?>
The database does exist and the username and password do work.
What is the issue?
Well, what’s the error displayed?
Mysqli_query requires 2 parameters, you only provided one.
You see, Doctor, God didn't kill that little girl. Fate didn't butcher her and destiny didn't feed her to those dogs.
If God saw what any of us did that night he didn't seem to mind. From then on I knew... God doesn't make the world this way.
We do.
I think you have to do something like:

Code
$conn->query($query);

Since you created the connection in variable $conn, and that variables is unused.
You can solve that with 2 options:
  • You can use object-oriented style doing this:
    PHP Code

    <?php 
    $con
    ->query($stmt); 
    But remember that you have to create the connection with the object MySQLi:
    PHP Code

    <?php 
    $con 
    = new mysqli("localhost","user","pass","db_name"); 


  • The second option is to add in this string:
    PHP Code

    <?php 
    if(!mysqli_query($stmt)) 
    That:
    PHP Code

    <?php 
    if(!mysqli_query($con,$stmt)) 


Your ask problem about creating table, but you provided connection string, and you put FYI as connection was successful without problems. So, which one is the script problem?
When using MySQLi procedurally instead of the object-orientated, you need to specify the connection as the first argument. (https://www.php.net/manual/en/mysqli.query.php)

Or you can look into switching to object orientated as xMAAT has described which uses classes/method vs functions.
Thanks a lot, the issue was that I was missing a parameter from the query call, but I only figured it out through your guys' help.

My new problem is trying to understand the issue with inserting into a table in this database.

This is the code I have:
Code
<?php
   $con = new mysqli("localhost", "username", "password", "database");
   if($con->connect_error)
   {
       echo "failed to connect to the database.";
   }
   else
   {
   echo '<script>alert("success, connected to database");</script>';
   }
       $stmt = "insert into test (name text, num text) values ('name', 'last')";
        if($con->query($stmt) !== true)
       {
        echo $con->error;
       }
?>

The error I receive is this:
Code
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'text, num text) values ('name', 'last')' at line 1


What is the issue.
(Apr 3, 2019 03:50 PM)Jack Drake Wrote:  Thanks a lot, the issue was that I was missing a parameter from the query call, but I only figured it out through your guys' help.

My new problem is trying to understand the issue with inserting into a table in this database.

This is the code I have:
Code
<?php
   $con = new mysqli("localhost", "username", "password", "database");
   if($con->connect_error)
   {
       echo "failed to connect to the database.";
   }
   else
   {
   echo '<script>alert("success, connected to database");</script>';
   }
       $stmt = "insert into test (name text, num text) values ('name', 'last')";
        if($con->query($stmt) !== true)
       {
        echo $con->error;
       }
?>

The error I receive is this:
Code
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'text, num text) values ('name', 'last')' at line 1


What is the issue.
Specify the column names only, not their character types.

Code
$stmt = "insert into test (name, num) values ('name', 'last')";

If you're inserting things dynamically, or through user input, start using prepared statements.

Je t'aime
[Image: fpZ5a5g.png]

(Apr 22, 2022 04:39 PM)-Scrub Wrote:  Jokes on you, I'm not smart enough to know what he's talking about.
Is it true that preparing the statement actually decreasing the chance of SQL Injection?
(Apr 3, 2019 04:04 PM)Jack Drake Wrote:  Is it true that preparing the statement actually decreasing the chance of SQL Injection?
Do you think that's just nazi-propaganda or something? Yes, using prepared/parameter statements with proper validation will absolutely decrease the chance of SQL injection.

Je t'aime
[Image: fpZ5a5g.png]

(Apr 22, 2022 04:39 PM)-Scrub Wrote:  Jokes on you, I'm not smart enough to know what he's talking about.
(Apr 3, 2019 04:46 PM)Nachfolger Wrote:  Do you think that's just nazi-propaganda or something? Yes, using prepared/parameter statements with proper validation will absolutely decrease the chance of SQL injection.

This made me laugh o//
@OP, please start learning PHP with more simple things. 2 weeks ago you tried to use the Selly API, yet you can't even make a proper MySQL connection, and doesn't even understand variable scopes. Try to learn 1 by 1, not all at once.

[Image: B37mjKj.png]


Possibly Related Threads...
Thread / Author Replies Views Last Post
by .eXeAug 28, 2023 05:07 AM 1 201 Aug 28, 2023 05:07 AM
Last Post: Nachfolger
[PHP] by AlexFeb 10, 2020 03:51 PM 2 169 Feb 10, 2020 03:51 PM
Last Post: Alex
[PHP] by Jack DrakeAug 30, 2019 08:12 PM 20 493 Aug 30, 2019 08:12 PM
Last Post: sdk