-3

I have this problem with my code. I have a database created in myphpadmin , the program connects to it, but when i try to use mysqli_query it keeps showing "not executed". Can you help me? Maybe i'm missing something

<?php
   $mysqli_host='localhost'; $mysqli_user='root'; $mysqli_password='';
   $conexiune=@mysqli_connect($mysqli_host,$mysqli_user,$mysqli_password);
   @mysqli_select_db('materiale',$conexiune);

 if(!$conexiune=@mysqli_connect($mysqli_host,$mysqli_user,$mysqli_password))
    {
    die('cant connect');    
    }
    else
    {
    echo 'connection success<br>';
    }
 if($is_query_run=@mysqli_query('SELECT * FROM `user`',$conexiune))
    {
    echo 'query executed';
    }
    else
    {
    echo 'not executed' ;   
    }

?>

I need to fetch the information from the database bot it will not work if the query isn't working

  • 2
    Of course there are no errors showing. You're *suppressing errors*. Remove all of the error suppression operators, and check your PHP logs, and check for errors with `mysqli_error`, and debug to ensure your code is executing at all. When you do these things, what specifically is failing? – David Jun 22 '19 at 15:25
  • Sorry if i'm being difficult it's my first day using mysqli. I made some changes to get rid of the errors that eventually showed after deleting the @. No errors showing now. The thing is I dont know why it displays the "not executed" message when i'm checking if the query runs. When i check the connection it shows the "connection soccess" message so that means the connection is working right? – Alexandra Ghezea Jun 22 '19 at 15:42
  • 1
    [How to enable MySQLi exception mode?](https://stackoverflow.com/a/22662582/1839439) – Dharman Jun 22 '19 at 15:42
  • You need to forget about existence of error suppression operator `@` as a beginner. Errors and warnings are your friend. To learn [how to connect to MySQLi try this tutorial](https://phpdelusions.net/mysqli/mysqli_connect). I would recommend to learn PDO instead, because it has simpler syntax and is more powerful. – Dharman Jun 22 '19 at 15:47
  • @AlexandraGhezea: If it’s showing “not executed” then it seems that the query is failing. You need to use mysqli_error($conexiune) to get the error from the database. – David Jun 22 '19 at 15:49
  • I used mysqli_error but it's showing 0 errors – Alexandra Ghezea Jun 22 '19 at 17:09

2 Answers2

0

Try remove the @ before the command .. in PHP @disable the error message

 $conexiune=mysqli_connect($mysqli_host,$mysqli_user,$mysqli_password);

..

 if(!$conexiune=mysqli_connect($mysqli_host,$mysqli_user,$mysqli_password))
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
0

Your parameter order is wrong, mysqli_query() expects the connection handle returned by ´mysqli_connect()` as the first parameter, and the actual query as 2nd parameter, when using the procedural call style.

Looks as if you are mixing parameter semantics of the old, deprecated mysql extension with that of the more modern mysqli one. Or maybe you are trying to port old code that used mysql functions to mysqli by just adding the extra i?

In old mysql the connection handle parameter was optional, and usually the last parameter to be passed when given. This turned out to be a maintenance nightmare over time, and the concept of having a hidden default connection handle was also somewhat questionable.

So there was a clear cut when the new mysqli API was introduced with PHP 5 in 2004, making the connection handle the mandatory first parameter, instead of optional last one, and also adding an object oriented API in addition to the classic procedural one.

PS: also, as mentioned by others already, when trying to track down an error, it is not necessarily the best idea to silence warning and error messages with the @ prefix when calling a function ...

Dharman
  • 30,962
  • 25
  • 85
  • 135
Hartmut Holzgraefe
  • 2,585
  • 12
  • 14
  • Im trying to make something for school and all i have are old courses from when we "learned" mysql 2 years ago – Alexandra Ghezea Jun 22 '19 at 15:47
  • I changed the order and i have no errors now. But i'm still confused about why the query is not executing – Alexandra Ghezea Jun 22 '19 at 15:49
  • Ok, the next step then would be to check for mysql protocol level error messages. Replace `echo 'not executed' ;` with `echo 'error: '.mysqli_error($conexiune);` See also https://www.php.net/manual/en/mysqli.error.php – Hartmut Holzgraefe Jun 22 '19 at 17:14