-2

I wrote this code and I ran it on my browser, I get the output as 'not executed'.

I use PHP 5.3 version.

I have already checked out a lot of posts but I seem to figure this error out. and I also tried to use mysqli functions but that also won't work. Please help me figure this out please.

<?php
require 'connect.php';
$query="SELECT * FROM 'masu'";

if($is_query_run=mysql_query($query))
{
    echo 'Query executed';
    while($query_execute=mysql_fetch_array($is_query_run))
    {
         echo "excuted";
    }
}
else
{
    echo "not executed";
}
?>

The code for connect.php is here :

<?php
$mysql_host='localhost';
$mysql_user='root';
$mysql_password='masu';
$con=mysql_connect($mysql_host, $mysql_user,$mysql_password);
if(!@mysql_connect($mysql_host, $mysql_user,$mysql_password))
{
    die('Cannot connect to database');
}
else
{
    if(@mysql_select_db('masudha'))
    {
    //  echo 'connection success with masu';
    }
    else
    {
        die('cannot connect to database');
    }
}
?>
ADyson
  • 57,178
  • 14
  • 51
  • 63
Masudha Meher
  • 93
  • 1
  • 1
  • 7
  • Get rid of error suppression. Don't use `mysql_`. You only use quotes on strings, columns/tables are not strings. – user3783243 Mar 18 '20 at 15:38
  • can you not just have `if (mysql_query($query)) {` as this would mean `if true` – Mech Mar 18 '20 at 15:38
  • You should be switching to mysqli urgently anyway, so you can start to move away from these insecure functions, and the out-of-support version of PHP you're using. So please show us the `mysqli` code you tried, and we can help you fix that instead. – ADyson Mar 18 '20 at 15:39
  • 2
    P.S. Before you show us the new code, remove any error suppression (i.e. remove `@`s before function names) and also ensure you have PHP set to log errors, and mysqli set to throw exceptions when SQL errors occur. See here for guidance: https://stackify.com/php-error-logs-guide/ (php error logging/reporting) https://stackoverflow.com/a/14578644/5947043 (mysqli exception handling) – ADyson Mar 18 '20 at 15:40
  • 1
    Using `mysql_connect` twice also generates two connections for every load. – user3783243 Mar 18 '20 at 15:40
  • "I use PHP 5.3 version." Why? – Patrick Q Mar 18 '20 at 15:42
  • Just remove the quotes from the table name. There isn't anything in it that mysql will complain about. Ticks are usually used in place of quotes, for too many reasons. – Funk Forty Niner Mar 18 '20 at 15:59

1 Answers1

0

Use Backticks for tablenames and columnnames

Single quotes are only valid for strings

$query="SELECT * FROM `masu`";

For more Information

nbk
  • 45,398
  • 8
  • 30
  • 47