How to properly retrieve unsigned bigint from MySQL column with PHP? [duplicate] - php

This question already has answers here:
Display binary(16) column as hex in mysql
(4 answers)
PHP mysql bigint issue
(3 answers)
Closed 2 years ago.
I'm not sure if it's PHP or MySQL that's messing me up.
Say, I have a table with BIGINT UNSIGNED column (let's name it flgs). I read data from that table with PHP as such:
$query = "SELECT * FROM `$tbl_nm` ORDER BY `$colID` ASC";
$res = mysqli_query($link, $query);
if($res)
{
while($aRow = mysqli_fetch_assoc($res))
{
echo("flags=0x".dechex($aRow['flgs'])."<br>");
}
}
if the flgs column has value with bit-63 reset, then I get the correct result. But if bit-63 is set, the return value is 0x7fffffffffffffff. Hmmmm?
For instance, if flgs is set to 0x8000000000000000 in the database, my code above prints:
flags=0x7fffffffffffffff
Why, PHP, why?

Related

Cannot create a table with php and sql [duplicate]

This question already has answers here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 1 year ago.
When I run this code with PHP it doesn't create a table.
$sqltablecreate = "CREATE TABLE userbase.'$username' ( ID int, category varchar(100), content varchar(1000), PRIMARY KEY (ID) )";
if(mysqli_query($conn, $sqltablecreate)){
echo "<script>alert('Table Created Successfully')</script>";
} else {
echo "<script>alert('Errors with Table creating')</script>";
}

How to get auto_increment particulars using mysqli_fetch_fields? [duplicate]

This question already has an answer here:
how to tell if column is primary key using mysqli?
(1 answer)
Closed 3 years ago.
objectice: to get auto_increment particulars of a field using mysqli_fetch_fields
code:
<?php
require_once("dbc.php");
$query="SELECT id, name, age from student WHERE 1>2";
$result=mysqli_query($dbc, $query);
$metas=mysqli_fetch_fields($result);
foreach($metas as meta){
$col_name=meta->name;
$col_type=meta->type;
$col_length=meta->length;
$col_flags=meta->flags;
echo "col_name: $col_name, col_type: $col_type, col_length: $col_length, col_flags: $col_flags";
}
mysqli_close($dbc);
?>
Observation:
in mysql prompt
mysql> describe student;
Field Type Null Key Default Extra
id int(16) NO PRI NULL auto_increment
but in mysqli
col_name: id, col_type: 3 , col_length: 11, col_flags: 49967
col_flags value 49967 corresponds to primary key.
Please guide me in getting whether a field is auto-increment using mysqli_fetch_fields?
The appropriate flag bit to test for AUTO_INCREMENT is bit 9 (AUTO_INCREMENT_FLAG) (Source). In PHP you just need to add the MYSQLI_ prefix. Something like this:
$auto_increment = $col_flags & MYSQLI_AUTO_INCREMENT_FLAG;

How to delete particular columns data in database using php mysql [duplicate]

This question already has answers here:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource
(31 answers)
Closed 7 years ago.
Hi iam trying to delete certain columns data from database.Here is my code for delete query.
//getting column id by comparing the id need to delete the data from that row.
$id=$_GET['id'];
$res = "DELETE rental_annual_rent,
rental_block,
rental_street,
rental_area,
rental_town,
rental_state,
rental_pincode
FROM house_details
WHERE house_details_id='$id'";
$result=mysql_query($res);
if(mysql_affected_rows()){
echo "successfully deleted";
session_start();
header("Location:property.php");
}else{
echo "Failure";
}
First iam inserting house_details data into database but i need to delete only particular data from that columns
What you want is an UPDATE, not a DELETE since you want to keep the row, but just clear/blank/unset certain columns within the row.
$res = "UPDATE house_details SET
rental_annual_rent = NULL,
rental_block = NULL,
rental_street = NULL,
rental_area = NULL,
rental_town = NULL,
rental_state = NULL,
rental_pincode = NULL
WHERE house_details_id='$id'";
Note that you should really be sanitizing your id input before using it in the query, you should parameterize it, and you should also migrate from the mysql library to mysqli or PDO

php prepared statement Update - sets wrong value [duplicate]

This question already has answers here:
Incorrect Integer (2147483647) is inserted into MySQL?
(11 answers)
Closed 8 years ago.
I am trying to make an update with a prepared statement, but it keeps updating the wrong value (2147483647). I can't figure out where this value is coming from. Here is my code:
$myID = 5;
$loginTokenNew = time() * rand(3, 33) * $myID;
$_SESSION['loginToken'] = $loginTokenNew;
$mysqli = connectToDB();
$stmt = $mysqli->prepare('UPDATE users SET token=? WHERE id=?') or die('Couldn\'t update user token');
$stmt->bind_param('ii', $loginTokenNew, $myID);
$stmt->execute();
$stmt->close();
$mysqli->close();
The weird thing is that the session variable takes the right value, but the "token field" in the db keeps taking the value: 2147483647
Is my prepared statement wrong somehow or could it have something to do with my db?
the field "token" is a INT (255) field btw.
2147483647 is the largest number a signed 32-bit (4-byte) INT can hold.
Change the field to a bigger type, such as BIGINT (or BIGINT UNSIGNED if the number is always positive), or to a string type, such as VARCHAR.

UPDATE table values without knowing column names PHP [duplicate]

This question already has answers here:
MySQL query to get column names?
(22 answers)
Closed 9 years ago.
Here is the situation. I just know total number of columns the table test_table has but don't know their names(sounds strange but its true). Is there any way I can write UPDATE Query for all column values on basis of some ID(auto-increment primary key)?
To add row in this table, I did following which is working but don't have idea how to do it for UPDATE:
$newCols = $_POST['newRowCols'];
$query = "INSERT INTO test_table VALUES "."("."NULL";
foreach($newCols as $col)
{
$query .= ",'$col'";
}
$query.=")";
mysqli_query($con,$query);
Thanks.
No this is not possible. But you can get the column names using the information_schema database:
SELECT
`ORDINAL_POSITION`,
`COLUMN_NAME`
FROM `information_schema`.COLUMNS
WHERE
`TABLE_SCHEMA` = $dbname
AND
`TABLE_NAME` = $tablename
ORDER BY `ORDINAL_POSITION`

Categories