Jump to content

Changing website from mysql to mysqli


mike12255

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?

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.