Having a problem getting mysqli_query to execute
Having a problem getting mysqli_query to execute
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: %sn", 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.
Does mysqli_query() return something?
– Marcin
Feb 21 '10 at 5:34
No, it doesn't return anything for the particular statement. This is just an INSERT. mysqli is enabled as far as phpinfo() is concerned. I'm running the latest version of Zend Community Edition. PHP 5.3, MYSQL 5.0.5
– GilloD
Feb 21 '10 at 5:38
3 Answers
3
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: %sn", 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: %sn", mysqli_error($con));
}
else {
echo 'done.';
}
Many thanks! Seems like a rogue syntax error. I finally got it run with a lot of single-quote hunting.
– GilloD
Feb 21 '10 at 6:54
You should set display_startup_errors=1 on your development server or keep an eye on the error log. And having some "guaranteed" visual feedback in the portion of the script you have trouble with is also often a good idea.
– VolkerK
Feb 21 '10 at 8:55
public function connection()
{
$con=mysqli_connect('localhost','root','','int');
return $con;
}
public function login($u,$p)
{
$fd=$this->connection();
$sql = "SELECT * FROM user WHERE (uname = '".$u."' AND pass = '".$p."')";
$result = mysqli_query($fd,$sql);
$num = mysqli_num_rows($result);
if($num)
{
return TRUE;
}
else
{
return FALSE;
}
}
Try adding an explanation to your answer, other than simply a block of code.
– Sotiris Kiritsis
Apr 6 '16 at 11:32
I have also suffer from this problem. Finally I found that the problem occur in my connection to the database. You can use this following connection code to connect the database easily.
$db_connect = mysqli_connect("host", "user", "password", "database");
I hope this code will help you. If you have any problem then leave a comment. Thanku.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
have you enabled the mysqli module in the PHP config file. I think it is disabled by default. Have you compiled PHP yourself? What is your configuration?
– Rouan van Dalen
Feb 21 '10 at 5:29