Showing posts with label debug. php. Show all posts
Showing posts with label debug. php. Show all posts

2014-08-11

Debugging PHP and MySQL (Part 5)

Debugging PHP with a MySQL database backend is something which most PHP developers would have to do. Here are some tips & suggestions when doing this form of debugging.

#1. Make sure you can connect to the MySQL

It might seem kind of obvious. But, when you have a new project and development environment setup. You may not be able to connect to the database right away
1) Because you forget to start the MySQL database server
2) Because you did not configure the connection passwords correctly
3) You are trying to use root to connect to the database from the web server. (Passwords in mysql are setup per IP. Usually they are only set for access via localhost only, especially for the root user.)

#2. Use a gist of code to test the database connection first

If the gist works, then you should debug the application logic instead of the database connection

<?php
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
if (!$link) {
    die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
}
echo 'Connected... ' . mysqli_get_host_info($link) . "\n";


#3. Check if all the parameters for your SQL query string are actually correct

When the SQL query string looks correct, 99% it is because it is correct and you
are actually using the wrong parameter values and hence causing errors to occur 
when you execute the code.

$sqlstr = 'SELECT * FROM mydbtable WHERE myname = $input_var';
echo $sqlstr;
if (!$mysqli->query($sqlstr)) {
    echo 'Error: ', $mysqli->error;
}


In the code above, you may find that $input_var is actually an empty string and
thus the SQL statement you are running
is SELECT * FROM mydbtable WHERE myname =

This would result in an error.


<?php $link = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db'); if (!$link) { die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error()); } echo 'Connected... ' . mysqli_get_host_info($link) . "\n"; - See more at: http://jason.pureconcepts.net/2013/04/common-debugging-php-mysql/#sthash.6ZkmQHcs.dpuf
<?php $link = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db'); if (!$link) { die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error()); } echo 'Connected... ' . mysqli_get_host_info($link) . "\n"; - See more at: http://jason.pureconcepts.net/2013/04/common-debugging-php-mysql/#sthash.6ZkmQHcs.dpuf
<?php $link = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db'); if (!$link) { die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error()); } echo 'Connected... ' . mysqli_get_host_info($link) . "\n"; - See more at: http://jason.pureconcepts.net/2013/04/common-debugging-php-mysql/#sthash.6ZkmQHcs.dpuf
<?php $link = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db'); if (!$link) { die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error()); } echo 'Connected... ' . mysqli_get_host_info($link) . "\n"; - See more at: http://jason.pureconcepts.net/2013/04/common-debugging-php-mysql/#sthash.6ZkmQHcs.dpuf

Github CoPilot Alternatives (VSCode extensions)

https://www.tabnine.com/blog/github-copilot-alternatives/