我正在尝试为即将举行的婚礼设置在线RSVP。我试图通过GoDaddy的托管使我的代码从使用SQLServer到mySQL。在我的数据库连接文件中,我有以下代码:
<?php
// function to connect to the database
function dbConnect()
{
$serverName = 'localhost';
$uName = 'codytaylorbrown';
$pWord = 'G0Braves!';
$db = 'BrownWedding';
try
{
//instantiate a PDO object and set connection properties
$conn = mysql_connect($serverName , $uName , $pWord );
mysql_select_db($db, $conn);
//return connection object
return $conn;
}
// if connection fails
catch (PDOException $e)
{
die('Connection failed: ' . $e->getMessage());
}
}
//method to execute a query - the SQL statement to be executed, is passed to it
function executeQuery($query)
{
// call the dbConnect function
$conn = dbConnect();
try
{
// execute query and assign results to a PDOStatement object
$stmt = $conn->query($query);
do
{
if ($stmt->columnCount() > 0) // if rows with columns are returned
{
$results = $stmt->fetchAll(PDO::FETCH_ASSOC); //retreive the rows as an associative array
}
} while ($stmt->nextRowset()); // if multiple queries are executed, repeat the process for each set of results
die;
//call dbDisconnect() method to close the connection
dbDisconnect($conn);
return $results;
}
catch (PDOException $e)
{
//if execution fails
dbDisconnect($conn);
die ('Query failed: ' . $e->getMessage());
}
}
function dbDisconnect($conn)
{
// closes the specfied connection and releases associated resources
$conn = null;
}
?>
当我打开我的RSVP.php页面,其中包含一个表单并引用另一个php文件将结果发布到数据库时,我收到错误
Fatal error: Call to a member function query() on a non-object in /home/ctbrown24/public_html/DBConnect.php on line 50
我并不是非常精通php并且正在建立我的婚礼网站作为一种学习方式,所以任何帮助都将受到赞赏。 TIA!
答案 0 :(得分:0)
您使用mysql API在第一步中连接到数据库,但是如果您使用的是mysqli API,那么您将尝试检索预期的连接对象。您对查询方法的后续调用不起作用,因为您没有处理mysqli连接对象。
阅读本文以获取有关此主题的更多信息: http://php.net/manual/en/mysqlinfo.api.choosing.php