检查表中是否存在结果时出错

时间:2014-02-27 02:26:44

标签: php mysql

所以我正在构建一个表session

session

session_name | *Text*
session_number | *auto_increment*

我想检查表中是否已存在session_name结果,如果没有添加唯一的session_number,则创建一个具有该数字的会话,如果 将session_number添加到会话中:

尝试

session_start();
ob_start();

if(isset($_POST['enter'])){
    if($_POST['name'] != ""){
        $_SESSION['name'] = stripslashes(htmlspecialchars($_POST['name']));
    }
}
$name = ucwords(strtolower($_SESSION['name']));

$method = "SELECT * FROM session WHERE session_name = '$name'";
$resulty = mysql_query($con, $method);
//where $con is the connection to my database
if(mysql_num_rows($resulty) == 0) {
    //not found
     $sql="INSERT INTO session (session_name)

     VALUES

     ('$name')";

}

$result = mysql_query("SELECT * FROM session WHERE session_name = '$name'");

$row = mysql_fetch_row($result);

echo $row[0]; 
echo $row[1]; // not sure which one is the session_number
mysqli_close($con);

首先我收到错误

Warning: mysql_query() expects parameter 1 to be string, object given

Warning: mysql_num_rows() expects parameter 1 to be resource, null given 

怎么回事?


另外,我如何将session_number存储到另一个会话?

2 个答案:

答案 0 :(得分:1)

你反过来了,$query是第一个参数,$con对象是第二个参数

$method = "SELECT * FROM session WHERE session_name = '$name'";
$resulty = mysql_query($method, $con);

请升级到mysqli_

答案 1 :(得分:-1)

您的第二个错误是由您的第一个错误造成的。

您对mysql_query的调用应该类似于

$resulty = mysql_query($method);