mySQLi查询:为什么我没有结果?

时间:2014-10-15 08:20:18

标签: php mysqli size

当我从phpMyAdmin执行相同的查询时,我有结果,因为我们可以看到该链接的图片: http://i59.tinypic.com/ncd2mp.jpg

但是当我从php做同样的事情时,查询的大小是0:

<?php   
//Connect to Database 
$con = mysqli_connect("127.0.0.1","root","","moodle2"); 

//Check connection 
if (mysqli_connect_errno()) { 
    echo 'Database connection error: ' . mysqli_connect_error(); 
    exit(); 
} 

//Escape special characters to avoid SQL injection attacks 
$namesubject="Aplicaciones Telemáticas Multimedia (Telemática)";
$namecategory="HTML CSS JS";
$namesubject=mysqli_real_escape_string($con,$namesubject);
$namecategory=mysqli_real_escape_string($con,$namecategory);

//Query the database to get the user details. 
$query="SELECT id, preguntaid, nombrepregunta, textopregunta, tipopregunta, categorianum FROM mdl_eliza_preguntas WHERE categorianum = (SELECT id FROM mdl_eliza_categoria WHERE namecategoria = '".$namesubject."' AND courseid = (SELECT category FROM mdl_course WHERE fullname = '".$namecategory."')) ORDER BY id";
$userdetails = mysqli_query($con,$query); 
//If no data was returned, check for any SQL errors 
if (!$userdetails) { 
    echo 'Could not run query: ' . mysqli_error($con); 
    exit; 
} 

$size=mysqli_num_rows($userdetails);        
?>

感谢您的时间。

1 个答案:

答案 0 :(得分:1)

根据your originally posted question,在我的回复发布后,您已将'".$namecategory".'更改为'".$namecategory."'


变化:

WHERE fullname = '".$namecategory".')) ORDER BY id";

为:

WHERE fullname = '".$namecategory."')) ORDER BY id";

您错放了引号/点。

这似乎是导致查询失败的最可能原因。