I keep getting an error message:
Fatal error: Call to a member function execute() on a non-object in C:\wamp\www\Website\index.php on line 46
Below is my code. What could be going wrong? I am just using the stock root account for now.
<?php
$dbserver="localhost";
$username="root";
$password="";
$connection=mysql_connect("$dbserver","$username","$password")or die("could not connect to the server");
//execute an SQL statement and return a recordset
$rs = $connection->execute("SELECT product_name FROM products");
$num_columns = $rs->Fields->Count();
echo "<table border='1'>";
echo "<tr><th>Name</th></tr>";
while (!$rs->EOF) //looping through the recordset (until End Of File)
{
echo "<tr>";
for ($i=0; $i < $num_columns; $i++) {
echo "<td>" . $rs->Fields($i)->value . "</td>";
}
echo "</tr>";
$rs->MoveNext();
}
echo "</table>";
//close the recordset and the database connection
$rs->close();
$rs = null;
$conn->close();
$conn = null;
?>
The function mysql_connect returns a resource. This means the variable $connection is not an object and so you cannot call the function execute on it. This explains the error message.
Most of the database-related function calls in the code (Fields, MoveNext, ...) seem to be ADOdb-related. These will not work on a resource (or rowset) created by the mysql_* functions.
See the documentation for ADOdb and the documentation for the mysql extension for PHP for more information.
Since the original mysql extension for PHP is deprecated and ADOdb is not updated very frequently, I would suggest using PDO for your database access.
Related
this is my first question here so feedback on how to improve my questions would be appreciated.
I am trying to display records from a database using PHP.
Here is the code
<?php
$dbConn = new mysqli('localhost', 'twa037', 'twa037Dg', 'autoservice037');
if($dbConn->connect_error) {
die("failed to connect to the database: " . $dbConn->connect_error);
}
else
{
echo "success";
}
$sql = "select * from customer ";
if ($dbConn->query($sql) )
{
echo "query successful";
}
while($row = $sql->fetch_assoc()) {
echo $row['familyName'];
}
$dbconn->close();
?>
I am able to connect to the database and also the query appears to be working fine as it is displaying "success" and "query successfull".
However, I am getting this error
Fatal error: Uncaught Error: Call to a member function fetch_assoc()
on string
Before you guys suggest this could be a duplicate of another post, I have noticed that in the other posts they have used the same code as I have without an issue.
mysqli_query returns a result on that you can call fetch_assoc(). not on the querystring itself.
if ($result = $dbConn->query($sql) )
{
echo "query successful";
}
while($row = $result->fetch_assoc()) {
more informations you can find here
while($row = $sql->fetch_assoc()) <=> while($row = $dbConn->query($sql)->fetch_assoc())
I am trying to build a simple custom CMS, but I'm getting an error:
Warning: mysqli_query() expects parameter 1 to be MySQLi, null given in
Why am I getting this error? All my code is already MySQLi and I am using two parameters, not one.
$con=mysqli_connect("localhost","xxxx","xxxx","xxxxx");
//check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL:" . mysqli_connect_error();
}
function getPosts() {
$query = mysqli_query($con,"SELECT * FROM Blog");
while($row = mysqli_fetch_array($query))
{
echo "<div class=\"blogsnippet\">";
echo "<h4>" . $row['Title'] . "</h4>" . $row['SubHeading'];
echo "</div>";
}
}
As mentioned in comments, this is a scoping issue. Specifically, $con is not in scope within your getPosts function.
You should pass your connection object in as a dependency, eg
function getPosts(mysqli $con) {
// etc
I would also highly recommend halting execution if your connection fails or if errors occur. Something like this should suffice
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); // throw exceptions
$con=mysqli_connect("localhost","xxxx","xxxx","xxxxx");
getPosts($con);
use global scope on your $con and put it inside your getPosts() function
like so.
function getPosts() {
global $con;
$query = mysqli_query($con,"SELECT * FROM Blog");
while($row = mysqli_fetch_array($query))
{
echo "<div class=\"blogsnippet\">";
echo "<h4>" . $row['Title'] . "</h4>" . $row['SubHeading'];
echo "</div>";
}
}
The getPosts() function seems to be expecting $con to be global, but you're not declaring it as such.
A lot of programmers regard bald global variables as a "code smell". The alternative at the other end of the scale is to always pass around the connection resource. Partway between the two is a singleton call that always returns the same resource handle.
I want to check whether a specific entry is present in my database or not. If present then condition,if not then condition. I tried this code but got errors
<?php
$con=mysqli_connect("localhost","root","","student");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$classname = mysqli_real_escape_string($con, $_POST['class']);
$result = mysql_query("SELECT * FROM subjectinfo WHERE class = '{$classname}'", $con);
if(mysql_num_rows($result) == 0)
{
echo "No Such Entry In Table. Please ADD it First.";
}
else
{
echo "Entry Available";
}
}
mysqli_close($con);
?>
Errors :
Warning: mysql_query() expects parameter 2 to be resource, object given in C:\xampp\htdocs\pages\test.php on line 11
Warning: mysql_num_rows() expects parameter 1 to be resource, null given in C:\xampp\htdocs\pages\test.php on line 13
No Such Entry In Table. Please ADD it First.
Like your comments. Make sure you don't mix up mysqli and mysql. mysql is deprecated so you're better off using mysqli.
$con=mysqli_connect("localhost","root","","student");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$classname = mysqli_real_escape_string($con, $_POST['class']);
$result = mysqli_query($con, "SELECT * FROM subjectinfo WHERE class = '{$classname}'");
if(mysqli_num_rows($result) == 0)
{
echo "No Such Entry In Table. Please ADD it First.";
}
else
{
echo "Entry Available";
}
}
mysqli_close($con);
?>
You are mixing MySQL APIs - mysql_ + mysqli_ they do not mix together. Plus, your DB connection's backwards in your query. The connection comes first.
Here:
<?php
$con=mysqli_connect("localhost","root","","student");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$classname = mysqli_real_escape_string($con, $_POST['class']);
$result = mysqli_query($con,"SELECT * FROM subjectinfo WHERE class = '{$classname}'");
if(mysqli_num_rows($result) == 0)
{
echo "No Such Entry In Table. Please ADD it First.";
}
else
{
echo "Entry Available";
}
}
mysqli_close($con);
?>
Also use or die(mysqli_error($con)) to mysqli_query()
Plus, add error reporting to the top of your file(s) which will help during production testing.
error_reporting(E_ALL);
ini_set('display_errors', 1);
Look into using prepared statements, or PDO with prepared statements, they're safer.
An insight:
Make sure your form element is indeed named.
I.e.:
<input type="text" name="class">
otherwise, you will receive an Undefined index class... warning.
I am trying to build a simple custom CMS, but I'm getting an error:
Warning: mysqli_query() expects parameter 1 to be MySQLi, null given in
Why am I getting this error? All my code is already MySQLi and I am using two parameters, not one.
$con=mysqli_connect("localhost","xxxx","xxxx","xxxxx");
//check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL:" . mysqli_connect_error();
}
function getPosts() {
$query = mysqli_query($con,"SELECT * FROM Blog");
while($row = mysqli_fetch_array($query))
{
echo "<div class=\"blogsnippet\">";
echo "<h4>" . $row['Title'] . "</h4>" . $row['SubHeading'];
echo "</div>";
}
}
As mentioned in comments, this is a scoping issue. Specifically, $con is not in scope within your getPosts function.
You should pass your connection object in as a dependency, eg
function getPosts(mysqli $con) {
// etc
I would also highly recommend halting execution if your connection fails or if errors occur. Something like this should suffice
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); // throw exceptions
$con=mysqli_connect("localhost","xxxx","xxxx","xxxxx");
getPosts($con);
use global scope on your $con and put it inside your getPosts() function
like so.
function getPosts() {
global $con;
$query = mysqli_query($con,"SELECT * FROM Blog");
while($row = mysqli_fetch_array($query))
{
echo "<div class=\"blogsnippet\">";
echo "<h4>" . $row['Title'] . "</h4>" . $row['SubHeading'];
echo "</div>";
}
}
The getPosts() function seems to be expecting $con to be global, but you're not declaring it as such.
A lot of programmers regard bald global variables as a "code smell". The alternative at the other end of the scale is to always pass around the connection resource. Partway between the two is a singleton call that always returns the same resource handle.
My project is working fine on my local machine but not on the web server. I think it is the stored procedures, because the error that I am getting is:
Fatal error: Call to a member function fetch_array() on a non-object in ...
The collation of the database is "utf8_general_ci".
Just a simple example:
I have a stored procedure called offices:
CREATE PROCEDURE offices()
BEGIN
SELECT * FROM offices;
END//
And the php code:
<?php
require ("db.php");
$db = dbConnect();
$result = $db->query("CALL offices()");
while(list($id, $city, $address) = $result->fetch_array())
echo "($id) $city: $address ";
?>
I don't think it matters which machine your code is on. This kind of error can occur anywhere.
I do not know much about the interface of your $db->query() method.
Is it returning 'null' upon a 'result error'?
Personally, I would add a bit of error checking between:
the call to $db->query()
and
usage of $result->fetch_array()
ie something along the lines of:
<?php
require ("db.php");
$db = dbConnect();
$result = $db->query("CALL offices()");
if (!(is_object($result)))
{
throw new Exception('No result returned from query: ' . $db->getLatestError() );
}
//count rows
if ($result->numberOfRows() < 1)
{
//do something for no rows
echo "No Offices found\n";
return;
}
while(list($id, $city, $address) = $result->fetch_array())
echo "($id) $city: $address
";
?>