Convert mysqli_result to mysqli or simular equivalent solution? [duplicate] - php

This question already has answers here:
MySQLi equivalent of mysql_result()?
(12 answers)
Closed 10 months ago.
I try to convert all MySQL codes to mysqli.
I stuck with this
$loginStrGroup = mysql_result($LoginRS,0,'UserLevel');
I tried with some mysqli_result functions but nothing worked

mysql_result() was the second most misused mysql function, next to mysql_num_rows().
It should have never been used with mysql either.
A regular fetch should be used instead of that ugly crutch
$row = mysqli_fetch_assoc($LoginRS);
$loginStrGroup = $row['UserLevel'] ?? false;

Related

I have problem with php code connecting to MySQL [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 3 years ago.
I want to print all database name list that are in my wamp server mysql
and I'm getting some errors
$link_id=mysqli_connect('localhost','root',"");
$a=mysql_list_dbs($link_id);
while($row = mysqli_fetch_object($a))
{
echo $row->Database."<br>";
}
( ! ) Fatal error: Uncaught Error: Call to undefined function mysql_list_dbs() in C:\wamp64\www\connection.php on line 3
( ! ) Error: Call to undefined function mysql_list_dbs() in C:\wamp64\www\connection.php on line 3
You can not mix mysql_* and mysqli_* functions, they are two different APIs. The mysql_* functions have been deprecated and should no longer be used. mysqli_* doesn't have an equivalent function, but you can do mysqli_query($link_id, 'SHOW DATABASES') and then iterate over the results. See mysqli_query() for examples.

"Function eregi() is deprecated" tried preg_match - no bueno [duplicate]

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 4 years ago.
I've read many threads here about this being deprecated and tried replacing it with preg_match but I don't know php enough to fix the rest of the line.
Been enjoying Fotoholder for years, I would switch to a newer similar single file gallery code but then I would lose all my descriptions in the gallery.
Please help resurrect Fotopholder!
( https://github.com/offsky/Fotopholder )
Here are the 2 parts that have eregi:
if(substr($entry,0,1)!="." && !preg_match("#_cache#i",$entry) && is_dir($path."/".$entry)) {
and the 2nd eregi:
if(substr($entry,0,1)!="." && !eregi("_cache",$entry)) {
Thank you very much for your help.
Deprecated means, that the function eregi() is likely to get deleted from the language.
Please use preg_match().
While you still can use eregi(), at a certain point of time your application might not be able to execute any more.
That said: Your code posted is far to big to get a detailed answer.

PHP ereg_replace is not working correctly [duplicate]

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 9 years ago.
My id is a5efa5.
Code below replacing deprecated[?] [^a-z0-9] is not working. a5efa5 in an id in my database table.
//Connect to the database through our include
include_once "database.php";
// Get the member id from the URL variable
$id = $_REQUEST['id'];
$id = ereg_replace("[^a-z0-9]", "", $id); // filter everything but numbers for security
if (!$id) {
echo "Missing Data to Run";
exit();
}
Help me friends, where did I make a mistake...
It could be because ereg_replace is deprecated. Below is what is stated on the php.net website
This function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged.
If you are using a version or PHP greater than 5.3.0 then it will not work.
Use preg_replace
$id = preg_replace('#[^a-z0-9]+#', '', $id);

Is there a way to get just one element from array that is returned as a function? [duplicate]

This question already has answers here:
Accessing an array element when returning from a function
(3 answers)
Closed 9 years ago.
Say I have a function/method that returns an array, let's call it ArrayReturner(). But I only want the first element, [0]. Right now I'm doing something like...
$arrayReturned = ArrayReturner();
$varIWant = $arrayReturned[0];
Is there a way to do that in one line without the need for the temporary $arrayReturned array?
Try:
$arrayReturned = reset(ArrayReturner());
Depends on PHP's version you use.
If you're using PHP < 5.4, then you cannot get that, like ArrayReturner()[0]. That's only possible in PHP >= 5.4.
If you want your code to be portable, that would work with old and new versions, then you'd better stick with that code:
$arrayReturned = ArrayReturner();
$varIWant = $arrayReturned[0];

Moving to mysqli, mysql_result [duplicate]

This question already has answers here:
MySQLi equivalent of mysql_result()?
(12 answers)
Closed 10 months ago.
I am converting my project from mysql to mysqli and my problem is that mysqli_result() does not work with my old code. My old code is: mysql_result($res,0,0);
When I try adding mysqli_result() with my old code it will not work.
Is there another way that'll work with my old parameters?
mysqli_result() will not work with your old parameters so you need to create a new function, Here is the code for a function that'll work with your old prameters:
function mysqli_result($res, $row, $field=0) {
$res->data_seek($row);
$datarow = $res->fetch_array();
return $datarow[$field];
}

Categories