-1

In my php.Mysql ($sql) result should be 1.Sysum should not be empty in another word, mysql_num_rows($sel) > 0 should be true.

But actually,mycode can not work.I don't know why.Who can help me?

$conn = new PDO('mysql:host=localhost;port=3306;dbname=hpc' , 'root' , 'Yd');
$Name=$_COOKIE['PName'];
$sql = "select sysnum from hpc where handler='".$Name."' and stat='N';";
$sel=$conn->query($sql);
if(mysql_num_rows($sel) > 0)
{       
 mycode;
}
undefined_variable
  • 6,180
  • 2
  • 22
  • 37
john
  • 55
  • 1
  • 7
  • My conn is $conn = new PDO('mysql:host=localhost;port=3306;dbname=hpc' , 'root' , 'Yd'); – john Jul 26 '17 at 08:03
  • Can you show us your connection? What inside `$conn` ? Did you used mysql, mysqli or PDO to connect to your database? – Twinfriends Jul 26 '17 at 08:04
  • Don't use `mysql_*` functions... they are deprecated. And DEFINITELY don't parse something straight out of a cookie into your query... – Ivar Jul 26 '17 at 08:04
  • You're mixing `PDO` and `mysql` APIs, those are two different things. – Rajdeep Paul Jul 26 '17 at 08:05
  • 4
    Possible duplicate of [Can I mix MySQL APIs in PHP?](https://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php) – Rajdeep Paul Jul 26 '17 at 08:05

2 Answers2

1

Oh boy that went quite fast with your comment, even before mine :P

So, the answer is simple: You can't use mysql_num_rows on a PDO connection. That are two different drivers. Thats like you try to start the car of your wife with your own keys. Principally, both are keys, but they don't work with any car.

Its the same here in your case. mysql_num_rows comes from the OUTDATED mysql driver. (Don't use mysql_* at all, its not longer supported in PHP7 and deprecated). To do it with PDO is the right way, but you have to adapt your code a bit.

$Name=$_COOKIE['PName'];
$sql = "select count(sysnum) from hpc where handler='".$Name."' and stat='N';";
$sel=$conn->query($sql);

$rows = $sel->rowCount();

echo $rows;

Hope that work, if not, let me know. I know I had some troubles the last time I tried to do a rowCount with PDO.

IMPORTANT - DANGER:

Also I really recommend you to take a look at http://bobby-tables.com and learn something about SQL injection. Your code actually is not save at all and your database can be hacked quite easy. Since you're using PDO, nothing stops you from using prepared statements, what would make your code much better.

Hope I could help you. If you have any other questions or something shouldn't work, please let me know.

Twinfriends
  • 1,972
  • 1
  • 14
  • 34
-1

fetch result into an array and count items using count function, like this:

$Name=$_COOKIE['PName'];
$sql = "select sysnum from hpc where handler='".$Name."' and stat='N';";
$sel = $conn->query($sql)->fetchall();
if(count($sel) > 0){       
 mycode;
}
aidinMC
  • 1,415
  • 3
  • 18
  • 35