2016-11-18 133 views
0

功能在main.php文件如何通过一个PHP变量到一个SQL SELECT查询

public function get_email_details($email, $table_name) 
{ 
    $query_verify_email = "SELECT * FROM $table_name where u_email='$email'"; 
    $result = mysqli_query($this->connection,$query_verify_email) or die(mysql_error()); 
    return $result; 
} 

,我用这个

$result_get_email_details = $Object->get_email_details($email, $table_name); 
$rows=mysql_fetch_array($result_get_email_details); // It is showing me error on this line 

错误我称之为:mysql_fetch_assoc( )期望参数1是资源,在上面给出的对象之后,我评论。

请帮我出哪里是在这个SQL查询

+1

试试这样'where u_email ='{$ email}'' –

+3

'mysqli' and'mysql' ??? ???你不能同时使用两者。顺便说一句'mysql'已被弃用。 – Manikiran

+1

更具体地说,您的问题是如何将变量传递给SQL,http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php。 – chris85

回答

5

那么你正在使用mysql_fetch_query错误...

岂不是mysqli_fetch_query。注意mysql中缺少的i

0

使用此,

$result_get_email_details = $Object->get_email_details($email, $table_name); 
$rows=mysqli_fetch_array($result_get_email_details); 
0

尝试使用foreach($rows as $row)因为$行是数组的集合。它不是一个单一的字符串。

-1

mysqli_fetch_array()需要两个参数,请尝试将您连接和查询对象

mysql_fetch_array($this->connection, $result_get_email_details); 
+1

不,它不会http://php.net/manual/en/mysqli-result.fetch-array.php和'mysql_ *'不能与'mysqli'一起使用。 – chris85

-2
$query = "SELECT * FROM " .$table_name. " where u_email=".$email; 
$result = mysqli_query($connection_variable, $query); 
mysqli_fetch_array($result ,MYSQL_ASSOC); 
+1

不工作和答案应该有一个解释。电子邮件将始终是一个字符串,因此需要引用它。还有SQL注入,这和OP一样。 – chris85

+0

知道请检查一下 –

+0

你应该提供你的答案解释,这将有助于OP了解你的答案。仅提供代码将无助于OP。 –

1

请使用mysqli_fetch_query,而不是mysql_fetch_query

在这里,你在错过你的代码。请改变这一点。