Home > Enterprise >  SQL Query not working but works in another file
SQL Query not working but works in another file

Time:10-09

Sorry I'm new to this.. and I am not yet versed with PDO so I used mysqli. I am working on a php file that checks if a name is already present in the table. I tried the query in phpMyAdmin and it returns a row:

select * from webdir where trcode='WEB-ORSBBZ3O7J' and fullname='TEST TEST' ->Returns 1 row

But when I use it in my php file:

include "connection.php"; 
$st="select * from webdir where trcode='$trcode' and fullname='$acctname'";     
$rst=mysqli_query($con, $st);
$rs=mysqli_num_rows($rst); >  
echo $rs;exit();

It returns zero.

The connection string works because its being used by another query. It has worked on my other page but not here..

Thanks!

CodePudding user response:

You may use like this:

$res = mysql_query("SELECT table_name FROM information_schema.tables WHERE table_schema = '$databasename' AND table_name = '$tablename';");

CodePudding user response:

What is the ">" in your php code? May be you are having an error for that, check server log, or remove that from your code. The correct code will be, assuming you are getting $con working right.

include "connection.php"; 
$st = "select * from webdir where trcode='$trcode' and fullname='$acctname'";     
$rst= mysqli_query($con, $st);
$rs = mysqli_num_rows($rst); 
echo $rs;
exit();

Side Note: Use prepared statement for your query to prevent SQL-Injection, if $trcode and $acctname comes from web then your DB may get compromised.

  • Related