PHP SELECT query giving wrong primary key result

618

I have a table which containscNumber as primary key which contains data in this format12-43245 or12-34537.

But when select gives out the result, it actually subtracts the value and returns this-43233 and-34525 respectively.

Is there any way to get the same value returned and not the subtracted value?

Here is the code datayupe for cNumber is varchar

     $query = "SELECT 'cNumber','cNature','cStNum','cAddress','cTime','cDate','cLocation','cDsp' FROM cases";
          $result=mysql_query($query);
 while([email protected]_fetch_assoc($result)){
 $id=$row["cNumber"];
echo ($id);
}

Checked something. The$id value is printed correctly, but when I am using this$id in anotherINSERT query, then it inserts the subtracted value in the database

705

Answer

Solution:

The issue is that the primary key is integer and not characters so when you do a select it does

12-43245 = -43233 and 12-34537 = -34525.

If you want to have both digits and special characters try changing the column type to varchar or char.

try to store the id as

$id = mysql_real_escape_string($row["cNumber"]);
124

Answer

Solution:

I think the primary key is integer and not varchar due to which we the subtract occurs.

622

Answer

Solution:

PHP doesn't do anything with your data. MySQL doesn't do anything with your data when you selecting it too.

So the most likely case - you have this data in your database.

The problem in that case is in insert queries - when you insert row you should insert primary key as string, not as integer.

People are also looking for solutions to the problem: Update ordering with php?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.