Hack Forums
[PHP] Error creating a table in mysql through php - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Web Development (https://hackforums.net/forumdisplay.php?fid=183)
+---- Thread: [PHP] Error creating a table in mysql through php (/showthread.php?tid=5956251)



Error creating a table in mysql through php - Jack Drake - Apr 2, 2019

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?


RE: Error creating a table in mysql through php - Charger - Apr 2, 2019

Well, what’s the error displayed?


RE: Error creating a table in mysql through php - Squito - Apr 3, 2019

Mysqli_query requires 2 parameters, you only provided one.


RE: Error creating a table in mysql through php - Mohinder - Apr 3, 2019

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.


RE: Error creating a table in mysql through php - xMAAT - Apr 3, 2019

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)) 





RE: Error creating a table in mysql through php - jascorp - Apr 3, 2019

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?


RE: Error creating a table in mysql through php - itzDecon - Apr 3, 2019

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.


RE: Error creating a table in mysql through php - Jack Drake - Apr 3, 2019

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.


RE: Error creating a table in mysql through php - Nachfolger - Apr 3, 2019

(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.


RE: Error creating a table in mysql through php - Jack Drake - Apr 3, 2019

Is it true that preparing the statement actually decreasing the chance of SQL Injection?


RE: Error creating a table in mysql through php - Nachfolger - Apr 3, 2019

(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.


RE: Error creating a table in mysql through php - Mohinder - Apr 5, 2019

(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//


RE: Error creating a table in mysql through php - xHeaven - Apr 5, 2019

@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.