Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysqli_query not working in php despite correctly built query

Tags:

php

mysql

I am trying to get what should otherwise be a simple but of php to insert sample data into a table, but something just isn't having any of it.

Table Definition:

CREATE TABLE IF NOT EXISTS teams ( 
     token varchar(12) COLLATE utf8_unicode_ci NOT NULL, 
     tname varchar(48) COLLATE utf8_unicode_ci NOT NULL, 
     captain varchar(64) COLLATE utf8_unicode_ci NOT NULL, 
     email varchar(64) COLLATE utf8_unicode_ci NOT NULL, 
     phone varchar(14) COLLATE utf8_unicode_ci NOT NULL, 
          PRIMARY KEY (token), 
          UNIQUE KEY name (tname), 
          KEY id (token) 
     )  
     ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Code:

$con = mysqli_connect("localhost","username","password","database");

$tname = "Big Bang";
$cname = "Mike";
$cemail = "[email protected]";
$cphone = "123-456-7898";

$teamToken = strtoupper(bin2hex(mcrypt_create_iv(6, MCRYPT_DEV_URANDOM)));

$query = "INSERT INTO teams (token, tname, captain, email, phone) VALUES ('" . $teamToken . "', '" . $tname . "', '" . $cname . "', '" . $cemail . "', '" . $cphone . "')";

if (mysqli_query($con, $query))
{
    echo "Pass!";
}
else
{
    echo $query;
}

mysqli_close($con);

What's odd is the php echos the query, because the mysqli_query result is false, yet the echoed query, when copied and pasted right into phpMyAdmin's terminal, works fine.

I am at my qit's end.

like image 466
Michael De Luca Avatar asked Oct 14 '25 03:10

Michael De Luca


1 Answers

Your Code:

$con = mysqli_connect("localhost","username","password","database");

Edited code:

$con = mysqli_connect("localhost","root","","database");
if(!$con):
die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
endif;

Its worked fine on my localhost

like image 60
CodeLove Avatar answered Oct 19 '25 05:10

CodeLove