Viewed   56 times

Ok i execute this

$table = get_personel_table(1);
function get_personel_table($id)
        global $connection;
        $query  = "SELECT * ";
        $query .= "FROM employees ";
        $query .= "WHERE id=" . $id . " ";
        $query .= "ORDER BY id ASC";
        $query_result = mysql_query( $query , $connection );
        $query_result_array = mysql_fetch_array($query_result);
        return $query_result_array; // returns associative array!;

and i do foreach

foreach($table as $table_var)
    echo "<td>" . $table_var . "</td>";

and by doing so i get double output ... "1 1 1 1 jordan jordan 9108121544 9108121544 testEmail testEmail testAddress testAddress testCounty testCounty"

this below is the result of print_r

        [0] => 1
        [id] => 1
        [1] => 1
        [department_id] => 1
        [2] => jordan
        [name] => jordan
        [3] => 9108121544
        [EGN] => 9108121544
        [4] => testEmail
        [email] => testEmail
        [5] => testAddress
        [address] => testAddress
        [6] => testCounty
        [country] => testCounty

The information i have in the database is id =>1 , department_id => 1 , and so on ... My question is why i get double feedback(i don't know how to call it) , 0 = id , 1 = department_id , 2 = name and so on ..

and when i do foreach( ... ) i get everything doubled.



From the manual:

mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both

By default, mysql_fetch_array gives both associative and numeric indexes. You don't want this. You can limit it with the second parameter:

$query_result_array = mysql_fetch_array($query_result, MYSQL_NUM); // numeric keys only
$query_result_array = mysql_fetch_array($query_result, MYSQL_ASSOC); // associative keys only

You can also use mysql_fetch_row to only get numeric keys, or mysql_fetch_assoc to only get associative keys.

$query_result_array = mysql_fetch_row($query_result); // numeric keys only
$query_result_array = mysql_fetch_assoc($query_result); // associative keys only
Wednesday, September 7, 2022

I would say just build it yourself. You can set it up like this:

$query = "INSERT INTO x (a,b,c) VALUES ";
foreach ($arr as $item) {
  $query .= "('".$item[0]."','".$item[1]."','".$item[2]."'),";
$query = rtrim($query,",");//remove the extra comma
//execute query

Don't forget to escape quotes if it's necessary.

Also, be careful that there's not too much data being sent at once. You may have to execute it in chunks instead of all at once.

Saturday, November 5, 2022

The function you're looking for is find_in_set:

 select * from ... where find_in_set($word, pets)

for multi-word queries you'll need to test each word and AND (or OR) the tests:

  where find_in_set($word1, pets) AND find_in_set($word2, pets) etc 
Wednesday, August 17, 2022

Interesting - although I generally don't trust normal ways of writing out floating point values when you're interested in the exact results.

Here's a slightly simpler demonstration, using DoubleConverter.cs which I've used a few times before.

using System;

class Test
    static void Main()
        decimal dcm1 = 8224055000.0000000000m;
        decimal dcm2 = 8224055000m;
        double dbl1 = (double) dcm1;
        double dbl2 = (double) dcm2;




Now the question is why the original value (8224055000.0000000000) which is an integer - and exactly representable as a double - ends up with extra data in. I strongly suspect it's due to quirks in the algorithm used to convert from decimal to double, but it's unfortunate.

It also violates section 6.2.1 of the C# spec:

For a conversion from decimal to float or double, the decimal value is rounded to the nearest double or float value. While this conversion may lose precision, it never causes an exception to be thrown.

The "nearest double value" is clearly just 8224055000... so this is a bug IMO. It's not one I'd expect to get fixed any time soon though. (It gives the same results in .NET 4.0b1 by the way.)

To avoid the bug, you probably want to normalize the decimal value first, effectively "removing" the extra 0s after the decimal point. This is somewhat tricky as it involves 96-bit integer arithmetic - the .NET 4.0 BigInteger class may well make it easier, but that may not be an option for you.

Sunday, November 6, 2022

Double quotes allow for interpolation whereas single quotes do not.

For example, using double quotes :echo "foonbar" will output foo and bar on separate lines whereas :echo 'foonbar' will not interpret n as a line break and will output foonbar literally.

For more info on different types of quotes type :h 41.2 for the help file and read the part near the end of the section with the heading STRING VARIABLES AND CONSTANTS.

This said, don't confuse quotes for strings with the double quote at the beginning of a line comment. Single quotes never start line comments, only double quotes do.

Saturday, December 17, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :