Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Noob question. Can't create table but no error number.

    I've done a bit of playing around with MySQL databases and PHP, but still fairly new to it. I've set up my database on my host and am using phpMyAdmin to check it.

    I'm trying to develop a program which requires some PHP ... right now I'm just running the PHP script directly by uploading it on the domain and running it from a browser. It's very simple, I'm just following tutorials right now. It wouldn't create a new database from PHP, but I skipped that part because I can create the database I need from my control panel (and would never need to create it again, so that's fine).

    It's connecting to the database correctly, but when I try to create a table, I just get an error but no error number. The PHP code for this is .... (this isn't the entire code but I don't want to show my database details above, but it is connecting fine).

    // Connected at this point... now trying to create sample table...

    $sql = "CREATE TABLE Persons
    (
    PID INT NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(PID),
    FirstName CHAR(15),
    LastName CHAR(15),
    Age INT
    )";

    // Execute query
    if (mysqli_query($con,$sql)) {
    echo "Table persons created successfully";
    } else {
    echo "Error creating table: " . mysqli_error($con);
    }

    When I run this, I just get the error message, i.e 'Error creating table : ', but then nothing else, i.e no error message. $con is set up correctly as it does connect through that.

    I'm guessing that the PHP code is fine since it's straight from the official tutorials but how do I work out what the error/problem is? Is there some reason that I can't create a table this way? I know I can create my tables directly in PHPMyAdmin but I want to create them from my project.

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,122
    Thanks
    2
    Thanked 327 Times in 319 Posts
    You need to have php's error_reporting set to E_ALL and display_errors set to ON to get php to help you.

    About the only way that the posted code wouldn't display any output from the mysqli_error statement is if $con isn't a valid database connection (you would be getting a php error about $conn, instead of the mysqli_error output.)
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah, that's what I thought. It certainly seems to be running through to that point fine with no other errors reported.

    I've turned on error reporting with...

    error_reporting(E_ALL);

    Still not seeing an error. I can't find any documentation on the display_errors command, how do I use it? I tried display_errors(on); but no joy with that.

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,122
    Thanks
    2
    Thanked 327 Times in 319 Posts
    Code:
    ini_set("display_errors", "1");
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #5
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, I got a couple of warnings....

    Warning: mysqli_query() expects parameter 1 to be mysqli, resource given in ...opendb.php on line 33

    Warning: mysqli_error() expects parameter 1 to be mysqli, resource given in...opendb.php on line 36

    Those lines being...

    if (mysqli_query($con,$sql)) { < line 33
    echo "Table persons created successfully";
    } else {
    echo "Error creating table: " . mysqli_error($con); < line 36
    }

    Is there something wrong with those? I just copied them from a tutorial.

  • #6
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,122
    Thanks
    2
    Thanked 327 Times in 319 Posts
    $con isn't an instance of a mysqli_connection. Best guess it that it's a mysql_ (no i) database connection. What is your code that's creating the $con (less any database credentials)?
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #7
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here's the full code. The 2nd line 'header.. etc' is something specific to a programming language that allows me to test from the language without having to upload the entire program to the server. Note that I'm not testing from the language right now anyway, I'm testing from a webpage. This code is uploaded onto the same domain as the database and run from my browser.

    This is the full code. I'm not actually sure about the 'localhost' at all, but people said to just leave it as localhost. The login details are correct because it gives other errors when I type(d) them incorrectly, obviously I've just 'xxx''d them for this.

    <?php
    header('Access-Control-Allow-Origin: *');

    error_reporting(E_ALL);
    ini_set("display_errors", "1");

    $host="localhost"; // Host name
    $username="xxx"; // Mysql username
    $password="xxx"; // Mysql password
    $db_name="xxx"; // Database name
    //$tbl_name="scores"; // Table name

    // Connect to server and select database.
    $con=mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");

    if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    // Create table

    $sql = "CREATE TABLE Persons
    (
    PID INT NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(PID),
    FirstName CHAR(15),
    LastName CHAR(15),
    Age INT
    )";

    // Execute query
    if (mysqli_query($con,$sql)) {
    echo "Table persons created successfully";
    } else {
    echo "Error creating table: " . mysqli_error($con);
    }

    // close MySQL connection
    mysql_close();
    ?>

  • #8
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,122
    Thanks
    2
    Thanked 327 Times in 319 Posts
    You cannot mix mysql_ (no i) and mysqli_ functions. Use all mysqli_ functions.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #9
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you, got there in the end. It did give an error but I think that's because you need to select the database name in the connect function now, rather than seperately on the next line? Well, I put it into the connect line directly and it's working fine now.

    Kinda glad it happened this way because the full-errors thing keeps popping up warnings, which will help me to code better while I'm learning

    Also, I need to ditch the tutorials I was reading and stick to mysqli tutorials.

    Thanks for your time


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •