如何使用数字将mysql转换为数组结果
I have php code like this
$sql = mysql_query("SELECT userName FROM `rcpa_users` WHERE upline_users = '$selectuname'");
while($tjans = mysql_fetch_array($sql)){
print_r($tjans);
}
But output show this
Array ( [userName] => mita )
Array ( [userName] => wendyang )
Array ( [userName] => viviekosasih )
Now, how to convert array like this result
Array
(
[0] => mita
[1] => wendyang
[2] => viviekosasih
)
so I can call with echo $tjans[0], $tjans[1], $tjans[2]
Thank You..
mysql_fetch_assoc()
- Fetch a result row as an associative array.This function will return a row as an associative array where the column names will be the keys storing corresponding value.
Description: Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead. mysql_fetch_assoc() is equivalent to calling mysql_fetch_array() with MYSQL_ASSOC for the optional second parameter. It only returns an associative array.
After that you need to do the array_values()
so that it converts all the values to array with numeric keys.
array_values
— Return all the values of an array
array_values()
returns all the values from the array and indexes the array numerically.
If your query returns multiple rows as output you can prefer this method:
$sqlxx = mysql_query("SELECT userName FROM rcpa_users WHERE upline_users = '$selectuname'");
$final_tjans = array();
while($tjans = mysql_fetch_assoc($sqlxx))
{
$final_tjans[] = $tjans['userName'];
}
print_r($final_tjans);
If you code returns only single row as output:
$sqlxx = mysql_query("SELECT userName FROM rcpa_users WHERE upline_users = '$selectuname'");
$tjans = mysql_fetch_assoc($sqlxx));
$final_tjans = array_values($tjans);
print_r($final_tjans);
Example:
<?php
$array = array("size" => "XL", "color" => "gold");
print_r(array_values($array));
?>
Output:
Array
(
[0] => XL
[1] => gold
)
Note: Usage of
mysql
extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.
You need not run a loop at all. Store your result in an array:
$tjans = mysql_fetch_assoc($sql);
$tjans = array_values($tjans);
When you have multiple records then simple getting array with array_values will not work as it will only returns a single row . To get all rows from this query you need to fetch through each row . Like below
if (mysql_num_rows($result) > 0) {
$tjan = array();
while($row = mysql_fetch_assoc($result) ) {
$tjan[] = $row['price'];
}
print_r($tjan);
}
Then add 'price' field in an array that must be blank before loop. At last you will get an array as you want . Cheers ,
First you should use mysqli instead of mysql as it is depreciated and there is no need to use the loop of any kind as your Select query will fetch all the records matching your where condition. Following is the code snippet you may use.
$sql = mysql_query("SELECT userName FROM `rcpa_users` WHERE upline_users = '$selectuname'");
$tjans = mysqli_fetch_assoc($sql);
print_r(array_values($tjans));
All you have to do is create an array and store the userName into it, and outside the while loop print the array to see the output.
$new_array = array();
$sql = mysqli_query($conn, "SELECT userName FROM `rcpa_users` WHERE upline_users = '$selectuname'");
while($tjans = mysqli_fetch_object($sql)){
$new_array[] = $tjans->userName;
}
print_r($new_array);
What i change in your code??
- Remove all the
mysql_*
intomysqli_*
. - Create an array for the further store of
userName
and store. - print the new array outside the loop.
- And the final one is use the
$conn
variable. This is the connection string, you have to learn about this. click here
Note: The original MySQL extension is now deprecated, and will generate E_DEPRECATED errors when connecting to a database. Instead, use the MySQLi or PDO_MySQL extensions.