mike12255 Posted February 16, 2013 Share Posted February 16, 2013 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>"; } Quote Link to comment https://forums.phpfreaks.com/topic/274569-changing-website-from-mysql-to-mysqli/ Share on other sites More sharing options...
Drongo_III Posted February 16, 2013 Share Posted February 16, 2013 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>"; } Quote Link to comment https://forums.phpfreaks.com/topic/274569-changing-website-from-mysql-to-mysqli/#findComment-1412800 Share on other sites More sharing options...
thara Posted February 16, 2013 Share Posted February 16, 2013 Do you make sure your db connection work for you? Try debugging your connection $mysqli = new mysqli($host, $user, $pwd, $db) or die ('Cannot open database'); Quote Link to comment https://forums.phpfreaks.com/topic/274569-changing-website-from-mysql-to-mysqli/#findComment-1412801 Share on other sites More sharing options...
mike12255 Posted February 16, 2013 Author Share Posted February 16, 2013 (edited) 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 February 16, 2013 by mike12255 Quote Link to comment https://forums.phpfreaks.com/topic/274569-changing-website-from-mysql-to-mysqli/#findComment-1412805 Share on other sites More sharing options...
jazzman1 Posted February 16, 2013 Share Posted February 16, 2013 (edited) Why your while loop starts with capital "W" ? Edited February 16, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/274569-changing-website-from-mysql-to-mysqli/#findComment-1412811 Share on other sites More sharing options...
Drongo_III Posted February 16, 2013 Share Posted February 16, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/274569-changing-website-from-mysql-to-mysqli/#findComment-1412823 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.