Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having a problem getting mysqli_query to execute [duplicate]

Tags:

Here's the problem: I started a swap today to use mysqli. No biggie, just had to change a few statements. Everything went fine, no errors... Except that I can't get it to execute any queries at all. I have double and triple checked my syntax. I even started creating situations where it SHOULD return an error (Trying to get it to INSERT to Tables that don't exist or with values that exceeded column limits or didn't match type) and... nothing. It doesn't return an error, it doesn't write. It WILL complain if parameter one is NOT a mysqli type.

Here's the relevant code:

$con = mysqli_connect("localhost", "root", "","test");

if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

$query = "INSERT INTO files VALUES (NULL, 5, 'hello')";
mysqli_query($con, $query);

And nothing. It runs without a problem, but it never writes the record. I can even change the $query to "hdjhkfhhjfkd" and it runs no problem. mysqli_query() just isn't executing, period. The only time I can get it to react is if I change $con to anything else, then it complains that it needs a mysqli type.

Thoughts? THis is driving me bonkers.

like image 596
GilloD Avatar asked Feb 21 '10 05:02

GilloD


People also ask

Why does mysqli_query return false?

php, it displays the following: connected but not executed. That means that it connects right, but the mysqli_query() returns false. However, mysqli_error is empty.

What does the mysqli_query () function do?

The query() / mysqli_query() function performs a query against a database.

What will be the return value of mysqli_query () function on error?

Return value:Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

How many parameters can be used with the mysqli_query () function?

PHP uses mysqli query() or mysql_query() function to drop a MySQL database. This function takes two parameters and returns TRUE on success or FALSE on failure.


1 Answers

Have you checked the return value of mysqli_query()? It returns FALSE if an error occurred. In that case mysqli_error() gives you more information about the error.

<?php
$con = mysqli_connect("localhost", "root", "", "test");
if (mysqli_connect_errno()) {
  printf("Connect failed: %s\n", mysqli_connect_error());
  exit();
}

$query = "INSERT INTO files VALUES (NULL, 5, 'hello')";
echo "<pre>Debug: $query</pre>\m";
$result = mysqli_query($con, $query);
if ( false===$result ) {
  printf("error: %s\n", mysqli_error($con));
}
else {
  echo 'done.';
}
like image 187
VolkerK Avatar answered Oct 07 '22 18:10

VolkerK