Jump to content

Recommended Posts

Hey there im trying to change my website from mysql statments to mysqli. I tried changing one of my simple statments to get some news from the db and im running into the following error:

 

Fatal error: Call to a member function query() on a non-object in

 

Here is the code I am using, does anyone know what/why the error is producing:

 

$sql = "SELECT * FROM news ORDER by id DESC LIMIT 2,0";
         $result = $mysqli->query($sql);
         While ($row = $result->fetch_array(MYSQLI_BOTH)){

          echo "<li class=\"clear\">";
          if ($img = $row['image'] != ""){
          echo "<div class\"imgl\"><img src=\".$img.\" alt=\"\"/></div>";
          }
          echo "<div class=\"latestnews\">".
          "<p><a href=\"news.php?id=".$row['id'].">".$row['title']."</a></p><p>".$row['content']."</p></div>";
         }

Link to comment
https://forums.phpfreaks.com/topic/274569-changing-website-from-mysql-to-mysqli/
Share on other sites

Could be a silly question but you have formed the connection via mysqli to create an object? i.e.

 

$mysqli = new mysqli("localhost", "user", "password", "database");

 

And are you sure the sql query is sound?

 

Hey there im trying to change my website from mysql statments to mysqli. I tried changing one of my simple statments to get some news from the db and im running into the following error:

 

 

 

Here is the code I am using, does anyone know what/why the error is producing:

 

$sql = "SELECT * FROM news ORDER by id DESC LIMIT 2,0";
$result = $mysqli->query($sql);
While ($row = $result->fetch_array(MYSQLI_BOTH)){

echo "<li class=\"clear\">";
if ($img = $row['image'] != ""){
echo "<div class\"imgl\"><img src=\".$img.\" alt=\"\"/></div>";
}
echo "<div class=\"latestnews\">".
"<p><a href=\"news.php?id=".$row['id'].">".$row['title']."</a></p><p>".$row['content']."</p></div>";
}

I have setup the connection in a seperate file (tested it is working) with the following code:

 

function MySQLDB(){
/* Make connection to database */
$this->connection = new mysqli( DB_SERVER, DB_USER, DB_PASS, DB_NAME) or die ('Cannot open database');
//mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
// mysql_select_db(DB_NAME, $this->connection) or die(mysql_error());

 

So when I changed my code in my index file to

 

$session->connection;
$result = $connection->query($sql);
While ($row = $result->fetch_array(MYSQLI_BOTH)){

echo "<li class=\"clear\">";
if ($img = $row['image'] != ""){
echo "<div class\"imgl\"><img src=\".$img.\" alt=\"\"/></div>";
}
echo "<div class=\"latestnews\">".
"<p><a href=\"news.php?id=".$row['id'].">".$row['title']."</a></p><p>".$row['content']."</p></div>";
}
$connection->close();

 

Im sure my issue is when trying to use $connection as my database connector im just not sure why/what I am doing wrong?

Edited by mike12255

I think you need to post the full code but based on what I can see there you setup the connection on '$this->connection' so when you try to retrieve the result you should write it:

$result = $this->connection->query($sql);

 

Also in your index file trying using var_dump($this->connection); to see what you're getting.

 

 

 

I have setup the connection in a seperate file (tested it is working) with the following code:

 

function MySQLDB(){
/* Make connection to database */
$this->connection = new mysqli( DB_SERVER, DB_USER, DB_PASS, DB_NAME) or die ('Cannot open database');
//mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
// mysql_select_db(DB_NAME, $this->connection) or die(mysql_error());

 

So when I changed my code in my index file to

 

$session->connection;
$result = $connection->query($sql);
While ($row = $result->fetch_array(MYSQLI_BOTH)){

echo "<li class=\"clear\">";
if ($img = $row['image'] != ""){
echo "<div class\"imgl\"><img src=\".$img.\" alt=\"\"/></div>";
}
echo "<div class=\"latestnews\">".
"<p><a href=\"news.php?id=".$row['id'].">".$row['title']."</a></p><p>".$row['content']."</p></div>";
}
$connection->close();

 

Im sure my issue is when trying to use $connection as my database connector im just not sure why/what I am doing wrong?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.