-1

I am getting a couple of errors when it comes to the use of MySql instead of MySqli. That MySql had been depracated. I am applying MySqli functions in the website, yet something that I need in the database connection folder uses MySql. I have tried reading the PHP MySqli documentation in order to figure out how to change it to MySqli, but it didn't work.

The function is:

This is to establish a connection to the MySql server

$this->conn = $func($this->host, $this->user, $this->password);
if (!$this->conn) {
return false;
}

And the following is to request the database

if (@!mysql_select_db($this->database, $this->conn)) {
return false;
}
return true;
}

Another thing that is happening is that in the admin panel I am trying to create page numberings to fetch the data, since sometimes the data inputted is too much. I am doing this through a for loop from the $start till the $end with an incrementation of i. What's happening is that I am using the mysql_result(), when I switch to mysqli it gives an error that this function is not defined. I tried using mysqli_fetch_all() yet it didnt work.

The following code is happening through the for loop that I mentioned above:

        echo '<td>' . mysqli_result($result, $i, 'productid') . '</td>';

        echo '<td>' . mysqli_result($result, $i, 'productname') . '</td>';

The error given for the above code is:

Call to undefined function mysqli_result() 
AJ1
  • 3
  • 1
  • 8
  • Can you show me your php version ? – Harutyun Abgaryan Jan 04 '15 at 10:57
  • there is no such counter part in `mysqli_*`. thus the undefined function error. – Kevin Jan 04 '15 at 10:59
  • Changing from mysql to mysqli is not exactly as straight forward as adding an `i` to all function calls. Oftentimes the functions will be *similar*, but not identical. Read the documentation examples how to use mysqli properly and convert your code accordingly. – deceze Jan 04 '15 at 11:25
  • possible duplicate of [The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead](http://stackoverflow.com/questions/13944956/the-mysql-extension-is-deprecated-and-will-be-removed-in-the-future-use-mysqli) – Sahil Jan 04 '15 at 11:58

2 Answers2

0

It is recommended that you fetch the result like this:

while ($row = mysqli_fetch_assoc($result)){

     echo "<td>" . $row['productid'] . "</td>";
     echo "<td>" . $row['productname'] . '</td>';

}

If you are still getting an error on mysqli, then you need to check your PHP version that should be 5.0.0 or above.

Add <?php phpinfo() ?> to your code to get the installed PHP version on your server.

Update

Based on your comments, you want to add the paging feature when displaying the data of productid and productname in table, now first you should use the Limit functionality of MySQL , read here http://www.w3schools.com/php/php_mysql_select_limit.asp

and then you should write a piece of code before your select query to provide it with the pagesize and pageindex, the pageindex will be calculated based on the pagesize and currentpage , also you should have a seperate query to get the total number of rows to be able to calculate the number of pages.

This question Simple PHP Pagination script has a very comprehensive answer that will explain to you how to do the paging.

Community
  • 1
  • 1
Aram Tchekrekjian
  • 925
  • 11
  • 26
  • no it didn't work, it gives an error on each line. "Undefined variable: row " – AJ1 Jan 04 '15 at 11:24
  • did you include the curly braces of the while loop? – Aram Tchekrekjian Jan 04 '15 at 11:35
  • It's printing all the results in a horizontal way. What I want to do is to print them vertically and for them to be page numbered. – AJ1 Jan 04 '15 at 11:39
  • I have managed to use what you said and it worked but it's printing everything and not separated by pages – AJ1 Jan 04 '15 at 11:44
  • Tha's why I decided to use a for loop. – AJ1 Jan 04 '15 at 11:46
  • Now is your first issue solved? are you still getting the `Undefined variable: row` error? and What do you mean by `horizontal way` , is it related to how you want to display the productid and the productname on html ? – Aram Tchekrekjian Jan 04 '15 at 11:47
  • I want to display them in a page numbering way. Now with the use of the while loop the results aren't breaking, thus all the results are being printed on-to one page – AJ1 Jan 04 '15 at 11:49
  • So now you have a different request, you want to do paging for your data productid and productname. Check my updated answer for hints to doing paging with PHP. Am sorry I won't be able to provide a full solution here, since there are many parts that should be changed in your code to do paging. – Aram Tchekrekjian Jan 04 '15 at 13:39
-1

MySql extension had been long deprecated. You should convert your PHP scripts to use MySQLi extension. This extension is available from PHP ver 4.1.3 onwards and provides many advanced features like object oriented implementation. It seems you have some legacy code and you need to replace MySql extension functions with MySqli functions, which is not too hard to do. It will be easier to help you if you can paste the full code.

As per your question, first you need to change the the way MySqli connects to database. Unlike MySql extension which has 2 functions to connect and select database. MySqli extension does this in one function call. (note: following is procedural style code which is similar to MySql extension, same can be done in object style code.)

$link = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');

Then you query your database with created connection above '$link'

$result = mysqli_query($link, 'SELECT * FROM emp ORDER BY id LIMIT 0,10');

The '$result' is the resource link to your fetched data. You can use many functions available in this new extension to fetch result data. There is no function named 'mysqli_result', thats why you get error.

mysqli_fetch_all, mysqli_fetch_assoc, mysqli_fetch_array etc.

Look at this link: http://php.net/mysqli_result

Depending upon the way code is written, you might have to change code to fetch results at many places. But this is the way you have to do it.

Hope this helps, ask any question you have.

Sahil
  • 489
  • 6
  • 12
  • A lot of being said, but still ain't an answer – Royal Bg Jan 04 '15 at 11:30
  • 'AJ1; it seems like you are not able to understand the MySqli extension at all. My answer does provide all the information you need. It seems instead of working it out you are just looking for copy & paste code. I agree with @deceze you should read the documentation & understand the extension. I tried to summarise it for you but it was clear waste of time. – Sahil Jan 04 '15 at 11:54