-3
\$\begingroup\$

There are many files [ file1.php, file2.php ] in our project & i wrote database connection code in many files instead of single file [ database.php ] , is there any problem with that ?

if it is wrong than please help me what code i need to change in file1.php to connect to database.php

Below 2 lines i used in file1.php

$con=mysqli_connect('localhost', 'root', 'gfgf', 'g');
if ($result=mysqli_query($con,$sql))

Instead of above 2 lines, is it correct to use below 2 lines ?

require_once("database.php");
$db_handle = new DBController();

file1.php

<?php

require_once("database.php");
$db_handle = new DBController();

$con=mysqli_connect('localhost', 'root', 'gfgf', 'g');

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

$todaydate=date('Y-m-d');

$sql="SELECT * FROM orders WHERE importdate >= NOW() - INTERVAL 1 DAY";

    if ($result=mysqli_query($con,$sql))
  {

  $rowcount=mysqli_num_rows($result);

  printf("Uploaded - %d rows.\n",$rowcount);

  // Free result set
  mysqli_free_result($result);
  }

mysqli_close($con);


?>  
\$\endgroup\$
4
  • \$\begingroup\$ please tell me why downvotes, so that i will correct it..... \$\endgroup\$ Aug 29, 2018 at 12:21
  • \$\begingroup\$ Asking others why they downvoted is useless. Often you get no response. (Hint: You didn't explain your code that well) \$\endgroup\$ Aug 29, 2018 at 12:46
  • \$\begingroup\$ @FreezePhoenix Thanks for suggestion, when i use this code : require_once("database.php"); $db_handle = new DBController(); its not give correct rows of table.... \$\endgroup\$ Aug 29, 2018 at 13:01
  • \$\begingroup\$ can you please help me for this ? \$\endgroup\$ Aug 29, 2018 at 13:28

1 Answer 1

2
\$\begingroup\$

database connection code in many files instead of single file, is there any problem with that?

Nothing critical, but what if your database credentials would change? Are you going to edit all these files?

is it correct to use below 2 lines ?

Yes, apart from the fact that DBController doesn't seem to belong here. Where did you get that and why do you think it is needed?

Besides, in your example you seem to be using both ways, including database.php and writing the connection code right in the script. You should use only one method.

there are other issues in your code

  • you should never ever use a code like echo "Failed to connect to MySQL: " . mysqli_connect_error();. Read my article on the proper error reporting
  • same goes for the if ($result=mysqli_query($con,$sql)) condition. It effectively mutes the possible error message which will never reach you. You should never run a query inside a condition like that. Just let the error go.
  • you should never ever select all rows from a database only to count them. Ask a database to count the rows instead.
\$\endgroup\$
5
  • \$\begingroup\$ Thanks for complete answer , please tell me what code i need to replace instead of if ($result=mysqli_query($con,$sql)) ? \$\endgroup\$ Aug 29, 2018 at 12:24
  • \$\begingroup\$ just $result=mysqli_query($con,$sql); without a condition \$\endgroup\$ Aug 29, 2018 at 12:30
  • \$\begingroup\$ i updated $result=mysqli_query($con,$sql); , also removed these 2 lines : $con=mysqli_connect('localhost', 'root', 'gfgf', 'g'); and if ($result=mysqli_query($con,$sql)) , but now i am getting result as Uploaded - 0 rows., before i was getting 10 rows...... \$\endgroup\$ Aug 29, 2018 at 12:38
  • \$\begingroup\$ can you please help me for this ? \$\endgroup\$ Aug 29, 2018 at 13:28
  • \$\begingroup\$ I doubt it is possible. I just have no idea how your code does look like. When asking for a code review you are supposed to understand the suggestion and implement it consciously, not just copy and paste it. Try to understand what is said in my answer and implement it by yourself. \$\endgroup\$ Aug 29, 2018 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.