Can I select values from one column in an php-array? [duplicate] - php

This question already has answers here:
mysqli_fetch_array returning only one result
(3 answers)
Closed 6 years ago.
I need only the values of one column in an array. Without php I would use "SELECT valueX FROM tableY".
This does not work with php. I only get one result.
This is what I have:
$salty = "SELECT salt FROM login";
$salts = mysqli_query($connection, $salty);
$validsalts = mysqli_fetch_array($salts);

You have to make a loop iteration for fetching the all value.
for example.
$salty = "SELECT salt FROM login";
$salts = mysqli_query($connection, $salty);
while($validsalts = mysqli_fetch_array($salts))
{
echo $validsalts['salt'];
}

Related

Select * in a remote DB (MySQL) doesn't bring all the data [duplicate]

This question already has answers here:
get array of rows with mysqli result
(2 answers)
Closed 1 year ago.
The query is simple -> "SELECT * FROM xxx";
When the result comes, it brings only the last result of the table instead all.
If I'm not mistaken, most remote databases come with a limit on the amount of data that is fetched, in order to avoid overflowing results.
But none of that is for sure, just my speculation.
Anyway, how to solve this?
$stmt = mysqli_stmt_init($conn);
$sql = "SELECT * FROM favorite";
mysqli_stmt_prepare($stmt, $sql);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$row = mysqli_fetch_assoc($result);
var_dump($row);
Try
$row = mysqli_fetch_all($result);
mysqli_fetch_assoc();
only returns 1 row which explains why you only see the last row of the expected result set.
mysqli_fetch_all

num_rows issue with returning rows from SELECT query [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 7 years ago.
I have an issue. I am trying to find out if a user's email already exists in the database here is my query:
$stmt1 = "select EmailAddress from customers where EmailAddress = ' .$emailaddress. '";
$result = $db->query($stmt1);
if($result->num_rows === 0){
$Err = "";
} else {
$Err = 'This user is already registered login instead.';
}
What am I doing wrong? I can't seem to get num_rows to return something I can work with. Shouldn't this query return 0 if no records are found or number of rows if there is a record?
Use :
$stmt1 = "SELECT EmailAddress FROM customers WHERE EmailAddress = '".$emailaddress."'";

PDO:: Confusion [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 7 years ago.
I have no idea why this is not returning anything. I'll show the code and talk through the steps I've taken.
if (isset($_GET['observation'])) {
require_once("../func/connect.php");
$query = "SELECT * FROM observations WHERE option = ?";
$stmt = $db->prepare($query);
$stmt->bindValue(1, $_GET['observation']);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo $row['question'];
} else {
echo 'nope';
}
$row dumps a false boolean, $row['question'] is null.
I've wrote about a million queries and don't have a clue why this doesn't work.
Database table observations consists of id, question & option and the bindValue is correct to match a string in the database.
However, it returns null.
option is a reserved word in mysql so you need to quote it with backticks:
$query = "SELECT * FROM observations WHERE `option` = ?";

How to compare $_POST to existing Text in table? [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 7 years ago.
I'm developing a mobile application for android and I'm trying to compare a variable on the phone to a variable already in the database, so that I can insert it if it's new and update it if it already exists.
$name_check = $_POST['Name'];
$result = mysqli_query($con, "SELECT * FROM Data WHERE Name = $name_check");
if($result && mysqli_num_rows($result) > 0)
{
// Update entry
}
This code doesn't seem to work as this block is skipped over and goes to my else block where a new entry is written, so I end up with loads of entries instead of updating one.
I have another field in the table called "Level", and when I compare against that it seems to work, which just confuses me further.
If anyone has any insight into how to do this or why it's not working for me I'd be very grateful.
Use quotes:
$result = mysqli_query($con, "SELECT * FROM Data WHERE Name = '$name_check'");
$result = mysqli_query($con, "SELECT * FROM Data WHERE Name = '".$name_check."'");
This should work fine
Use this:
$result = mysqli_query($con, "SELECT * FROM Data WHERE Name = '" . $name_check . "'");

How to change mysql_fetch_assoc into mysqli [duplicate]

This question already has answers here:
How do I migrate my site from mysql to mysqli? [duplicate]
(3 answers)
Closed 3 years ago.
How can i change this into mysqli?
// add meta_key to the query
$get_user_data = mysql_query("SELECT `meta_value`,`meta_key` FROM `table` WHERE `user_id` = $user_id", $link);
// use _assoc instead of _array, we do not need the numerical indexes.
while($row=mysql_fetch_assoc($get_user_data)){
$userdata[$row['meta_key']] = $row['meta_value'];
}
Just simple changes you have to made in your code:
// add meta_key to the query
$get_user_data = mysqli_query($link,"SELECT `meta_value`,`meta_key` FROM `table` WHERE `user_id` = $user_id");
// use _assoc instead of _array, we don't need the numerical indexes.
while($row=mysqli_fetch_assoc($get_user_data))
{
$userdata[$row['meta_key']] = $row['meta_value'];
}

Categories