Moving to mysqli, mysql_result [duplicate] - php

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];
}

Related

PHP Function create_function() is deprecated , the code cant work in newest PHP? [duplicate]

This question already has answers here:
PHP 7.2 Function create_function() is deprecated
(6 answers)
Closed 1 year ago.
I have a function which im not entirely sure how to convert it to get working in newest php
$eventSponsor = array_map(create_function('$o', 'return $o["id"];'), $event->sponsors);
which method should i use in newest php version ?
yeah i was searching and i found this method called anonymous function so the code be like
$awe function ($o) {
return $o["id"];
};
$eventSponsor = array_map($awe,$event->sponsors); ```

How to use SQL TRIM Function in Laravel [duplicate]

This question already has answers here:
How to perform TRIM() and CONCAT() in Laravel Query Builder?
(4 answers)
Closed 3 years ago.
I'm working on a school project that register and login with "username", as far as i tested i can't find any good method to work, I've found this function of SQL TRIM which removes spaces and other character if anyone knows how to work with it in laravel.
reference:
http://www.sqltutorial.org/sql-string-functions/sql-trim/
You can use this Example:
$data = DB::table('table_name')
->select(
DB::raw("TRIM(CONCAT(field1,' ',field2,' ',field3)) AS Name")
)->get();

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

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;

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);

Convert PDO recordset to JSON in PHP [duplicate]

This question already has answers here:
How to create a JSON object
(5 answers)
Closed 2 years ago.
Im using PHP and I need a way to convert an entire recordset to a JSON string.
While searching Stack Overflow I found this solution that works:
function recordSetToJson($mysql_result) {
$rs = array();
while($rs[] = mysql_fetch_assoc($mysql_result)) {
// you donĀ“t really need to do anything here.
}
return json_encode($rs);
}
The problem with this code is that I found that the function mysql_fetch_assoc() is deprecated in PHP 5.5.0. Another thing is that im using PDO to connect to my database.
Given the circunstances above, what would be the best solution to convert a PDO recordset to JSON? I want it to work at later versions of PHP too.
The solution is simple.
Considering that the variable $stmt is your PDO recordset, you can convert it to JSON like this:
json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));
For more info about the functions used in this piece of code:
http://www.php.net/manual/en/function.json-encode.php
http://www.php.net/manual/en/pdostatement.fetchall.php
You should use something like that
somewhere previously
$stmt = $pdo->prepare("SELECT * FROM fruit WHERE name = ?");
$stmt->execute(array("Apple"));
....
function recordSetToJson($stmt) {
$json_result = array();
while($tmp = $stmt->fetch() ) {
$json_result[] = $tmp;
}
return json_encode($json_result);
}
But final solution will be totally depends form too many factors.

Categories