0

I'm trying to print a row from a table in mysql using a PHP script - here is the code but its not working - it shows Bad query apparently there is no issue:

$conn = mysqli_connect($servername, $username, $password, $db);
if (!$conn) {
 die("Connection failed: " . mysqli_connect_error());
}
else{
echo "Connected successfully";
}
$any = "show columns from help_category";
$chk = mysqli_query($any) or die("Bad Query: $sql");
echo"<table border='3'>";
echo"<tr><td>Type</td><tr>";
while($row = mysqli_fetch_assoc($chk))
{
echo"<tr><td>{$row['Type']}</td><tr>";
}
echo"</table>";
Randi Vertongen
16.6k4 gold badges36 silver badges64 bronze badges
asked Mar 29, 2019 at 12:05
3
  • this script just shows connected successfully and Bad Query after that, i cant find any bad query logs in the mysql log files. Can you confirm where shall i check them ? im using debian 9 Commented Mar 29, 2019 at 12:31
  • First - run the query "raw" from the mysql CLI (Command Line Interface which, funnily enough, is called mysql!). From there you will have a meaningful (i.e. direct from the server's mouth) error message - when your SQL works from mysql, then "translate" it into PHP! This should be your default method of working! Commented Mar 29, 2019 at 12:37
  • i have run these queries in mysql already and they are working there. it seems that its any issue while i embed it in php. Commented Mar 29, 2019 at 12:53

1 Answer 1

0

I have checked your code and i found out that you have issue with your query execution on php function mysqli_query

mysqli_query requires two parameter first one is connection object and second one is query variable to execute a query.

Below is exact code:-

$conn = mysqli_connect($servername, $username, $password, $db);
if (!$conn) {
 die("Connection failed: " . mysqli_connect_error());
}
else{
echo "Connected successfully";
}
$any = "show columns from transaction";
$chk = mysqli_query($conn, $any) or die("Bad Query: $sql");
echo"<table border='3'>";
echo"<tr><td>Type</td><tr>";
while($row = mysqli_fetch_assoc($chk))
{
echo"<tr><td>{$row['Type']}</td><tr>";
}
echo"</table>";
Paul White
95.4k30 gold badges440 silver badges689 bronze badges
answered Mar 29, 2019 at 12:36

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.